Class: OAuth::Provider::Authorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth/provider/authorizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, authorized, params = {}) ⇒ Authorizer

Returns a new instance of Authorizer.



8
9
10
11
12
# File 'lib/oauth/provider/authorizer.rb', line 8

def initialize(user, authorized, params = {})
  @user = user
  @params = params
  @authorized = authorized
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



6
7
8
# File 'lib/oauth/provider/authorizer.rb', line 6

def app
  @app
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/oauth/provider/authorizer.rb', line 6

def params
  @params
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/oauth/provider/authorizer.rb', line 6

def user
  @user
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/oauth/provider/authorizer.rb', line 32

def authorized?
  @authorized == true
end

#codeObject



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

def code
  @code ||= ::Oauth2Verifier.create! :client_application => app,
                                :user => @user,
                                :scope => @params[:scope],
                                :callback_url => @params[:redirect_uri]
end

#encode_responseObject



70
71
72
73
74
# File 'lib/oauth/provider/authorizer.rb', line 70

def encode_response
  response.map do |k, v|
    [URI.escape(k.to_s),URI.escape(v)] * "="
  end * "&"
end

#redirect_uriObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oauth/provider/authorizer.rb', line 36

def redirect_uri
  uri = base_uri
  if params[:response_type] == 'code'
    if uri.query
      uri.query << '&'
    else
      uri.query = ''
    end
    uri.query << encode_response
  else
    uri.fragment = encode_response
  end
  uri.to_s
end

#responseObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/oauth/provider/authorizer.rb', line 51

def response
  r = {}
  if ['token','code'].include? params[:response_type]
    if authorized?
      if params[:response_type] == 'code'
        r[:code] = code.token
      else
        r[:access_token] = token.token
      end
    else
      r[:error] = 'access_denied'
    end
  else
    r[:error] = 'unsupported_response_type'
  end
  r[:state] = params[:state] if params[:state]
  r
end

#tokenObject



25
26
27
28
29
30
# File 'lib/oauth/provider/authorizer.rb', line 25

def token
  @token ||= ::Oauth2Token.create! :client_application => app,
                                :user => @user,
                                :scope => @params[:scope],
                                :callback_url => @params[:redirect_uri]
end