Class: Warden::OAuth::Strategy

Inherits:
Strategies::Base
  • Object
show all
Extended by:
StrategyBuilder
Defined in:
lib/warden_oauth/strategy.rb

Overview

Holds all the main logic of the OAuth authentication, all the generated OAuth classes will extend from this class

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StrategyBuilder

access_token_user_finder, build

Class Method Details

.access_token_user_findersObject



16
17
18
# File 'lib/warden_oauth/strategy.rb', line 16

def self.access_token_user_finders
  (@_user_token_finders ||= {})
end

Instance Method Details

#access_tokenObject



85
86
87
# File 'lib/warden_oauth/strategy.rb', line 85

def access_token
  @access_token ||= request_token.get_access_token(:oauth_verifier => params['oauth_verifier'])
end

#authenticate!Object

Note:

Manages the OAuth authentication process, there can be 3 outcomes from this Strategy:

  1. The OAuth credentials are invalid and the FailureApp is called

  2. The OAuth credentials are valid, but there is no user associated to them. In this case the FailureApp is called, but the env[:oauth] will be available.

  3. The OAuth credentials are valid, and the user is authenticated successfuly

If you want to signup users with the twitter credentials, you can manage the creation of a new user in the FailureApp with the given access_token



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/warden_oauth/strategy.rb', line 43

def authenticate!
  if params.include?('warden_oauth_provider')
    store_request_token_on_session
    redirect!(request_token.authorize_url)
    throw(:warden)
  elsif params.include?('oauth_token')
    load_request_token_from_session
    if missing_stored_token?
      fail!("There is no OAuth authentication in progress")
    elsif !stored_token_match_recieved_token?
      fail!("Received OAuth token didn't match stored OAuth token")
    else
      user = find_user_by_access_token(access_token)
      if user.nil?
        fail!("User with access token not found")
        throw_error_with_oauth_info
      else
        success!(user)
      end
    end
  end

end

#consumerObject

OAuth Logic ###



76
77
78
# File 'lib/warden_oauth/strategy.rb', line 76

def consumer
  @consumer ||= ::OAuth::Consumer.new(config.consumer_key, config.consumer_secret, config.options)
end

#fail!(msg) ⇒ Object

:nodoc:



67
68
69
70
# File 'lib/warden_oauth/strategy.rb', line 67

def fail!(msg) #:nodoc:
  self.errors.add(service_param_name.to_sym, msg)
  super
end

#request_tokenObject



80
81
82
83
# File 'lib/warden_oauth/strategy.rb', line 80

def request_token
  host_with_port = Warden::OAuth::Utils.host_with_port(request)
  @request_token ||= consumer.get_request_token(:oauth_callback => host_with_port)
end

#valid?Boolean

An OAuth strategy will be valid to execute if:

  • A ‘warden_oauth_provider’ parameter is given, with the name of the OAuth service

  • A ‘oauth_token’ is being receive on the request (response from an OAuth provider)

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/warden_oauth/strategy.rb', line 25

def valid?
  (params.include?('warden_oauth_provider') &&  params['warden_oauth_provider'] == config.provider_name.to_s) ||
    params.include?('oauth_token') 
end