Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make User.from_token robust to invalid token #337

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

floehopper
Copy link
Contributor

@floehopper floehopper commented Jun 19, 2024

Previously, when an API request was made using an expired or invalid access token, a Faraday::UnauthorizedError was raised and the request failed.

Recently we've seen a bunch of these exceptions happening due to some other problems in editor-standalone and/or in the editor-ui web component - see this issue for more details.

Failing hard with an exception like this seems a bit over the top when the user is trying to view a public project for which they don't need to be logged-in. And it seems as if User.from_token might have been expecting HydraPublicApiClient.fetch_oauth_user to return nil when the token was invalid when in fact it returns a 401 Unauthorized HTTP status code which results in a Faraday::UnauthorizedError exception being raised.

This commit rescues the Faraday::UnauthorizedError exception, captures the exception in Sentry in case we want to know about it, but then returns nil. This means that if the user is trying to carry out an action that does not require them to be logged-in, they can still do so despite their access token not being valid.

Previously, when an API request was made using an expired or invalid
access token, a `Faraday::UnauthorizedError` was raised and the request
failed.

Recently we've seen a bunch of these exceptions happening due to some
other problems in editor-standalone and/or in the editor-ui web
component - see this issue [1] for more details.

Failing hard with an exception like this seems a bit over the top when
the user is trying to view a public project for which they don't need to
be logged-in. And it seems as if `User.from_token` might have been
expecting `HydraPublicApiClient.fetch_oauth_user` to return `nil` when
the token was invalid [2].

This commit rescues the `Faraday::UnauthorizedError` exception, captures
the exception in Sentry in case we want to know about it, but then
returns `nil`. This means that if the user is trying to carry out an
action that does not require them to be logged-in, they can still do so
despite their access token not being valid.

[1]: RaspberryPiFoundation/editor-ui#1044
[2]: https://github.com/RaspberryPiFoundation/editor-api/blob/055741503b0ad295e44993c9f55b2fc95e912beb/app/models/user.rb#L91
@cla-bot cla-bot bot added the cla-signed label Jun 19, 2024
@raspberrypiherokubot raspberrypiherokubot temporarily deployed to editor-api-p-make-user--pirhik June 19, 2024 16:06 Inactive
Copy link
Contributor

@sra405 sra405 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice!

@floehopper floehopper merged commit 0488d78 into main Jun 19, 2024
3 checks passed
@floehopper floehopper deleted the make-user-from-token-robust-to-invalid-token branch June 19, 2024 16:09
floehopper added a commit to RaspberryPiFoundation/editor-ui that referenced this pull request Jun 20, 2024
This reverts commit eac96dd.

The problem that this fix was addressing has been more effectively
addressed in this editor-api PR [1].

[1]: RaspberryPiFoundation/editor-api#337.
floehopper added a commit to RaspberryPiFoundation/editor-ui that referenced this pull request Jun 20, 2024
This reverts commit 4be26c8.

This was in preparation for fixing a problem that has now been more
effectively addressed in this editor-api PR [1].

[1]: RaspberryPiFoundation/editor-api#337.
floehopper added a commit to RaspberryPiFoundation/editor-ui that referenced this pull request Jun 20, 2024
This reverts commit eac96dd.

This commit was part of #1046. The problem that this fix was addressing
has been more effectively addressed in this editor-api PR [1].

[1]: RaspberryPiFoundation/editor-api#337.
floehopper added a commit to RaspberryPiFoundation/editor-ui that referenced this pull request Jun 20, 2024
This reverts commit 4be26c8.

This commit was part of #1046. This was in preparation for fixing a
problem that has now been more effectively addressed in this
editor-api PR [1].

[1]: RaspberryPiFoundation/editor-api#337.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants