uber_login 1.0.3 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGE2Y2E2MzNjYTA2ODMxZDYwNmU0MGFlNjM2MmVkNTkxOTJkMWU3Mw==
4
+ MmY5ZDRiZGZiNmViZTAyNzQwNDRjYWFlMjQyNjA4MzkxZjYyYjc0YQ==
5
5
  data.tar.gz: !binary |-
6
- NDAzMDg0NGU2MmMyNDBkZDY4YmNhY2ZjYWEwODUwNjBjMjY2NjhmOA==
6
+ Y2Y0ZWEyMzhmZWQwMzQ2ZGIxY2MzZWFlMWY5YTgzZTIyMDc2YzNkMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTZjYTBkMzRhZTIyOGFlM2M2NjA0YjE3NmFjOGU1MzdkNzcxM2Q2OTMxYmU2
10
- NmI3NjcwMGI5ZGE0NWMxN2U0ZDA5MGY5OWQxYzQ1Y2UxMWViNDE3MGNmNWEw
11
- ZjE5ZDM3ZTZlZTliZTUzMjc3YTljMzdhMDk2NTc4Zjk4NTVmMDQ=
9
+ YzQ3ZWFlMmNhNGIzZmY0N2M4ZmI2NzY2Yzk3NDg5ZmUyNGI0MzY1YjUyZjI4
10
+ NThhMjM3ZDI0ZTkyY2Q0ZDEzZmZhYjA0MzY3NTg4OGU2YzM3NDc4ZjMxNTJi
11
+ YjgwZTI3YWQ0Y2UzMDc4ZTNiMDYwM2Y2ZjBmNDYzNjA5NDYxNzQ=
12
12
  data.tar.gz: !binary |-
13
- ZDNmNjI3NWUyYWJiYWJiYTNjYThiZmM1YmFjNDMwYjljMDVhMGNmMmY1MGU1
14
- YzdjZTYyYTlmNTZjY2NjY2Q3MGU1ZmVlMTQ0MDY4MDI5YmY4OWRmZGU2NzE1
15
- OWI5ZjZiNGRhZGY0ZWFhZTE5MzI2ZWQwZDk5MGYzYzcxYzAyZGU=
13
+ MzBlM2Y3ZTMzYjcwYmQ5YjgwZjExOGVjODA4NjFlYjU3MWE4MDcyNDNiOTU3
14
+ ZTNmYTI2YWY4MjIyNTdhYWNlYThiZTk4MjI1MjYwNTU1YmIzNDZlMWVkYzli
15
+ OWJjODFjMDUxZWFmM2EyYjIxMDJiZDA1ZDkwNmJiNjNhMzc3ZTM=
@@ -1,3 +1,3 @@
1
1
  module UberLogin
2
- VERSION = '1.0.3'
2
+ VERSION = '2.1.0'
3
3
  end
data/lib/uber_login.rb CHANGED
@@ -5,8 +5,16 @@ require 'uber_login/session_manager'
5
5
  require 'securerandom'
6
6
  require 'bcrypt'
7
7
  require 'user_agent'
8
+ require 'active_support'
8
9
 
9
10
  module UberLogin
11
+ include ActiveSupport::Callbacks
12
+ extend ActiveSupport::Concern
13
+
14
+ included do
15
+ define_callbacks :login, :logout
16
+ end
17
+
10
18
  ##
11
19
  # Returns the logged in user.
12
20
  # If session[+:uid+] is set:
@@ -34,14 +42,17 @@ module UberLogin
34
42
  def login(user, remember = false)
35
43
  logout_all unless UberLogin.configuration.allow_multiple_login
36
44
 
37
- if strong_sessions or remember
38
- composite = generate_and_save_token(user.id)
39
- cookie_manager.persistent_login(user.id, composite) if remember
40
- else
41
- composite = nil
42
- end
45
+ run_callbacks :login do
46
+ if strong_sessions or remember
47
+ composite = generate_and_save_token(user.id)
48
+ cookie_manager.persistent_login(user.id, composite) if remember
49
+ else
50
+ composite = nil
51
+ end
43
52
 
44
- session_manager.login(user.id, composite)
53
+ reset_session
54
+ session_manager.login(user.id, composite)
55
+ end
45
56
  end
46
57
 
47
58
  ##
@@ -49,12 +60,14 @@ module UberLogin
49
60
  # and corresponding token removed from the database.
50
61
  # If sequence is not nil it only removes the sequence and token from the database.
51
62
  def logout(sequence = nil)
52
- if sequence.nil? or sequence == current_sequence
53
- delete_from_database if cookies[:uid] or strong_sessions
54
- session_manager.clear
55
- cookie_manager.clear
56
- else
57
- delete_from_database(sequence)
63
+ run_callbacks :logout do
64
+ if sequence.nil? or sequence == current_sequence
65
+ delete_from_database if cookies[:uid] or strong_sessions
66
+ session_manager.clear
67
+ cookie_manager.clear
68
+ else
69
+ delete_from_database(sequence)
70
+ end
58
71
  end
59
72
  end
60
73
 
@@ -67,6 +80,10 @@ module UberLogin
67
80
  cookie_manager.clear
68
81
  end
69
82
 
