lifen 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +2 -2
- data/README.md +15 -0
- data/lib/lifen/token.rb +11 -3
- data/lib/lifen/version.rb +1 -1
- data/spec/token_spec.rb +43 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06e6dfcf106062c1fb84355a92286dd8a150adea
|
4
|
+
data.tar.gz: 6ecef6ecc87eeceb42e1a44e693ec061ca7109ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a57cb90c9249979d227e2b9a704572609022a280b37f79901dc83382e2331b87c865c5ecd56333bbfa525bdd029e736bfb1f594a01d1af751e8f5a356e79183d
|
7
|
+
data.tar.gz: e7386eb960f36c4fb240c993b4c2251f58bc67ff2cc06d30ceb52576cce4bf0e50914a43b26a0a52709b548defe4876a6e630080e9d0e686615d47401d8441c0
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lifen (1.
|
4
|
+
lifen (1.5.0)
|
5
5
|
faraday (>= 0.9)
|
6
6
|
inflecto
|
7
7
|
virtus (>= 1.0)
|
@@ -24,7 +24,7 @@ GEM
|
|
24
24
|
thread_safe (~> 0.3, >= 0.3.1)
|
25
25
|
diff-lcs (1.2.5)
|
26
26
|
equalizer (0.0.11)
|
27
|
-
faraday (0.10.
|
27
|
+
faraday (0.10.1)
|
28
28
|
multipart-post (>= 1.2, < 3)
|
29
29
|
hashdiff (0.3.0)
|
30
30
|
ice_nine (0.11.2)
|
data/README.md
CHANGED
@@ -64,6 +64,21 @@ user.settings.push_notifications = false
|
|
64
64
|
user.settings.save
|
65
65
|
```
|
66
66
|
|
67
|
+
### Refreshing a token in a threadsafe way
|
68
|
+
|
69
|
+
Depending on the OAuth provider strategy, the token refresh strategy has to be adapted. In our case, if token are stored in a database, you can ask the client to reload the token from your database value before performing a real refresh:
|
70
|
+
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
token.refresh_once_if_needed do |token|
|
74
|
+
current_user.reload
|
75
|
+
|
76
|
+
token.value = current_user.lifen_token
|
77
|
+
token.expires_at = current_user.lifen_token_expires_at.to_i
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
|
67
82
|
### Managing flows
|
68
83
|
|
69
84
|
```ruby
|
data/lib/lifen/token.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
module Lifen
|
2
2
|
class Token
|
3
|
+
|
4
|
+
@@lock = Mutex.new
|
5
|
+
|
3
6
|
include Virtus.model(finalize: false)
|
4
7
|
|
5
8
|
attribute :user, "Lifen::User"
|
@@ -39,11 +42,16 @@ module Lifen
|
|
39
42
|
end
|
40
43
|
|
41
44
|
def refresh_once_if_needed
|
42
|
-
|
45
|
+
@@lock.synchronize do
|
46
|
+
|
47
|
+
yield(self) if block_given?
|
43
48
|
|
44
|
-
|
49
|
+
return if active?
|
45
50
|
|
46
|
-
|
51
|
+
refresh
|
52
|
+
|
53
|
+
raise Error, "Token can't be refreshed" if !active?
|
54
|
+
end
|
47
55
|
end
|
48
56
|
|
49
57
|
private
|
data/lib/lifen/version.rb
CHANGED
data/spec/token_spec.rb
CHANGED
@@ -76,4 +76,47 @@ describe Lifen::Token do
|
|
76
76
|
|
77
77
|
end
|
78
78
|
|
79
|
+
describe 'thread safe' do
|
80
|
+
|
81
|
+
let(:user) { Lifen::User.new }
|
82
|
+
|
83
|
+
let(:expired_token) { Lifen::Token.new(user: user, value: "valid_token", expires_at: Time.now.to_i - 60) }
|
84
|
+
let(:other_expired_token) { Lifen::Token.new(user: user, value: "valid_token", expires_at: Time.now.to_i - 60) }
|
85
|
+
|
86
|
+
before do
|
87
|
+
allow(expired_token).to receive(:refresh) do
|
88
|
+
sleep 1
|
89
|
+
expired_token.expires_at = Time.now.to_i + 60
|
90
|
+
end
|
91
|
+
|
92
|
+
allow(other_expired_token).to receive(:refresh) do
|
93
|
+
sleep 1
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'queues refresh calls' do
|
98
|
+
expect(expired_token).to receive(:refresh).once
|
99
|
+
expect(other_expired_token).to_not receive(:refresh)
|
100
|
+
|
101
|
+
threads = []
|
102
|
+
|
103
|
+
threads << Thread.new do
|
104
|
+
expired_token.refresh_once_if_needed do |token|
|
105
|
+
token.value = "expired_token"
|
106
|
+
token.expires_at = Time.now.to_i - 60
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
threads << Thread.new do
|
111
|
+
other_expired_token.refresh_once_if_needed do |token|
|
112
|
+
token.value = "valid_token"
|
113
|
+
token.expires_at = Time.now.to_i + 60
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
threads.map { |t| t.join } # waiting for all threads to finish
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
79
122
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lifen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Etienne Depaulis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|