Class: OAuth2::Rack::Authentication::AccessToken::BearerHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2/rack/authentication/access_token/bearer_header.rb

Overview

  1. Accessing Protected Resources

Constant Summary collapse

HEADER_KEYS =
['HTTP_AUTHORIZATION', 'X-HTTP_AUTHORIZATION', 'X_HTTP_AUTHORIZATION']

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}, &authenticator) ⇒ BearerHeader

Returns a new instance of BearerHeader.



7
8
9
10
11
12
# File 'lib/oauth2/rack/authentication/access_token/bearer_header.rb', line 7

def initialize(app, opts = {}, &authenticator)
  @app = app
  @realm = opts[:realm]
  @required = opts.fetch(:required, true)
  @authenticator = authenticator || opts[:authenticator]
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/oauth2/rack/authentication/access_token/bearer_header.rb', line 14

def call(env)
  key = HEADER_KEYS.find { |k| env.has_key?(k) }
  auth_string = env[key]

  if auth_string.nil?
    return @required ? error_response('code' => 400, 'error' => 'invalid_request') : @app.call(env)
  end

  schema, credentials = auth_string.split(' ', 2)
  if schema.downcase != 'bearer'
    return error_response('code' => 400,
                          'error' => 'invalid_request')
  end

  access_grant = @authenticator.call(:access_token => credentials)

  if access_grant.nil? || (access_grant.is_a?(Hash) && access_grant[:error])
    error_response(access_grant)
  else
    env['oauth2.access_grant'] = access_grant
    @app.call(env)
  end
end