Skip to content

Instantly share code, notes, and snippets.

@aaripurna
Last active June 23, 2022 13:57
Show Gist options
  • Save aaripurna/6ac923e52bba57f5390f13c9e97002d4 to your computer and use it in GitHub Desktop.
Save aaripurna/6ac923e52bba57f5390f13c9e97002d4 to your computer and use it in GitHub Desktop.
Rack Middleware
class ApiKeyValidator
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.has_header?('X-API-KEY') && request.get_header('X-API-KEY') == ENV['API_KEY']
return @app.call(env)
end
[401, {'Content-Type' => 'text/plain'}, ['Unauthorized']]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment