Class: OAuth2::AuthRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth20/auth_request.rb

Overview

Class to represent incoming authorization request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_key, response_type, options = {}) ⇒ AuthRequest

Initialize OAuth flow request with given attributes.

Parameters:

  • Unique (String)

    client identifier.

  • Type (String)

    of the response expected.

  • Additional (Hash)

    hash of commands (recirect_uri, scope, state).



15
16
17
18
19
20
21
22
23
# File 'lib/oauth20/auth_request.rb', line 15

def initialize(client_key, response_type, options = {})
  @client_id = client_key
  @response_type = response_type
  @redirect_uri = options[:redirect_uri] || nil
  @scope = options[:scope] || nil
  @state = options[:state] || nil
  
  validate!
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/oauth20/auth_request.rb', line 6

def client
  @client
end

#client_idObject (readonly)

Returns the value of attribute client_id.



6
7
8
# File 'lib/oauth20/auth_request.rb', line 6

def client_id
  @client_id
end

#redirect_uriObject (readonly)

Returns the value of attribute redirect_uri.



6
7
8
# File 'lib/oauth20/auth_request.rb', line 6

def redirect_uri
  @redirect_uri
end

#response_typeObject (readonly)

Returns the value of attribute response_type.



6
7
8
# File 'lib/oauth20/auth_request.rb', line 6

def response_type
  @response_type
end

#scopeObject (readonly)

Returns the value of attribute scope.



6
7
8
# File 'lib/oauth20/auth_request.rb', line 6

def scope
  @scope
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/oauth20/auth_request.rb', line 6

def state
  @state
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/oauth20/auth_request.rb', line 6

def user
  @user
end

Instance Method Details

#responseAuthResponse

Get the response object. Its gonna raise error unless user was stored to the request. That should happen after user had used valid credentials to login to authorization server.

Returns:

  • (AuthResponse)

    AuthResponse object with all necessary attributes.

Raises:



32
33
34
35
36
# File 'lib/oauth20/auth_request.rb', line 32

def response
  raise AuthError.new(OAuth2::ERROR_ACCESS_DENIED) unless @user
  
  AuthResponse.new(self)
end

#validate!Object

Validate if the request parameters match to the protocol specification.

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/oauth20/auth_request.rb', line 42

def validate!
  unless @response_type && @client_id
    raise AuthError.new(OAuth2::ERROR_INVALID_REQUEST)
  end
  
  @client = Client.find_by_key(@client_id)
  raise AuthError.new(OAuth2::ERROR_INVALID_CLIENT) unless @client
  
  #if @redirect_uri && @client.redirect_uri
  #  raise AuthError.new(OAuth2::ERROR_INVALID_REQUEST) unless @redirect_uri == @client.redirect_uri
  #end
  
  @redirect_uri = @client.redirect_uri unless @redirect_uri && @client.redirect_uri
  
  
  unless @response_type == 'code'
    raise AuthError.new(OAuth2::ERROR_UNSUPPORTED_RESPONSE_TYPE)
  end
end