Skip to content

Commit 1ae8b79

Browse files
authored
Standardise origin param as returnTo (#47)
This ties in our existing use of the `returnTo` param, allowing apps to have consistent interfaces.
2 parents 70888ca + b7769ea commit 1ae8b79

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

CHANGELOG.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## Unreleased
8+
## [Unreleased]
99

1010
### Added
1111
- Added dummy route for `/auth/rpi` to add path helper `rpi_auth_login` (#44)
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Refactored `RpiAuth::Models::Authenticatable` to fix "include"/"extend" issues (#44)
1717
- Refactored `RpiAuth::AuthController#callback` to reduce its complexity (#44)
1818
- Refactored how auth bypass is enabled (#44)
19+
- OmniAuth origin parameter name set as `returnTo` (#47)
1920

2021
## Updated
2122

@@ -46,18 +47,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4647

4748
- omniauth-rpi gem updated to fix nil user ID in returned user object
4849

49-
## [v1.0.0]
50+
## [v1.1.0]
5051

51-
### Added
52+
- Defaults to setting the `user_id` param on the model rather than id (so that the application can use an internal ID structure for the user model).
5253

53-
- Rails 7 / Ruby 3.1 support (these are the only officially supported versions)
54+
## [v1.0.1]
5455

55-
## [Unreleased]
56+
- Updates Omniauth-rpi to latest version (fixing a bug where the returbed uid was empty)
57+
58+
## [v1.0.0]
5659

5760
### Added
5861

62+
- Rails 7 / Ruby 3.1 support (these are the only officially supported versions)
5963
- omniauth-rpi strategy to auth via Hydra1
6064
- include omniauth rails csrf protection
6165
- configuration to allow setting endpoints and credentials for auth
6266
- rails model concern to allow host app to add auth behaviour to a model
6367
- callback, logout and failure routes to handle auth
68+
69+
[Unreleased]: https://github.com/RaspberryPiFoundation/rpi-auth/compare/v1.4.0...HEAD
70+
[v1.4.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v1.4.0
71+
[v1.3.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v1.3.0
72+
[v1.2.1]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v1.2.1
73+
[v1.2.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v1.2.0
74+
[v1.1.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v1.1.0
75+
[v1.0.1]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v1.0.1
76+
[v1.0.0]: https://github.com/RaspberryPiFoundation/rpi-auth/releases/tag/v1.0.0

README.md

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,60 @@ This model needs to be the same one defined in the initializer, an instance will
8181
To login via Hydra your app needs to send the user to `/auth/rpi` via a POST request:
8282

8383
```ruby
84-
<%= link_to 'Log in', '/auth/rpi', method: :post %>
84+
link_to 'Log in', '/auth/rpi', method: :post
8585
# or:
86-
<%= button_to 'Log in', '/auth/rpi' %>
86+
button_to 'Log in', '/auth/rpi'
8787
```
8888

8989
A GET request will be blocked by the CSRF protection gem.
9090

9191
Alternatively you can use the path helpers:
9292

9393
```ruby
94-
<%= link_to 'Log in', rpi_auth_login_path, method: :post %>
94+
link_to 'Log in', rpi_auth_login_path, method: :post
9595
# or:
96-
<%= button_to 'Log in', rpi_auth_login_path %>
96+
button_to 'Log in', rpi_auth_login_path
9797
```
9898

9999
There is also a helper for the logout route:
100100

101101
```ruby
102-
<%= link_to 'Log out', rpi_auth_logout_path %>
102+
link_to 'Log out', rpi_auth_logout_path
103103
```
104104

105+
### Sin
106+
107+
### Redirecting users to the "next step"
108+
109+
There are a three possible places the user will end up at following logging in,
110+
in the following order:
111+
112+
1. The `success_redirect` URL.
113+
2. The specified `returnTo` URL.
114+
3. The page the user was on (if the Referer header is sent in).
115+
4. The root path of the application.
116+
117+
Note that none of these places can be on a different host, i.e. they have to be
118+
inside your application.
119+
120+
The `success_redirect` set in the RpiAuth configuration block will trump
121+
everything, so only use this configuration option if you always want your users
122+
to end up at the same place.
123+
124+
If you wish to redirect users to the next step in the process, e.g. to a
125+
registration form, then you should supply a parameter called `returnTo` which
126+
is then used to redirect after log in/sign up are successful.
127+
128+
```ruby
129+
button_to 'Log in to start registraion', rpi_auth_login_path, params: { returnTo: '/registration_form' }
130+
```
131+
132+
If this parameter is missing [Omniauth uses the HTTP Referer
133+
header](https://github.com/omniauth/omniauth/blob/d2fd0fc80b0342046484b99102fa00ec5b5392ff/lib/omniauth/strategy.rb#L252-L256)
134+
meaning (most) users will end up back on the page where they started the auth flow (this is often the most preferable situation).
135+
136+
Finally, if none of these things are set, we end up back at the application root.
137+
105138
### Globbed/catch-all routes
106139

107140
If your app has a catch-all route at the end of the routing table, you must

lib/rpi_auth/engine.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class Engine < ::Rails::Engine
2929
authorize_url: "#{RpiAuth.configuration.auth_url}/oauth2/auth",
3030
token_url: "#{RpiAuth.configuration.auth_token_url || RpiAuth.configuration.auth_url}/oauth2/token"
3131
},
32-
authorize_params: { brand: RpiAuth.configuration.brand }
32+
authorize_params: { brand: RpiAuth.configuration.brand },
33+
origin_param: 'returnTo'
3334
)
3435

3536
OmniAuth.config.on_failure = RpiAuth::AuthController.action(:failure)

spec/dummy/spec/requests/auth_request_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@
151151
end
152152
end
153153

154+
context 'when returnTo param is set' do
155+
it 'redirects back to the original page' do
156+
post '/auth/rpi', params: { returnTo: 'http://www.example.com/bar' }
157+
expect(response).to redirect_to('/rpi_auth/auth/callback')
158+
follow_redirect!
159+
160+
expect(response).to redirect_to('/bar')
161+
end
162+
end
163+
154164
context 'when success_redirect is set in config' do
155165
before do
156166
RpiAuth.configuration.success_redirect = 'http://www.example.com/success'

0 commit comments

Comments
 (0)