Class: Auth::Middleware

Inherits:
Rack::Auth::AbstractHandler
  • Object
show all
Defined in:
lib/auth/middleware.rb

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Constructor Details

#initialize(app, realm = nil, options = {}, &authenticator) ⇒ Middleware

Returns a new instance of Middleware.



10
11
12
13
# File 'lib/auth/middleware.rb', line 10

def initialize(app, realm=nil, options={}, &authenticator)
  super(app, realm, &authenticator)
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/auth/middleware.rb', line 15

def call(env)
  auth = Request.new(env)

  unless @options[:allow_unauthenticated]
    return unauthorized unless auth.provided?
    return bad_request unless auth.bearer?
  end

  if auth.provided? && valid?(auth)
    env['REMOTE_USER'] = auth.
    return @app.call(env)
  end

  if @options[:allow_unauthenticated]
    res = @app.call(env)
    return [res[0],
            res[1].merge('WWW-Authenticate' => challenge),
            res[2]]
  else
    unauthorized
  end
end