Class: Rack::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-oauth.rb

Overview

Rack Middleware for integrating OAuth into your application

Note: this requires that a Rack::Session middleware be enabled

Defined Under Namespace

Modules: Methods

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :login_path    => '/oauth_login',
  :callback_path => '/oauth_callback',
  :redirect_to   => '/oauth_complete',
  :rack_session  => 'rack.session'
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, *args) ⇒ OAuth

Returns a new instance of OAuth.



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rack-oauth.rb', line 167

def initialize app, *args
  @app = app

  options = args.pop
  @name   = args.first || Rack::OAuth.default_instance_name
  
  DEFAULT_OPTIONS.each {|name, value| send "#{name}=", value }
  options.each         {|name, value| send "#{name}=", value } if options

  raise_validation_exception unless valid?
end

Class Attribute Details

.default_instance_nameObject

The name we use for Rack::OAuth instances when a name is not given.

This is ‘default’ by default



84
85
86
# File 'lib/rack-oauth.rb', line 84

def default_instance_name
  @default_instance_name
end

.test_mode_enabledObject

Set this equal to true to enable ‘test mode’



87
88
89
# File 'lib/rack-oauth.rb', line 87

def test_mode_enabled
  @test_mode_enabled
end

Instance Attribute Details

#authenticationObject

for using “Sign in w/twitter”



158
159
160
# File 'lib/rack-oauth.rb', line 158

def authentication
  @authentication
end

#callback_pathObject Also known as: callback

the URL that the OAuth provider should callback to after OAuth login is complete



122
123
124
# File 'lib/rack-oauth.rb', line 122

def callback_path
  ::File.join *[@callback_path.to_s, name_unless_default].compact
end

#consumer_keyObject Also known as: key

required

Your OAuth consumer key



143
144
145
# File 'lib/rack-oauth.rb', line 143

def consumer_key
  @consumer_key
end

#consumer_secretObject Also known as: secret

required

Your OAuth consumer secret



148
149
150
# File 'lib/rack-oauth.rb', line 148

def consumer_secret
  @consumer_secret
end

#consumer_siteObject Also known as: site

required

The site you want to request OAuth for, eg. ‘twitter.com



153
154
155
# File 'lib/rack-oauth.rb', line 153

def consumer_site
  @consumer_site
end

#login_pathObject Also known as: login

the URL that should initiate OAuth and redirect to the OAuth provider’s login page



114
115
116
# File 'lib/rack-oauth.rb', line 114

def 
  ::File.join *[@login_path.to_s, name_unless_default].compact
end

#nameObject

an arbitrary name for this instance of Rack::OAuth



161
162
163
# File 'lib/rack-oauth.rb', line 161

def name
  @name.to_s
end

#rack_sessionObject

the name of the Rack env variable used for the session



140
141
142
# File 'lib/rack-oauth.rb', line 140

def rack_session
  @rack_session
end

#redirect_toObject Also known as: redirect

the URL that Rack::OAuth should redirect to after the OAuth has been completed (part of your app)



135
136
137
# File 'lib/rack-oauth.rb', line 135

def redirect_to
  @redirect_to
end

Class Method Details

.all(env) ⇒ Object

Returns all of the Rack::OAuth instances found in this Rack ‘env’ Hash



96
97
98
# File 'lib/rack-oauth.rb', line 96

def self.all env
  env['rack.oauth']
end

.disable_test_modeObject



89
# File 'lib/rack-oauth.rb', line 89

def disable_test_mode() self.test_mode_enabled =  false end

.enable_test_modeObject



88
# File 'lib/rack-oauth.rb', line 88

def enable_test_mode()  self.test_mode_enabled =  true  end

.get(env, name = nil) ⇒ Object

Simple helper to get an instance of Rack::OAuth by name found in this Rack ‘env’ Hash



101
102
103
104
# File 'lib/rack-oauth.rb', line 101

def self.get env, name = nil
  name = Rack::OAuth.default_instance_name if name.nil?
  all(env)[name.to_s]
end

.test_mode?Boolean

Returns:

  • (Boolean)


90
# File 'lib/rack-oauth.rb', line 90

def test_mode?()             test_mode_enabled == true  end

Instance Method Details

#call(env) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rack-oauth.rb', line 179

def call env
  # put this instance of Rack::OAuth in the env 
  # so it's accessible from the application
  env['rack.oauth'] ||= {}
  env['rack.oauth'][name] = self

  case env['PATH_INFO']
  
  # find out where to redirect to authorize for this oauth provider 
  # and redirect there.  when the authorization is finished, 
  # the provider will redirect back to our application's callback path
  when 
    (env)
  # the oauth provider has redirected back to us!  we should have a 
  # verifier now that we can use, in combination with out token and 
  # secret, to get an access token for this user
  when callback_path
    do_callback(env)
  else
    @app.call(env)
  end
