Class: OmniAuth::Strategies::OAuth

Inherits:
Object
  • Object
show all
Includes:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategies/oauth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, name, consumer_key = nil, consumer_secret = nil, consumer_options = {}, options = {}, &block) ⇒ OAuth

Returns a new instance of OAuth.



10
11
12
13
14
15
16
17
18
# File 'lib/omniauth/strategies/oauth.rb', line 10

def initialize(app, name, consumer_key=nil, consumer_secret=nil, consumer_options={}, options={}, &block)
  self.consumer_key = consumer_key
  self.consumer_secret = consumer_secret
  self.consumer_options = consumer_options
  super
  self.options[:open_timeout] ||= 30
  self.options[:read_timeout] ||= 30
  self.options[:authorize_params] = options[:authorize_params] || {}
end

Instance Attribute Details

#consumer_keyObject

Returns the value of attribute consumer_key.



28
29
30
# File 'lib/omniauth/strategies/oauth.rb', line 28

def consumer_key
  @consumer_key
end

#consumer_optionsObject

Returns the value of attribute consumer_options.



28
29
30
# File 'lib/omniauth/strategies/oauth.rb', line 28

def consumer_options
  @consumer_options
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



28
29
30
# File 'lib/omniauth/strategies/oauth.rb', line 28

def consumer_secret
  @consumer_secret
end

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/omniauth/strategies/oauth.rb', line 27

def name
  @name
end

Instance Method Details

#auth_hashObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/omniauth/strategies/oauth.rb', line 73

def auth_hash
  OmniAuth::Utils.deep_merge(super, {
    'credentials' => {
      'token' => @access_token.token,
      'secret' => @access_token.secret
    }, 'extra' => {
      'access_token' => @access_token
    }
  })
end

#callback_phaseObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/omniauth/strategies/oauth.rb', line 47

def callback_phase
  raise OmniAuth::NoSessionError.new("Session Expired") if session['oauth'].nil?

  request_token = ::OAuth::RequestToken.new(consumer, session['oauth'][name.to_s].delete('request_token'), session['oauth'][name.to_s].delete('request_secret'))

  opts = {}
  if session['oauth'][name.to_s]['callback_confirmed']
    opts[:oauth_verifier] = request['oauth_verifier']
  else
    opts[:oauth_callback] = callback_url
  end

  @access_token = request_token.get_access_token(opts)
  super
rescue ::Timeout::Error => e
  fail!(:timeout, e)
rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
  fail!(:service_unavailable, e)
rescue ::OAuth::Unauthorized => e
  fail!(:invalid_credentials, e)
rescue ::NoMethodError, ::MultiJson::DecodeError => e
  fail!(:invalid_response, e)
rescue ::OmniAuth::NoSessionError => e
  fail!(:session_expired, e)
end

#consumerObject



20
21
22
23
24
25
# File 'lib/omniauth/strategies/oauth.rb', line 20

def consumer
  consumer = ::OAuth::Consumer.new(consumer_key, consumer_secret, consumer_options.merge(options[:client_options] || options[:consumer_options] || {}))
  consumer.http.open_timeout = options[:open_timeout] if options[:open_timeout]
  consumer.http.read_timeout = options[:read_timeout] if options[:read_timeout]
  consumer
end

#request_phaseObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omniauth/strategies/oauth.rb', line 30

def request_phase
  request_token = consumer.get_request_token(:oauth_callback => callback_url)
  session['oauth'] ||= {}
  session['oauth'][name.to_s] = {'callback_confirmed' => request_token.callback_confirmed?, 'request_token' => request_token.token, 'request_secret' => request_token.secret}

  if request_token.callback_confirmed?
    redirect request_token.authorize_url(options[:authorize_params])
  else
    redirect request_token.authorize_url(options[:authorize_params].merge(:oauth_callback => callback_url))
  end

rescue ::Timeout::Error => e
  fail!(:timeout, e)
rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
  fail!(:service_unavailable, e)
end

#unique_idObject



84
85
86
# File 'lib/omniauth/strategies/oauth.rb', line 84

def unique_id
  nil
end