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.
- data/Gemfile.lock +1 -1
- data/VERSION +1 -1
- data/lib/sinatra/omniauth.rb +163 -145
- data/sinatra_omniauth.gemspec +2 -2
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/lib/sinatra/omniauth.rb
CHANGED
@@ -123,181 +123,199 @@ module SinatraOmniAuth
|
|
123
123
|
end
|
124
124
|
|
125
125
|
def self.registered app
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
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
|
-
|
143
|
-
|
143
|
+
# Make _method=delete work in POST requests:
|
144
|
+
enable :method_override
|
144
145
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
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
|
-
|
152
|
-
|
152
|
+
# A little help from our friends...
|
153
|
+
include Helpers
|
153
154
|
|
154
|
-
|
155
|
-
|
156
|
-
|
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
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
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
|
-
|
171
|
-
|
171
|
+
haml :auth
|
172
|
+
end
|
172
173
|
|
173
|
-
|
174
|
-
|
175
|
-
|
174
|
+
get '/auth/:authentication/callback' do
|
175
|
+
callback
|
176
|
+
end
|
176
177
|
|
177
|
-
|
178
|
-
|
179
|
-
|
178
|
+
post '/auth/:authentication/callback' do
|
179
|
+
callback
|
180
|
+
end
|
180
181
|
|
181
|
-
|
182
|
-
|
183
|
-
|
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
|
-
|
186
|
-
|
186
|
+
# get the authentication parameter from the Rails router
|
187
|
+
authentication_route = params[:authentication] ? params[:authentication] : 'No authentication recognized (invalid callback)'
|
187
188
|
|
188
|
-
|
189
|
-
|
189
|
+
# get the full hash from omniauth
|
190
|
+
omniauth = request.env['omniauth.auth']
|
190
191
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
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
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
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
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
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
|
-
|
232
|
+
auth = Authentication.first(:provider => @authhash[:provider], :uid => @authhash[:uid])
|
232
233
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
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
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
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
|
-
|
255
|
-
|
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
|
-
|
259
|
-
|
260
|
-
|
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
|
-
|
269
|
-
|
270
|
-
redirect to('/')
|
271
|
-
end
|
291
|
+
get '/auth/signout' do
|
292
|
+
authenticate_user!
|
272
293
|
|
273
|
-
|
274
|
-
|
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
|
-
|
277
|
-
|
278
|
-
|
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
|
-
|
284
|
-
|
285
|
-
|
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
|
-
|
288
|
-
|
289
|
-
|
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
|
-
|
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
|
data/sinatra_omniauth.gemspec
CHANGED
@@ -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
|
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-
|
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:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
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-
|
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
|