Class: OAuth2::Strategy::Password

Inherits:
Base
  • Object
show all
Defined in:
lib/oauth2/strategy/password.rb

Instance Method Summary collapse

Methods inherited from Base

#access_token_url, #authorize_params, #initialize, #refresh_token_params

Constructor Details

This class inherits a constructor from OAuth2::Strategy::Base

Instance Method Details

#access_token_params(username, password, options = {}) ⇒ Object

:nodoc:



29
30
31
32
33
34
35
# File 'lib/oauth2/strategy/password.rb', line 29

def access_token_params(username, password, options={}) #:nodoc:
  super(options).merge({
    'grant_type'  => 'password',
    'username'    => username,
    'password'    => password
  })
end

#authorize_urlObject

Raises:

  • (NotImplementedError)


6
7
8
# File 'lib/oauth2/strategy/password.rb', line 6

def authorize_url
  raise NotImplementedError, "The authorization endpoint is not used in this strategy"
end

#get_access_token(username, password, options = {}) ⇒ Object

Retrieve an access token given the specified validation code. Note that you must also provide a :redirect_uri option in order to successfully verify your request for most OAuth 2.0 endpoints.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/oauth2/strategy/password.rb', line 13

def get_access_token(username, password, options={})
  response = @client.request(:post, @client.access_token_url, access_token_params(username, password, options))

  params   = MultiJson.decode(response) rescue nil
  # the ActiveSupport JSON parser won't cause an exception when
  # given a formencoded string, so make sure that it was
  # actually parsed in an Hash. This covers even the case where
  # it caused an exception since it'll still be nil.
  params   = Rack::Utils.parse_query(response) unless params.is_a? Hash

  access   = params.delete('access_token')
  refresh  = params.delete('refresh_token')
  expires_in = params.delete('expires_in')
  OAuth2::AccessToken.new(@client, access, refresh, expires_in, params)
end