Class: Songkick::OAuth2::Provider::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/songkick/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, transport_error = nil) ⇒ Exchange


19
20
21
22
23
24
25
26
27
# File 'lib/songkick/oauth2/provider/exchange.rb', line 19

def initialize(resource_owner, params, transport_error = nil)
  @params     = params
  @scope      = params[SCOPE]
  @grant_type = @params[GRANT_TYPE]

  @transport_error = transport_error

  validate!
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


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

def client
  @client
end

#errorObject (readonly)

Returns the value of attribute error


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

def error
  @error
end

#error_descriptionObject (readonly)

Returns the value of attribute error_description


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

def error_description
  @error_description
end

Instance Method Details

#ownerObject


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

def owner
  @authorization && @authorization.owner
end

#redirect?Boolean


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

def redirect?
  false
end

#response_bodyObject


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

def response_body
  return jsonize(ERROR, ERROR_DESCRIPTION) unless valid?
  update_authorization

  response = {}
  [ACCESS_TOKEN, REFRESH_TOKEN, SCOPE].each do |key|
    value = @authorization.__send__(key)
    response[key] = value if value
  end
  if expiry = @authorization.expires_in
    response[EXPIRES_IN] = expiry
  end

  JSON.unparse(response)
end

#response_headersObject


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

def response_headers
  RESPONSE_HEADERS
end

#response_statusObject


57
58
59
# File 'lib/songkick/oauth2/provider/exchange.rb', line 57

def response_status
  valid? ? 200 : 400
end

#scopesObject


61
62
63
64
# File 'lib/songkick/oauth2/provider/exchange.rb', line 61

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

#update_authorizationObject


66
67
68
69
70
# File 'lib/songkick/oauth2/provider/exchange.rb', line 66

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

#valid?Boolean


72
73
74
# File 'lib/songkick/oauth2/provider/exchange.rb', line 72

def valid?
  @error.nil?
end