sinatra_omniauth 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- addressable (2.2.6)
4
+ addressable (2.2.4)
5
5
  columnize (0.3.4)
6
6
  data_objects (0.10.6)
7
7
  addressable (~> 2.1)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -123,181 +123,199 @@ module SinatraOmniAuth
123
123
  end
124
124
 
125
125
  def self.registered app
126
- # Register OmniAuth Strategies and keys for all providers:
127
- app.use ::OmniAuth::Builder do
128
- app.settings.omniauth.each do |a|
129
- provider = a['provider']
130
- client_options = a[:client_options]
131
- client_options = client_options ? {:client_options => client_options} : {}
132
- if key = a['key']
133
- provider provider, key, a['secret'], client_options
134
- else
135
- name = a['name'].downcase.gsub(/ /,' ')
136
- store = OpenID::Store::Filesystem.new(a['store']||'./tmp')
137
- provider provider, store, :name => name, :identifier => a['identifier']
126
+ app.class_eval do
127
+ # Register OmniAuth Strategies and keys for all providers:
128
+ use ::OmniAuth::Builder do
129
+ app.settings.omniauth.each do |a|
130
+ provider = a['provider']
131
+ client_options = a[:client_options]
132
+ client_options = client_options ? {:client_options => client_options} : {}
133
+ if key = a['key']
134
+ provider provider, key, a['secret'], client_options
135
+ else
136
+ name = a['name'].downcase.gsub(/ /,' ')
137
+ store = OpenID::Store::Filesystem.new(a['store']||'./tmp')
138
+ provider provider, store, :name => name, :identifier => a['identifier']
139
+ end
138
140
  end
139
141
  end
140
- end
141
142
 
142
- # Make _method=delete work in POST requests:
143
- app.enable :method_override
143
+ # Make _method=delete work in POST requests:
144
+ enable :method_override
144
145
 
145
- # Create a flash, so we can display a message after a redirect
146
- app.use Rack::Flash, :accessorize => [:notice, :error]
147
- app.send(:define_method, :flash) do
148
- env['x-rack.flash']
149
- end
146
+ # Create a flash, so we can display a message after a redirect
147
+ use Rack::Flash, :accessorize => [:notice, :error]
148
+ send(:define_method, :flash) do
149
+ env['x-rack.flash']
150
+ end
150
151
 
151
- # A little help from our friends...
152
- app.send(:include, Helpers)
152
+ # A little help from our friends...
153
+ include Helpers
153
154
 
154
- # Display the authentication in use, registered for the current user, and available
155
- app.get '/auth' do
156
- @authentications_possible = settings.omniauth
155
+ # Display the authentication in use, registered for the current user, and available
156
+ get '/auth' do
157
+ @authentications_possible = settings.omniauth
157
158
 
158
- if current_user and @authentication_current = current_auth
159
- @authentications_available = current_user.authentications.all(:order => [ :provider.desc ])
160
- @authentications_unused = @authentications_available.
161
- reject do|a|
162
- a.provider == @authentication_current.provider
163
- end
164
- @authentications_possible = @authentications_possible.dup.
165
- reject do |a|
166
- @authentications_available.detect{|p| p.provider.gsub(/[ _]/,'') == a['name'].downcase.gsub(/[ _]/,'') }
167
- end
168
- end
159
+ if current_user and @authentication_current = current_auth
160
+ @authentications_available = current_user.authentications.all(:order => [ :provider.desc ])
161
+ @authentications_unused = @authentications_available.
162
+ reject do|a|
163
+ a.provider == @authentication_current.provider
164
+ end
165
+ @authentications_possible = @authentications_possible.dup.
166
+ reject do |a|
167
+ @authentications_available.detect{|p| p.provider.gsub(/[ _]/,'') == a['name'].downcase.gsub(/[ _]/,'') }
168
+ end
169
+ end
169
170
 
170
- haml :auth
171
- end
171
+ haml :auth
172
+ end
172
173
 
173
- app.get '/auth/:authentication/callback' do
174
- callback
175
- end
174
+ get '/auth/:authentication/callback' do
175
+ callback
176
+ end
176
177
 
177
- app.post '/auth/:authentication/callback' do
178
- callback
179
- end
178
+ post '/auth/:authentication/callback' do
179
+ callback
180
+ end
180
181
 
