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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dfa27c22c1d17a8d6ac0bae61dc1e1c566bd32e9
4
- data.tar.gz: 5986f0073d029ce33c700520824505b9653f2885
3
+ metadata.gz: 06e6dfcf106062c1fb84355a92286dd8a150adea
4
+ data.tar.gz: 6ecef6ecc87eeceb42e1a44e693ec061ca7109ac
5
5
  SHA512:
6
- metadata.gz: 1aa7e611af1d416b2633f5808c1e6ad9454d8a388dc7d2eb93d5e4c779be71b4b5b8c260811de0d1f8dd3a6dfcbea3887f2f4b115c1e021ce329f7928fc4b74c
7
- data.tar.gz: 5993c5066b88ce9ca004155b1e879b31da46b76a65334a991a1b388e6d7b2cda606c2b107c5aa8ae3b8af40b1e1a921c2687840cc9c2b8246b3419b689c67e98
6
+ metadata.gz: a57cb90c9249979d227e2b9a704572609022a280b37f79901dc83382e2331b87c865c5ecd56333bbfa525bdd029e736bfb1f594a01d1af751e8f5a356e79183d
7
+ data.tar.gz: e7386eb960f36c4fb240c993b4c2251f58bc67ff2cc06d30ceb52576cce4bf0e50914a43b26a0a52709b548defe4876a6e630080e9d0e686615d47401d8441c0
@@ -1,3 +1,8 @@
1
+ 1.5.0
2
+ -----
3
+
4
+ - Added threadsafe strategy when refreshing a token
5
+
1
6
  1.4.0
2
7
  -----
3
8
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lifen (1.4.0)
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.0)
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
@@ -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
- return if active?
45
+ @@lock.synchronize do
46
+
47
+ yield(self) if block_given?
43
48
 
44
- refresh
49
+ return if active?
45
50
 
46
- raise Error, "Token can't be refreshed" if !active?
51
+ refresh
52
+
53
+ raise Error, "Token can't be refreshed" if !active?
54
+ end
47
55
  end
48
56
 
49
57
  private
@@ -1,3 +1,3 @@
1
1
  module Lifen
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -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.0
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: 2016-12-27 00:00:00.000000000 Z
11
+ date: 2017-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler