Class: Rack::MogueraAuthentication

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/moguera_authentication.rb

Overview

A Rack middleware inserts a Moguera Authentication.

Examples:

config.ru

# Add Rack::MogueraAuthentication somewhere in your rack stack
use Rack::MogueraAuthentication do |request_access_key|

  # Search a secret_access_key by a request_access_key
  key_to_secret(request_access_key)
end

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ MogueraAuthentication

Returns a new instance of MogueraAuthentication.



14
15
16
17
# File 'lib/rack/moguera_authentication.rb', line 14

def initialize(app, &block)
  @app = app
  @secret_block = block
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/moguera_authentication.rb', line 19

def call(env)
  begin
    request_token = Moguera::Authentication.new(env['HTTP_AUTHORIZATION'])
    auth = request_token.authenticate! do |access_key|
      secret_access_key = @secret_block.call(access_key)
      params = build_parameter(access_key, secret_access_key, env)

      Moguera::Authentication::Request.new(params)
    end

    env['moguera.auth'] = auth
  rescue => e
    env['moguera.error'] = e
  end

  @app.call(env)
end