181
- app.send(:define_method, :callback) do
182
- # callback: success
183
- # This handles signing in and adding an authentication authentication to existing accounts itself
182
+ send(:define_method, :callback) do
183
+ # callback: success
184
+ # This handles signing in and adding an authentication authentication to existing accounts itself
184
185
 
185
- # get the authentication parameter from the Rails router
186
- authentication_route = params[:authentication] ? params[:authentication] : 'No authentication recognized (invalid callback)'
186
+ # get the authentication parameter from the Rails router
187
+ authentication_route = params[:authentication] ? params[:authentication] : 'No authentication recognized (invalid callback)'
187
188
 
188
- # get the full hash from omniauth
189
- omniauth = request.env['omniauth.auth']
189
+ # get the full hash from omniauth
190
+ omniauth = request.env['omniauth.auth']
190
191
 
191
- # continue only if hash and parameter exist
192
- unless omniauth and params[:authentication]
193
- flash.error = 'Error while authenticating via ' + authentication_route.capitalize + '. The authentication did not return valid data.'
194
- redirect to('/signin')
195
- end
192
+ # continue only if hash and parameter exist
193
+ unless omniauth and params[:authentication]
194
+ flash.error = 'Error while authenticating via ' + authentication_route.capitalize + '. The authentication did not return valid data.'
195
+ redirect to('/signin')
196
+ end
196
197
 
197
- # create a new regularised authentication hash
198
- @authhash = Hash.new
199
- oaeuh = omniauth['extra'] && omniauth['extra']['user_hash']
200
- oaui = omniauth['user_info']
201
- if authentication_route == 'facebook'
202
- @authhash[:email] = oaeuh['email'] || ''
203
- @authhash[:name] = oaeuh['name'] || ''
204
- @authhash[:uid] = oaeuh['name'] || ''
205
- @authhash[:provider] = omniauth['provider'] || ''
206
- elsif authentication_route == 'github'
207
- @authhash[:email] = oaui['email'] || ''
208
- @authhash[:name] = oaui['name'] || ''
209
- @authhash[:uid] = (oaeuh['id'] || '').to_s
210
- @authhash[:provider] = omniauth['provider'] || ''
211
- elsif ['google', 'yahoo', 'linked_in', 'twitter', 'myopenid', 'openid', 'open_id'].index(authentication_route) != nil
212
- @authhash[:email] = oaui['email'] || ''
213
- @authhash[:name] = oaui['name'] || ''
214
- @authhash[:uid] = (omniauth['uid'] || '').to_s
215
- @authhash[:provider] = omniauth['provider'] || ''
216
- elsif authentication_route == 'aol'
217
- @authhash[:email] = oaui['email'] || ''
218
- @authhash[:name] = oaui['name'] || ''
219
- @authhash[:uid] = (omniauth['uid'] || '').to_s
220
- @authhash[:provider] = omniauth['provider'] || ''
221
- else
222
- # REVISIT: debug to output the hash that has been returned when adding new authentications
223
- return '<pre>'+omniauth.to_yaml+'</pre>'
224
- end
198
+ # create a new regularised authentication hash
199
+ @authhash = Hash.new
200
+ oaeuh = omniauth['extra'] && omniauth['extra']['user_hash']
201
+ oaui = omniauth['user_info']
202
+ if authentication_route == 'facebook'
203
+ @authhash[:email] = oaeuh['email'] || ''
204
+ @authhash[:name] = oaeuh['name'] || ''
205
+ @authhash[:uid] = oaeuh['name'] || ''
206
+ @authhash[:provider] = omniauth['provider'] || ''
207
+ elsif authentication_route == 'github'
208
+ @authhash[:email] = oaui['email'] || ''
209
+ @authhash[:name] = oaui['name'] || ''
210
+ @authhash[:uid] = (oaeuh['id'] || '').to_s
211
+ @authhash[:provider] = omniauth['provider'] || ''
212
+ elsif ['google', 'yahoo', 'linked_in', 'twitter', 'myopenid', 'openid', 'open_id'].index(authentication_route) != nil
213
+ @authhash[:email] = oaui['email'] || ''
214
+ @authhash[:name] = oaui['name'] || ''
215
+ @authhash[:uid] = (omniauth['uid'] || '').to_s
216
+ @authhash[:provider] = omniauth['provider'] || ''
217
+ elsif authentication_route == 'aol'
218
+ @authhash[:email] = oaui['email'] || ''
219
+ @authhash[:name] = oaui['name'] || ''
220
+ @authhash[:uid] = (omniauth['uid'] || '').to_s
221
+ @authhash[:provider] = omniauth['provider'] || ''
222
+ else
223
+ # REVISIT: debug to output the hash that has been returned when adding new authentications
224
+ return '<pre>'+omniauth.to_yaml+'</pre>'
225
+ end
225
226
 
