Class: OAuth2::Provider::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2/provider/exchange.rb

Constant Summary collapse

REQUIRED_PARAMS =
[CLIENT_ID, CLIENT_SECRET, GRANT_TYPE]
VALID_GRANT_TYPES =
[AUTHORIZATION_CODE, PASSWORD, ASSERTION, REFRESH_TOKEN]
REQUIRED_PASSWORD_PARAMS =
[USERNAME, PASSWORD]
REQUIRED_ASSERTION_PARAMS =
[ASSERTION_TYPE, ASSERTION]
RESPONSE_HEADERS =
{
  'Cache-Control' => 'no-store',
  'Content-Type'  => 'application/json'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_owner, params) ⇒ Exchange

Returns a new instance of Exchange.



18
19
20
21
22
23
# File 'lib/oauth2/provider/exchange.rb', line 18

def initialize(resource_owner, params)
  @params     = params
  @scope      = params[SCOPE]
  @grant_type = @params[GRANT_TYPE]
  validate!
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/oauth2/provider/exchange.rb', line 5

def client
  @client
end

#errorObject (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/oauth2/provider/exchange.rb', line 5

def error
  @error
end

#error_descriptionObject (readonly)

Returns the value of attribute error_description.



5
6
7
# File 'lib/oauth2/provider/exchange.rb', line 5

def error_description
  @error_description
end

Instance Method Details

#ownerObject



25
26
27
# File 'lib/oauth2/provider/exchange.rb', line 25

def owner
  @authorization && @authorization.owner
end

#redirect?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/oauth2/provider/exchange.rb', line 33

def redirect?
  false
end

#response_bodyObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/oauth2/provider/exchange.rb', line 37

def response_body
  return jsonize(ERROR, ERROR_DESCRIPTION) unless valid?
  update_authorization
  
  response = {}
  %w[access_token refresh_token scope].each do |key|
    value = @authorization.__send__(key)
    response[key] = value if value
  end
  
  JSON.unparse(response)
end

#response_headersObject



50
51
52
# File 'lib/oauth2/provider/exchange.rb', line 50

def response_headers
  RESPONSE_HEADERS
end

#response_statusObject



54
55
56
# File 'lib/oauth2/provider/exchange.rb', line 54

def response_status
  valid? ? 200 : 400
end

#scopesObject



29
30
31
# File 'lib/oauth2/provider/exchange.rb', line 29

def scopes
  @scope ? @scope.split(/\s+/).delete_if { |s| s.empty? } : []
end

#update_authorizationObject



58
59
60
61
62
# File 'lib/oauth2/provider/exchange.rb', line 58

def update_authorization
  return if not valid? or @already_updated
  @authorization.exchange!
  @already_updated = true
end

#valid?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/oauth2/provider/exchange.rb', line 64

def valid?
  @error.nil?
end