Class: OAuth2::Rack::Authentication::Client::HTTPBasic

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2/rack/authentication/client/http_basic.rb

Overview

2.3.1. Client Password

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HTTPBasic.



7
8
9
10
11
12
# File 'lib/oauth2/rack/authentication/client/http_basic.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
37
38
39
# File 'lib/oauth2/rack/authentication/client/http_basic.rb', line 14

def call(env)
  return @app.call(env) if env.has_key?('oauth2.client')

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

  if auth_string.nil?
    return @required ? unauthorized : @app.call(env)
  end

  schema, credentials = auth_string.split(' ', 2)
  if schema.downcase != 'basic'
    return bad_request
  end

  client_id, client_secret = credentials.unpack('m*').first.split(':', 2)
  client = @authenticator.call(:client_id => client_id,
                               :client_secret => client_secret)

  if client
    env['oauth2.client'] = client
    @app.call(env)
  else
    unauthorized
  end
end