226
- if @authhash[:uid] == '' or @authhash[:provider] == ''
227
- flash.error = 'Error while authenticating via ' + authentication_route + '/' + @authhash[:provider].capitalize + '. The authentication returned invalid data for the user id.'
228
- redirect to('/auth')
229
- end
227
+ if @authhash[:uid] == '' or @authhash[:provider] == ''
228
+ flash.error = 'Error while authenticating via ' + authentication_route + '/' + @authhash[:provider].capitalize + '. The authentication returned invalid data for the user id.'
229
+ redirect to('/auth')
230
+ end
230
231
 
231
- auth = Authentication.first(:provider => @authhash[:provider], :uid => @authhash[:uid])
232
+ auth = Authentication.first(:provider => @authhash[:provider], :uid => @authhash[:uid])
232
233
 
233
- # if the user is currently signed in, he/she might want to add another account to signin
234
- if current_user
235
- if auth
236
- flash.notice = 'You are now signed in using your' + @authhash[:provider].capitalize + ' account'
237
- session[:authentication_provider] = auth.provider # They're now signed in using the new account
238
- redirect to('/auth/signedin') # Already signed in, and we already had this authentication
234
+ # if the user is currently signed in, he/she might want to add another account to signin
235
+ if current_user
236
+ if auth
237
+ flash.notice = 'You are now signed in using your' + @authhash[:provider].capitalize + ' account'
238
+ session[:authentication_provider] = auth.provider # They're now signed in using the new account
239
+ redirect to('/auth/signedin') # Already signed in, and we already had this authentication
240
+ else
241
+ auth = current_user.authentications.create!(:provider => @authhash[:provider], :uid => @authhash[:uid], :user_name => @authhash[:name], :user_email => @authhash[:email])
242
+ flash.notice = 'Your ' + @authhash[:provider].capitalize + ' account has been added for signing in at this site.'
243
+ session[:authentication_provider] = auth.provider # They're now signed in using the new account
244
+ session[:user_name] = @authhash[:name] if @authhash[:name] != ''
245
+ redirect to('/auth/signedin')
246
+ end
239
247
  else
240
- auth = current_user.authentications.create!(:provider => @authhash[:provider], :uid => @authhash[:uid], :user_name => @authhash[:name], :user_email => @authhash[:email])
241
- flash.notice = 'Your ' + @authhash[:provider].capitalize + ' account has been added for signing in at this site.'
242
- session[:authentication_provider] = auth.provider # They're now signed in using the new account
243
- session[:user_name] = @authhash[:name] if @authhash[:name] != ''
244
- redirect to('/auth/signedin')
245
- end
246
- else
247
- if auth
248
- # Signin existing user
249
- # in the session his user id and the authentication id used for signing in is stored
250
- session[:user_id] = auth.user.id
251
- session[:authentication_provider] = auth.provider # They're now signed in using the new account
252
- session[:user_name] = @authhash[:name] if @authhash[:name] != ''
248
+ if auth
249
+ # Signin existing user
250
+ # in the session his user id and the authentication id used for signing in is stored
251
+ session[:user_id] = auth.user.id
252
+ session[:authentication_provider] = auth.provider # They're now signed in using the new account
253
+ session[:user_name] = @authhash[:name] if @authhash[:name] != ''
254
+
255
+ flash.notice = 'Signed in successfully via ' + @authhash[:provider].capitalize + '.'
256
+ redirect to('/auth/signedin')
257
+ end
258
+
259
+ if email = @authhash[:email] and email != '' and
260
+ auth = Authentication.first(:email => email)
261
+ # Would have been seen as a new user, but instead we found that we know their email address already
262
+ provider = @authhash[:provider]
263
+ auth = auth.user.authentications.create!(
264
+ :provider => provider,
265
+ :uid => @authhash[:uid],
266
+ :user_name => @authhash[:name],
267
+ :user_email => @authhash[:email]
268
+ )
269
+ flash.notice = 'Your ' + provider.capitalize + ' account has been added for signing in at this site.'
270
+ session[:user_id] = auth.user.id
271
+ session[:authentication_provider] = auth.provider # They're now signed in using the new account
272
+ session[:user_name] = @authhash[:name] if @authhash[:name] != ''
273
+ redirect to('/auth/signedin')
274
+ end
253
275
 
