Commit 057049a
committed
Add Controllers::AutoRefreshingToken concern
This can optionally be included into your controller to automatically
use the user's refresh token to obtain a new access token when the old
one expires. It adds a before action to the target controller which
automatically calls `OauthClient#refresh_credentials!` if the user is
signed in and their access token has expired.
Again this has been substantially copied from `code-club-frontend`:
- `app/controllers/application_controller.rb` [1]
- `spec/requests/refresh_credentials_spec.rb` [2]
Some things that I've changed:
- Moved functionality from `ApplicationController` into an `RpiAuth`
namespaced concern which can be included into the controller in the
Rails app.
- Make `AutoRefreshingToken#refresh_credentials_if_needed` private since
it's not a controller action.
- Use the home page in the dummy app in the request spec instead of the
home page in `code-club-frontend`.
- Construct a user directly in the request spec; there's no user factory
in the `rpi_auth` gem.
- Set `RpiAuth.configuration.user_model = 'User'` in the `before` block
as per the other request specs.
I'm slightly concerned about a few details of the implementation of
`AutoRefreshingToken#refresh_credentials_if_needed`:
- Rescuing all `ArgumentError` exceptions seems risky, because it's
quite a common exception. It would be better to be more specific.
- Even rescuing all `OAuth2::Error` exceptions seems a bit broad,
although I haven't investigated what this encompasses.
- While resetting the session in the event of an exception seems OK
security-wise, it probably doesn't result in a great user experience.
It would be better to redirect and/or display an error message to the
user to explain what's happened.
[1]: https://github.com/RaspberryPiFoundation/code-club-frontend/blob/f7e965798d910584fed0d1eb7867f32a899f9ce8/app/controllers/application_controller.rb#L8
[2]: https://github.com/RaspberryPiFoundation/code-club-frontend/blob/f7e965798d910584fed0d1eb7867f32a899f9ce8/spec/requests/refresh_credentials_spec.rb1 parent fdfc794 commit 057049a
File tree
3 files changed
+128
-0
lines changed- lib/rpi_auth/controllers
- spec/dummy
- app/controllers
- spec/requests
3 files changed
+128
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
| 6 | + | |
4 | 7 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
0 commit comments