end

#consumerObject



253
254
255
# File 'lib/rack-oauth.rb', line 253

def consumer
  @consumer ||= ::OAuth::Consumer.new consumer_key, consumer_secret, :site => consumer_site
end

#do_callback(env) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rack-oauth.rb', line 219

def do_callback env
  # get access token and persist it in the session in a way that we can get it back out later
  request = ::OAuth::RequestToken.new consumer, session(env)[:token], session(env)[:secret]
  set_access_token env, request.get_access_token(:oauth_verifier => Rack::Request.new(env).params['oauth_verifier'])

  # clear out the session variables (won't need these anymore)
  session(env).delete(:token)
  session(env).delete(:secret)

  # we have an access token now ... redirect back to the user's application
  [ 302, { 'Content-Type' => 'text/html', 'Location' => redirect_path }, [] ]
end

#do_login(env) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rack-oauth.rb', line 202

def  env
  if Rack::OAuth.test_mode?
    set_access_token env, OpenStruct.new(:params => { 'I am a' => 'fake token' })
    return [ 302, { 'Content-Type' => 'text/html', 'Location' => redirect_to }, [] ]
  end

  # get request token and hold onto the token/secret (which we need later to get the access token)
  request = consumer.get_request_token :oauth_callback => ::File.join("http://#{ env['HTTP_HOST'] }", callback_path)
  session(env)[:token]  = request.token
  session(env)[:secret] = request.secret

  # redirect to the oauth provider's authorize url to authorize the user
  path = request.authorize_url
  path.gsub!('authorize', 'authenticate') if @authentication
  [ 302, { 'Content-Type' => 'text/html', 'Location' => path }, [] ]
end

#get_access_token(env) ⇒ Object

See #set_access_token



238
239
240
241
# File 'lib/rack-oauth.rb', line 238

def get_access_token env
  params = session(env)[:access_token_params]
  ::OAuth::AccessToken.from_hash consumer, params if params
end

#get_access_token!(env) ⇒ Object

Same as #get_access_token but it clears the access token info out of the session



244
245
246
247
# File 'lib/rack-oauth.rb', line 244

def get_access_token! env
  params = session(env).delete(:access_token_params)
  ::OAuth::AccessToken.from_hash consumer, params if params
end

#name_unless_defaultObject

Returns the #name of this Rack::OAuth unless the name is ‘default’, in which case it returns nil



287
288
289
# File 'lib/rack-oauth.rb', line 287

def name_unless_default
  name == Rack::OAuth.default_instance_name ? nil : name
end

#raise_validation_exceptionObject



265
266
267
# File 'lib/rack-oauth.rb', line 265

def raise_validation_exception
  raise @errors.join(', ')
end

#redirect_pathObject



130
131
132
# File 'lib/rack-oauth.rb', line 130

def redirect_path
  ::File.join *[@redirect_to.to_s, name_unless_default].compact
end

#session(env) ⇒ Object

Returns a hash of session variables, specific to this instance of Rack::OAuth and the end-user

All user-specific variables are stored in the session.

The variables we currently keep track of are:

  • token

  • secret

  • verifier

With all three of these, we can make arbitrary requests to our OAuth provider for this user.



279
280
281
282
283
284
# File 'lib/rack-oauth.rb', line 279

def session env
  raise "Rack env['rack.session'] is nil ... has a Rack::Session middleware be enabled?  " + 
        "use :rack_session for custom key" if env[rack_session].nil?      
  env[rack_session]['rack.oauth']       ||= {}
  env[rack_session]['rack.oauth'][name] ||= {}
end

#set_access_token(env, token) ⇒ Object

Stores the access token in this env’s session in a way that we can get it back out via #get_access_token(env)



233
234
235
# File 'lib/rack-oauth.rb', line 233

def set_access_token env, token
  session(env)[:access_token_params] = token.params
end

#valid?Boolean

Returns:

  • (Boolean)


257
258
259
260
261
262
263
# File 'lib/rack-oauth.rb', line 257

def valid?
  @errors = []
  @errors << ":consumer_key option is required"    unless consumer_key
  @errors << ":consumer_secret option is required" unless consumer_secret
  @errors << ":consumer_site option is required"   unless consumer_site
  @errors.empty?
end

#verified?(env) ⇒ Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/rack-oauth.rb', line 249

def verified? env
  [ :token, :secret, :verifier ].all? { |required_session_key| session(env)[required_session_key] }
end