Skip to content

Commit fb6de87

Browse files
authored
Merge branch 'main' into update-workflows
2 parents 6a07904 + b0877e1 commit fb6de87

36 files changed

+853
-69
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ jobs:
2828
ruby-version: ${{ matrix.ruby-version }}
2929
rubygems: 3.4.10
3030
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
31+
32+
- name: Run linter
33+
if: matrix.ruby-version == '3.3' && matrix.gemfile == 'rails_7.2'
34+
run: bundle exec rubocop
35+
3136
- name: Run tests
3237
run: bundle exec rspec
3338

.rubocop_todo.yml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-04-14 10:55:46 UTC using RuboCop version 1.75.2.
3+
# on 2025-06-04 10:53:17 UTC using RuboCop version 1.75.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 11
9+
# Offense count: 12
1010
# Configuration parameters: EnforcedStyle, AllowedGems, Include.
1111
# SupportedStyles: Gemfile, gems.rb, gemspec
1212
# Include: **/*.gemspec, **/Gemfile, **/gems.rb
@@ -28,19 +28,12 @@ RSpec/ExampleLength:
2828
RSpec/MultipleExpectations:
2929
Max: 4
3030

31-
# Offense count: 8
31+
# Offense count: 9
3232
# Configuration parameters: AllowSubject.
3333
RSpec/MultipleMemoizedHelpers:
3434
Max: 6
3535

36-
# Offense count: 12
36+
# Offense count: 13
3737
# Configuration parameters: AllowedGroups.
3838
RSpec/NestedGroups:
3939
Max: 5
40-
41-
# Offense count: 1
42-
# Configuration parameters: Include, CustomTransform, IgnoreMethods, IgnoreMetadata.
43-
# Include: **/*_spec.rb
44-
RSpec/SpecFilePathFormat:
45-
Exclude:
46-
- 'spec/rpi_auth/models/authenticatable_spec.rb'

CHANGELOG.md

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

1515
### Removed
1616

