Class: AuthHMAC::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/auth-hmac/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, credentials) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
6
# File 'lib/auth-hmac/middleware.rb', line 3

def initialize(app, credentials)
  @app = app
  @hmac = AuthHMAC.new(credentials)
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
# File 'lib/auth-hmac/middleware.rb', line 8

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/auth-hmac/middleware.rb', line 12

def call!(env)
  @env = env

  if @hmac.authenticated?(request)
    rx = Regexp.new("#{@hmac.service_id} ([^:]+):(.+)$")

    if md = rx.match(@hmac.authorization_header(request))
      env["auth-hmac.access_key_id"] = md[1]
    else
      raise "Unknown request"
    end

    @app.call(env)
  else
    Rack::Response.new("Authorization required", 401).finish
  end
end

#requestObject



30
31
32
# File 'lib/auth-hmac/middleware.rb', line 30

def request
  @request ||= Rack::Request.new(@env)
end