Class: OmniAuth::Strategies::XAuth

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

Direct Known Subclasses

Instapaper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of XAuth.



12
13
14
15
16
17
# File 'lib/omniauth/strategies/xauth.rb', line 12

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
end

Instance Attribute Details

#consumer_keyObject

Returns the value of attribute consumer_key.



8
9
10
# File 'lib/omniauth/strategies/xauth.rb', line 8

def consumer_key
  @consumer_key
end

#consumer_optionsObject

Returns the value of attribute consumer_options.



8
9
10
# File 'lib/omniauth/strategies/xauth.rb', line 8

def consumer_options
  @consumer_options
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



8
9
10
# File 'lib/omniauth/strategies/xauth.rb', line 8

def consumer_secret
  @consumer_secret
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/omniauth/strategies/xauth.rb', line 7

def name
  @name
end

Instance Method Details

#auth_hashObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/omniauth/strategies/xauth.rb', line 53

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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/omniauth/strategies/xauth.rb', line 40

def callback_phase
  @access_token = consumer.get_access_token(nil, {}, session['omniauth.xauth'])
  super
  rescue ::Net::HTTPFatalError => e
    fail!(:service_unavailable, e)
  rescue ::OAuth::Unauthorized => e
    fail!(:invalid_credentials, e)
  rescue ::MultiJson::DecodeError => e
    fail!(:invalid_response, e)
ensure
  session['omniauth.xauth'] = nil
end

#consumerObject



36
37
38
# File 'lib/omniauth/strategies/xauth.rb', line 36

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

#get_credentialsObject



29
30
31
32
33
34
# File 'lib/omniauth/strategies/xauth.rb', line 29

def get_credentials
  OmniAuth::Form.build(consumer_options[:title] || "xAuth Credentials") do
    text_field 'Username', 'username'
    password_field 'Password', 'password'
  end.to_response
end

#request_phaseObject



19
20
21
22
23
24
25
26
27
# File 'lib/omniauth/strategies/xauth.rb', line 19

def request_phase
  session['oauth'] ||= {}
  if env['REQUEST_METHOD'] == 'GET'
    get_credentials
  else
    session['omniauth.xauth'] = { 'x_auth_mode' => 'client_auth', 'x_auth_username' => request['username'], 'x_auth_password' => request['password'] }
    redirect callback_path
  end
end