Class: OAuth2::Rack::Authentication::ResourceOwner::RequestParams

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2/rack/authentication/resource_owner/request_params.rb

Overview

Authenticate resource owner with request params

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RequestParams.



5
6
7
8
9
# File 'lib/oauth2/rack/authentication/resource_owner/request_params.rb', line 5

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

Instance Method Details

#call(env) ⇒ Object



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

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

  @request = Rack::Request.new(env)

  username = @request['username']
  password = @request['password']
  if username.nil? && password.nil?
    return @required ? unauthorized : @app.call(env)
  elsif username.nil? || password.nil?
    return bad_request
  end

  credentials = {
    :username => username,
    :password => password
  }
  credentials[:client] = env['oauth2.client'] if env['oauth2.client']
  resource_owner = @authenticator.call credentials

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