Class: OAuth2::TokenRequest
- Inherits:
-
Object
- Object
- OAuth2::TokenRequest
- Defined in:
- lib/oauth20/token_request.rb
Overview
Token request instance represents one request for a new token. Each token request is done on behalf of specific user and for one specific application. Parameters sent along have to match to the grant type used.
If any requred param is not present, or request is other way malformed, the response error is returned. If everything is correct new access token is issued and returned in response.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#grant_type ⇒ Object
readonly
Returns the value of attribute grant_type.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#initialize(client_secret, grant_type, options) ⇒ TokenRequest
constructor
Initialize new token request.
- #response ⇒ Object
-
#validate! ⇒ Object
Validate request params to match ones specified by protocol for a given grant type.
Constructor Details
#initialize(client_secret, grant_type, options) ⇒ TokenRequest
Initialize new token request.
21 22 23 24 25 26 27 |
# File 'lib/oauth20/token_request.rb', line 21 def initialize(client_secret, grant_type, ) @client_secret = client_secret @grant_type = grant_type @options = validate! end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
13 14 15 |
# File 'lib/oauth20/token_request.rb', line 13 def client @client end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
13 14 15 |
# File 'lib/oauth20/token_request.rb', line 13 def client_secret @client_secret end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
13 14 15 |
# File 'lib/oauth20/token_request.rb', line 13 def code @code end |
#grant_type ⇒ Object (readonly)
Returns the value of attribute grant_type.
13 14 15 |
# File 'lib/oauth20/token_request.rb', line 13 def grant_type @grant_type end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/oauth20/token_request.rb', line 13 def @options end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
13 14 15 |
# File 'lib/oauth20/token_request.rb', line 13 def user_id @user_id end |
Instance Method Details
#response ⇒ Object
29 30 31 |
# File 'lib/oauth20/token_request.rb', line 29 def response TokenResponse.new(self) end |
#validate! ⇒ Object
Validate request params to match ones specified by protocol for a given grant type.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/oauth20/token_request.rb', line 38 def validate! case @grant_type when 'token' raise AuthError.new(OAuth2::ERROR_INVALID_REQUEST) unless @options[:code] && @options[:redirect_uri] @code = AuthCode.find_by_key(@options[:code]) if @code.used? access_token = OAuth2::AccessToken.find_by_key(@code.access_token) access_token.revoke! raise AuthError.new(OAuth2::ERROR_INVALID_GRANT, 'expired_or_invalid_auth_code') end raise AuthError.new(OAuth2::ERROR_INVALID_GRANT, 'expired_or_invalid_auth_code') if @code.nil? || @code.expired? @client = Client.find_by_key(@code.client_key) raise AuthError.new(OAuth2::ERROR_INVALID_GRANT, 'invalid_client_credentials') if !@client.secret.eql?(@client_secret) @user_id = @code.user_id else raise AuthError.new(OAuth2::ERROR_UNSUPPORTED_GRANT_TYPE) end true end |