Skip to content

Commit 2c5cedb

Browse files
authored
Refresh access tokens before expiry (#89)
We've seen a small number of [exceptions in Experience CS][1] which are caused by a 401 response from Editor API. I'm fairly confident these are occurring when the access token used to make the request to Editor API has expired. My hypothesis is that the token is valid at the time the request comes into Experience CS (and therefore the auto refresh behaviour isn't triggered) but is invalid by the time we make the request to Editor API. This change reduces the risk of this happening by refreshing the token if it expires in the next 60 seconds. [1]: https://github.com/RaspberryPiFoundation/experience-cs/issues/914
2 parents f9bebfe + 10350f7 commit 2c5cedb

File tree

8 files changed

+20
-11
lines changed

8 files changed

+20
-11
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Removed
1515

16+
## [v4.2.1]
17+
18+
### Fixed
19+
20+
- Refresh access tokens before expiry (#89)
21+
1622
## [v4.2.0]
1723

1824
### Added
@@ -154,7 +160,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
154160
- rails model concern to allow host app to add auth behaviour to a model
155161
- callback, logout and failure routes to handle auth
156162

157-
[Unreleased]: https://github.com/RaspberryPiFoundation/rpi-auth/compare/v4.2.0...HEAD
163+
[Unreleased]: https://github.com/RaspberryPiFoundation/rpi-auth/compare/v4.2.1...HEAD
164+
[v4.2.1]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.2.1
158165
[v4.2.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.2.0
159166
[v4.1.1]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.1.1
160167
[v4.1.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.1.0

gemfiles/rails_6.1.gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
rpi_auth (4.2.0)
4+
rpi_auth (4.2.1)
55
oauth2
66
omniauth-rails_csrf_protection (~> 1.0.0)
77
omniauth_openid_connect (~> 0.7.1)

gemfiles/rails_7.0.gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
rpi_auth (4.2.0)
4+
rpi_auth (4.2.1)
55
oauth2
66
omniauth-rails_csrf_protection (~> 1.0.0)
77
omniauth_openid_connect (~> 0.7.1)

gemfiles/rails_7.1.gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
rpi_auth (4.2.0)
4+
rpi_auth (4.2.1)
55
oauth2
66
omniauth-rails_csrf_protection (~> 1.0.0)
77
omniauth_openid_connect (~> 0.7.1)

gemfiles/rails_7.2.gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
rpi_auth (4.2.0)
4+
rpi_auth (4.2.1)
55
oauth2
66
omniauth-rails_csrf_protection (~> 1.0.0)
77
omniauth_openid_connect (~> 0.7.1)

lib/rpi_auth/controllers/auto_refreshing_token.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
module RpiAuth
66
module Controllers
77
module AutoRefreshingToken
8+
REFRESH_WINDOW_IN_SECONDS = 60
9+
810
extend ActiveSupport::Concern
911

1012
include CurrentUser
@@ -18,7 +20,7 @@ module AutoRefreshingToken
1820
def refresh_credentials_if_needed
1921
return unless current_user
2022

21-
return if Time.now.to_i < current_user.expires_at
23+
return if Time.now.to_i + REFRESH_WINDOW_IN_SECONDS <= current_user.expires_at
2224

2325
current_user.refresh_credentials!
2426
self.current_user = current_user

lib/rpi_auth/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module RpiAuth
4-
VERSION = '4.2.0'
4+
VERSION = '4.2.1'
55
end

spec/dummy/spec/requests/refresh_credentials_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@
6363
log_in(user:)
6464
end
6565

66-
context 'when the access token has not expired' do
67-
let(:expires_at) { 10.seconds.from_now }
66+
context 'when the access token is valid for at least another 60 seconds' do
67+
let(:expires_at) { 60.seconds.from_now }
6868

6969
it_behaves_like 'the user is logged in'
7070
it_behaves_like 'there is no attempt to renew the token'
7171
end
7272

73-
context 'when the access token has expired' do
74-
let(:expires_at) { 10.seconds.ago }
73+
context 'when the access token expires in the next 60 seconds' do
74+
let(:expires_at) { 59.seconds.from_now }
7575

7676
before do
7777
allow(stub_oauth_client).to receive(:refresh_credentials).with(any_args).and_return({ access_token: 'foo',

0 commit comments

Comments
 (0)