17+
## [v4.3.0]
18+
19+
### Added
20+
21+
- Add optional `on_login_success` callback (#90)
22+
23+
### Fixed
24+
25+
- Return boolean from `AccountTypes#student_account?` (#91)
26+
27+
### Removed
28+
29+
## [v4.2.1]
30+
31+
### Fixed
32+
33+
- Refresh access tokens before expiry (#89)
34+
35+
## [v4.2.0]
36+
37+
### Added
38+
39+
- Allow OmniAuth setup phase to be configured (#76)
40+
- Add `RpiAuth::Models::Roles#parsed_roles` (extracted from experience-cs) (#87)
41+
- Add `RpiAuth::Models::AccountTypes#student_account?` (extracted from experience-cs) (#87)
42+
43+
## [v4.1.1]
44+
45+
### Fixed
46+
47+
- Fix requiring of oauth2 to avoid `NoMethodError: undefined method 'config' for module OAuth2` (#86)
48+
49+
## [v4.1.0]
50+
51+
### Added
52+
- Add access token-related functionality including auto-refresh (#83)
53+
54+
### Fixed
55+
- Fix use of `User#expires_at` in `SpecHelpers#stub_auth_for` (#82)
56+
1757
## [v4.0.0]
1858

1959
### Added
@@ -133,7 +173,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
133173
- rails model concern to allow host app to add auth behaviour to a model
134174
- callback, logout and failure routes to handle auth
135175

136-
[Unreleased]: https://github.com/RaspberryPiFoundation/rpi-auth/compare/v4.0.0...HEAD
176+
[Unreleased]: https://github.com/RaspberryPiFoundation/rpi-auth/compare/v4.3.0...HEAD
177+
[v4.3.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.3.0
178+
[v4.2.1]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.2.1
179+
[v4.2.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.2.0
180+
[v4.1.1]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.1.1
181+
[v4.1.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.1.0
137182
[v4.0.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v4.0.0
138183
[v3.6.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v3.6.0
139184
[v3.5.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v3.5.0

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ link_to 'Log out', rpi_auth_logout_path, params: { returnTo: '/thanks-dude' }
170170

171171
This has to be a relative URL, i.e. it has to start with a slash. This is to ensure there's no open redirect.
172172

173+
### Callback on successful login
174+
175+
If the RpiAuth configuration option `on_login_success` is set to a `Proc`, this will be called in the context of the `RpiAuth::AuthController#callback` action, i.e. `current_user` will be available. This is intended to allow apps to record successful logins.
176+
173177
### Globbed/catch-all routes
174178

175179
If your app has a catch-all route at the end of the routing table, you must
@@ -187,6 +191,29 @@ class in `config/application.rb`.
187191
config.railties_order = [RpiAuth::Engine, :main_app, :all]
188192
```
189193

194+
### Obtaining an access token for user
195+
196+
This optional behaviour is useful if your Rails app (which is using this gem)
197+
needs to use a RPF API which required authentication via an OAuth2 access
198+
token.
199+
200+
Include the `RpiAuth::Models::WithTokens` concern (which depends on the
201+
`RpiAuth::Models::Authenticatable` concern) into your user model in order to
202+
add `access_token`, `refresh_token` & `expires_at` attributes. These methods
203+
are automatically populated by `RpiAuth::AuthController#callback` via the
204+
`RpiAuth::Models::WithTokens.from_omniauth` method.
205+
206+
This also relies on the following:
207+
- `RpiAuth.configuration.scope` including the "offline" scope in the Rails app
208+
which is using the `rpi_auth` gem.
209+
- In the `profile` app `hydra_client` config for the Rails app, `grant_types`
210+
must include "refresh_token" and `scope` must include "offline".
211+
212+
Include the `RpiAuth::Controllers::AutoRefreshingToken` concern (which depends
213+
on the `RpiAuth::Controllers::CurrentUser` concern) into your controller so
214+
that when the user's access token expires, a new one is obtained using the
215+
user's refresh token.
216+
190217
## Test helpers and routes
191218

192219
There are some standardised test helpers in `RpiAuth::SpecHelpers` that can be used when testing.

app/controllers/rpi_auth/auth_controller.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def callback
2727
auth = request.env['omniauth.auth']
2828
self.current_user = RpiAuth.user_model.from_omniauth(auth)
2929

30+
run_login_success_callback
31+
3032
redirect_to ensure_relative_url(login_redirect_path)
3133
end
3234

@@ -53,6 +55,16 @@ def failure
5355

5456
private
5557

58+
def run_login_success_callback
59+
return unless RpiAuth.configuration.on_login_success.is_a?(Proc)
60+
61+
begin
62+
instance_exec(&RpiAuth.configuration.on_login_success)
63+
rescue StandardError => e
64+
Rails.logger.warn("Caught #{e} while processing on_login_success proc.")
65+
end
66+
end
67+
5668
def login_redirect_path
5769
unless RpiAuth.configuration.success_redirect.is_a?(Proc)
5870
return RpiAuth.configuration.success_redirect || request.env['omniauth.origin']

gemfiles/rails_6.1.gemfile.lock

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
PATH
22
remote: ..
33
specs:
4-
rpi_auth (4.0.0)
4+
rpi_auth (4.3.0)
5+
oauth2
56
omniauth-rails_csrf_protection (~> 1.0.0)
67
omniauth_openid_connect (~> 0.7.1)
78
rails (>= 6.1.4)
@@ -74,6 +75,7 @@ GEM
7475
ast (2.4.3)
7576
attr_required (1.0.2)
7677
base64 (0.2.0)
78+
bigdecimal (3.1.9)
7779
bindata (2.5.1)
7880
builder (3.3.0)
7981
byebug (12.0.0)
@@ -88,35 +90,41 @@ GEM
8890
xpath (~> 3.2)
8991
coderay (1.1.3)
9092
concurrent-ruby (1.3.5)
93+
crack (1.0.0)
94+
bigdecimal
95+
rexml
9196
crass (1.0.6)
9297
date (3.4.1)
9398
diff-lcs (1.6.1)
9499
docile (1.4.1)
95100
email_validator (2.2.4)
96101
activemodel
97102
erubi (1.13.1)
98-
faraday (2.13.0)
103+
faraday (2.13.4)
99104
faraday-net_http (>= 2.0, < 3.5)
100105
json
101106
logger
102107
faraday-follow_redirects (0.3.0)
103108
faraday (>= 1, < 3)
104-
faraday-net_http (3.4.0)
109+
faraday-net_http (3.4.1)
105110
net-http (>= 0.5.0)
106111
ffi (1.17.1)
107112
globalid (1.2.1)
108113
activesupport (>= 6.1)
114+
hashdiff (1.1.2)
109115
hashie (5.0.0)
110116
i18n (1.14.7)
111117
concurrent-ruby (~> 1.0)
112118
json (2.10.2)
113-
json-jwt (1.16.7)
119+
json-jwt (1.17.0)
114120
activesupport (>= 4.2)
115121
aes_key_wrap
116122
base64
117123
bindata
118124
faraday (~> 2.0)
119125
faraday-follow_redirects
126+
jwt (3.1.2)
127+
base64
120128
language_server-protocol (3.17.0.4)
121129
lint_roller (1.1.0)
122130
listen (3.9.0)
@@ -137,6 +145,8 @@ GEM
137145
mini_mime (1.1.5)
138146
mini_portile2 (2.8.8)
139147
minitest (5.25.5)
148+
multi_xml (0.7.1)
149+
bigdecimal (~> 3.1)
140150
net-http (0.6.0)
141151
uri
142152
net-imap (0.5.6)
@@ -152,6 +162,14 @@ GEM
152162
nokogiri (1.18.7)
153163
mini_portile2 (~> 2.8.2)
154164
racc (~> 1.4)
165+
oauth2 (2.0.12)
166+
faraday (>= 0.17.3, < 4.0)
167+
jwt (>= 1.0, < 4.0)
168+
logger (~> 1.2)
169+
multi_xml (~> 0.5)
170+
rack (>= 1.2, < 4)
171+
snaky_hash (~> 2.0, >= 2.0.3)
172+
version_gem (>= 1.1.8, < 3)
155173
omniauth (2.1.3)
156174
hashie (>= 3.4.6)
157175
rack (>= 2.2.3)
@@ -237,6 +255,7 @@ GEM
237255
rb-inotify (0.11.1)
238256
ffi (~> 1.0)
239257
regexp_parser (2.10.0)
258+
rexml (3.4.1)
240259
rspec-core (3.13.3)
241260
rspec-support (~> 3.13.0)
242261
rspec-expectations (3.13.3)
@@ -290,6 +309,9 @@ GEM
290309
simplecov_json_formatter (~> 0.1)
291310
simplecov-html (0.13.1)
292311
simplecov_json_formatter (0.1.4)
312+
snaky_hash (2.0.3)
313+
hashie (>= 0.1.0, < 6)
314+
version_gem (>= 1.1.8, < 3)
293315
sprockets (4.2.1)
294316
concurrent-ruby (~> 1.0)
295317
rack (>= 2.2.4, < 4)
@@ -313,10 +335,15 @@ GEM
313335
validate_url (1.0.15)
314336
activemodel (>= 3.0.0)
315337
public_suffix
338+
version_gem (1.1.8)
316339
webfinger (2.1.3)
317340
activesupport
318341
faraday (~> 2.0)
319342
faraday-follow_redirects
343+
webmock (3.25.1)
344+
addressable (>= 2.8.0)
345+
crack (>= 0.3.2)
346+
hashdiff (>= 0.4.0, < 2.0.0)
320347
websocket-driver (0.7.7)
321348
base64
322349
websocket-extensions (>= 0.1.0)
@@ -342,6 +369,7 @@ DEPENDENCIES
342369
rubocop-rails
343370
rubocop-rspec
344371
simplecov
372+
webmock
345373

346374
BUNDLED WITH
347375
2.3.27

0 commit comments

Comments
 (0)