Class: BasicApiAuth::Middleware

Inherits:
Object
  • Object
show all
Includes:
HashkeyGenerator
Defined in:
lib/basic_api_auth.rb

Constant Summary collapse

@@authenticity_error =
[401, {"Content-Type" => "text/plain"}, ['{ "message": "Authentication Failed" }']]
@@hashkey_requirement_error =
[401, {"Content-Type" => "text/plain"}, ['{ "message": "Hashkey is required for authentication" }']]

Instance Method Summary collapse

Methods included from HashkeyGenerator

#concat_params, #generate_hashkey_for, #sort_params

Constructor Details

#initialize(app, api_key) ⇒ Middleware

Returns a new instance of Middleware.



11
12
13
14
# File 'lib/basic_api_auth.rb', line 11

def initialize(app, api_key)
  @app = app
  @api_key = api_key
end

Instance Method Details

#authenticate_hashkeyObject



24
25
26
# File 'lib/basic_api_auth.rb', line 24

def authenticate_hashkey
  @hashkey == generate_hashkey_for(@params, @api_key)
end

#call(env) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/basic_api_auth.rb', line 16

def call(env)
  @params = Rack::Request.new(env).params
  @hashkey = @params.delete('hashkey')
  return @@hashkey_requirement_error unless @hashkey
  return @@authenticity_error unless authenticate_hashkey
  @app.call(env)
end