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

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

Returns a new instance of RequestParams



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

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

Instance Method Details

- (Object) call(env)



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

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

  resource_owner = @authenticator.call(:username => username, :password => password)
  if resource_owner
    env['oauth2.resource_owner'] = resource_owner
    @app.call(env)
  else
    unauthorized
  end
end