Class: OAuth2::TokenRequest

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(client_secret, grant_type, options) ⇒ TokenRequest

Initialize new token request.

Parameters:

  • Secret (String)

    key of the OAuth2::Client.

  • Request (String)

    grant type.

  • Additional (Hash)

    params requred for specific grant type.



21
22
23
24
25
26
27
# File 'lib/oauth20/token_request.rb', line 21

def initialize(client_secret, grant_type, options)
  @client_secret = client_secret
  @grant_type = grant_type
  @options = options
  
  validate!
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/oauth20/token_request.rb', line 13

def client
  @client
end

#client_secretObject (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

#codeObject (readonly)

Returns the value of attribute code.



13
14
15
# File 'lib/oauth20/token_request.rb', line 13

def code
  @code
end

#grant_typeObject (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

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/oauth20/token_request.rb', line 13

def options
  @options
end

#user_idObject (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

#responseObject



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