254
- flash.notice = 'Signed in successfully via ' + @authhash[:provider].capitalize + '.'
255
- redirect to('/auth/signedin')
276
+ # this is a new user; add them
277
+ @current_user = User.create()
278
+ session[:user_id] = @current_user.id
279
+ session[:user_name] = @authhash[:name] if @authhash[:name] != ''
280
+ auth = current_user.authentications.create!(:provider => @authhash[:provider], :uid => @authhash[:uid], :user_name => @authhash[:name], :user_email => @authhash[:email])
281
+ session[:authentication_provider] = auth.provider
282
+ redirect to('/auth/welcome')
256
283
  end
284
+ end
257
285
 
258
- # this is a new user; add them
259
- @current_user = User.create()
260
- session[:user_id] = @current_user.id
261
- session[:user_name] = @authhash[:name] if @authhash[:name] != ''
262
- auth = current_user.authentications.create!(:provider => @authhash[:provider], :uid => @authhash[:uid], :user_name => @authhash[:name], :user_email => @authhash[:email])
263
- session[:authentication_provider] = auth.provider
264
- redirect to('/auth/welcome')
286
+ get '/auth/failure' do
287
+ flash.error = 'There was an error at the remote authentication authentication. You have not been signed in.'
288
+ redirect to('/')
265
289
  end
266
- end
267
290
 
268
- app.get '/auth/failure' do
269
- flash.error = 'There was an error at the remote authentication authentication. You have not been signed in.'
270
- redirect to('/')
271
- end
291
+ get '/auth/signout' do
292
+ authenticate_user!
272
293
 
273
- app.get '/auth/signout' do
274
- authenticate_user!
294
+ session.delete :user_id
295
+ session.delete :user_name
296
+ session.delete :authentication_provider
297
+ flash.notice = 'You have been signed out'
298
+ redirect to('/')
299
+ end
275
300
 
276
- session.delete :user_id
277
- session.delete :user_name
278
- session.delete :authentication_provider
279
- flash.notice = 'You have been signed out'
280
- redirect to('/')
281
- end
301
+ # authentication
302
+ delete '/auth/:provider' do
303
+ authenticate_user!
282
304
 
283
- # authentication
284
- app.delete '/auth/:provider' do
285
- authenticate_user!
305
+ # remove an authentication authentication linked to the current user
306
+ provider = params[:provider]
307
+ @authentication = current_user.authentications.first(:provider => provider)
286
308
 
287
- # remove an authentication authentication linked to the current user
288
- provider = params[:provider]
289
- @authentication = current_user.authentications.first(:provider => provider)
309
+ if !@authentication
310
+ pass
311
+ elsif session[:authentication_provider] == @authentication.provider
312
+ flash.error = 'You can\'t delete your authorization through #{provider.capitalize} because you are currently signed in with it!'
313
+ else
314
+ @authentication.destroy
315
+ end
290
316
 
291
- if !@authentication
292
- pass
293
- elsif session[:authentication_provider] == @authentication.provider
294
- flash.error = 'You can\'t delete your authorization through #{provider.capitalize} because you are currently signed in with it!'
295
- else
296
- @authentication.destroy
317
+ redirect to('/auth')
297
318
  end
298
-
299
- redirect to('/auth')
300
319
  end
301
-
302
320
  end
303
321
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra_omniauth}
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Clifford Heath}]
12
- s.date = %q{2011-08-17}
12
+ s.date = %q{2011-08-18}
13
13
  s.description = %q{This Sinatra extension, derived from omniauth_pure by Marcus Proske, adds OmniAuth authorization to your Sinatra application, so your users can login using FaceBook, Twitter and many other authorization providers, as long as you supply the API keys. It uses DataMapper and HAML.}
14
14
  s.email = %q{clifford.heath@gmail.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_omniauth
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
9
+ - 0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Clifford Heath
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-17 00:00:00 Z
18
+ date: 2011-08-18 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  version_requirements: &id001 !ruby/object:Gem::Requirement