83
+ def persistent_login?
84
+ cookie_manager.valid?
85
+ end
86
+
70
87
  private
71
88
  def cookie_manager
72
89
  @cookie_manager ||= CookieManager.new(cookies, request)
@@ -78,8 +95,8 @@ module UberLogin
78
95
 
79
96
  # See +current_user+
80
97
  def current_user_uncached
81
- if session[:uid]
82
- logout if strong_sessions and !session_manager.valid?
98
+ if session[:uid] and strong_sessions
99
+ logout unless session_manager.valid?
83
100
  else
84
101
  login_from_cookies if cookie_manager.login_cookies?
85
102
  end
@@ -90,11 +107,14 @@ module UberLogin
90
107
  ##
91
108
  # Attempts a login from the +:uid+ and +:ulogin+ cookies.
92
109
  def login_from_cookies
93
- if cookie_manager.valid?
94
- session[:uid] = cookies[:uid]
95
- generate_new_token
96
- session[:ulogin] = cookies[:ulogin]
97
- session[:uid]
110
+ if persistent_login?
111
+ run_callbacks :login do
112
+ reset_session
113
+ session[:uid] = cookies[:uid]
114
+ generate_new_token
115
+ session[:ulogin] = cookies[:ulogin]
116
+ session[:uid]
117
+ end
98
118
  else
99
119
  cookie_manager.clear
100
120
  nil
data/spec/spec_helper.rb CHANGED
@@ -42,6 +42,9 @@ class ApplicationController
42
42
  @cookies = FakeCookieJar.new
43
43
  @request = FakeRequest.new
44
44
  end
45
+
46
+ def reset_session
47
+ end
45
48
  end
46
49
 
47
50
  # This is required to be an ActiveRecord like class
@@ -40,6 +40,16 @@ describe UberLogin do
40
40
  expect(session[:ulogin]).to be_nil
41
41
  end
42
42
  end
43
+
44
+ it 'resets the session' do
45
+ expect(controller).to receive :reset_session
46
+ controller.login(user)
47
+ end
48
+
49
+ it 'runs the :login callbacks' do
50
+ expect(controller).to receive(:run_callbacks).with(:login)
51
+ controller.login(user)
52
+ end
43
53
  end
44
54
 
45
55
  context 'remember is true' do
@@ -62,6 +72,11 @@ describe UberLogin do
62
72
  expect(cookies).to receive(:permanent).twice.and_return cookies
63
73
  controller.login(user, true)
64
74
  end
75
+
76
+ it 'resets the session' do
77
+ expect(controller).to receive :reset_session
78
+ controller.login(user, true)
79
+ end
65
80
  end
66
81
 
67
82
  context 'only one session is allowed per user' do
@@ -105,6 +120,11 @@ describe UberLogin do
105
120
  }.to change{ LoginToken.count }.by -1
106
121
  end
107
122
  end
123
+
124
+ it 'runs the :logout callbacks' do
125
+ expect(controller).to receive(:run_callbacks).with(:logout)
126
+ controller.logout
127
+ end
108
128
  end
109
129
 
110
130
  context 'sequence is equal to current user sequence' do
@@ -213,6 +233,16 @@ describe UberLogin do
213
233
  context 'the cookies are valid' do
214
234
  before { UberLogin::CookieManager.any_instance.stub(:valid?).and_return true }
215
235
 
236
+ it 'resets the session' do
237
+ expect(controller).to receive :reset_session
238
+ controller.login(user, true)
239
+ end
240
+
241
+ it 'runs the :login callbacks' do
242
+ expect(controller).to receive(:run_callbacks)
243
+ controller.login(user, true)
244
+ end
245
+
216
246
  it 'returns an user object with that uid' do
217
247
  expect(controller.current_user.id).to eq "100"
218
248
  end
@@ -256,4 +286,35 @@ describe UberLogin do
256
286
  end
257
287
  end
258
288
  end
289
+
290
+ describe '#persistent_login?' do
291
+ context 'cookies[:uid] and cookies[:ulogin] are set' do
292
+ before {
293
+ cookies[:uid] = "100"
294
+ cookies[:ulogin] = "whatever:beef"
295
+ }
296
+
297
+ context 'the cookies are valid' do
298
+ before { UberLogin::CookieManager.any_instance.stub(:valid?).and_return true }
299
+
300
+ it 'is true' do
301
+ expect(controller.persistent_login?).to be_true
302
+ end
303
+ end
304
+
305
+ context 'the cookies are not valid' do
306
+ before { UberLogin::CookieManager.any_instance.stub(:valid?).and_return false }
307
+
308
+ it 'is false' do
309
+ expect(controller.persistent_login?).to be_false
310
+ end
311
+ end
312
+ end
313
+
314
+ context 'cookies are not set' do
315
+ it 'is false' do
316
+ expect(controller.persistent_login?).to be_false
317
+ end
318
+ end
319
+ end
259
320
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uber_login
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Boffa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-18 00:00:00.000000000 Z
11
+ date: 2014-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: useragent
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ type: :runtime
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description: Login and logout management with secure "remember me" capabilities and
28
42
  complete session control. You can even force a user to logout!
29
43
  email: fra.boffa@gmail.com