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

Implement refresh endpoint #11

Merged
merged 15 commits into from
Jan 30, 2024
Merged

Implement refresh endpoint #11

merged 15 commits into from
Jan 30, 2024

Conversation

VKTB
Copy link
Collaborator

@VKTB VKTB commented Dec 21, 2023

Description

Implements a /refresh POST endpoint and relevant application logic that takes a JWT access token along with a JWT refresh token, generates an updated JWT access token, and returns it along with the JWT refresh token as an HTTP-only cookie.

Testing instructions

Add a set of instructions describing how the reviewer should test the code

  • Review code
  • Check Actions build
  • Supply valid access token
  • Supply invalid access token
  • Supply invalid refresh token
  • Supply no refresh token

Agile board tracking

closes #10

@VKTB VKTB linked an issue Dec 21, 2023 that may be closed by this pull request
@VKTB VKTB requested a review from joelvdavies January 12, 2024 09:27
Copy link
Collaborator

@joelvdavies joelvdavies left a comment

Choose a reason for hiding this comment

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

One minor thing and same query about tests/linting.

Also note for @joshuadkitenge, it appears operations gateway-api/this actually currently uses the icat provider instead of the jwtAuthProvider (https://github.com/ral-facilities/scigateway/blob/8c501b09aeec1fdf6c04474c080b1b30577c86be/src/authentication/icatAuthProvider.tsx vs https://github.com/ral-facilities/scigateway/blob/8c501b09aeec1fdf6c04474c080b1b30577c86be/src/authentication/jwtAuthProvider.tsx). The main difference is the inclusion of the maintenance states by the looks of it. Also the jwt provider instead uses /api/jwt/authenticate, /api/jwt/checkToken, /api/jwt/refresh instead which will also not currently work unless we change this to use that instead. Never mind - it was updated in the React 18 branch.

Comment on lines 58 to 61
payload = self._get_jwt_payload(access_token, {"verify_exp": False})
payload["exp"] = datetime.now(timezone.utc) + timedelta(
minutes=config.authentication.access_token_validity_minutes
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just some info on token rotation https://auth0.com/docs/secure/tokens/refresh-tokens/refresh-token-rotation I mentioned. No need here though.

)
def refresh_access_token(
jwt_handler: Annotated[JWTHandler, Depends(JWTHandler)],
access_token: Annotated[str, Query(description="The JWT access token to refresh")],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Addressed in 886f9c1

@VKTB VKTB requested a review from joelvdavies January 18, 2024 10:51
Copy link
Collaborator

@joelvdavies joelvdavies left a comment

Choose a reason for hiding this comment

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

Code LGTM - On the refresh issue, it appears SciGateway is missing { withCredentials: true } in the refresh axios request to send the cookie - Adding it made it work for me otherwise it couldn't find the cookie like you mentioned. Although I don't know why this hasn't already been addressed unless it hasn't been tried yet or there is some other way around it.

Although even with that change - it fails as the token is returned in the body but needs

{
    "token": token
}

instead of

{
    token
}

I don't know why it is different to the login though - it doesn't look like operations gateway does this either, so I suspect SciGateway needs updating. If this looks right to you, I think I can just request the changes on the react-18 pr.

@VKTB
Copy link
Collaborator Author

VKTB commented Jan 23, 2024

@joelvdavies I am not sure about the cookie, sorry. Regarding the response for the /refresh endpoint, SciGateway Auth just returns a string like the /login endpoint does.

Base automatically changed from implement-verify-endpoint-#8 to main January 23, 2024 08:26
@joelvdavies
Copy link
Collaborator

@VKTB SciGateway auth also has a maintenance endpoint so it looks like it uses SciGateway's icatAuthProvider, if I look at https://github.com/ral-facilities/scigateway/pull/1275/files#diff-262fc2f51a09abb09571717b6aa50067596d5fd00a8953bf8b1624ad2ce4f281R15 - The login was changed to use data.token, but the refresh was left as res.data.token. I will raise it with Louise as I have a catch-up today anyway, it definitely looks like it just needs changing too.

Copy link
Collaborator

@joelvdavies joelvdavies left a comment

Choose a reason for hiding this comment

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

It seems like the issue with refresh is likely CORS related, so should be solved when going via proxy/potentially by using https so have agreed to test this again once its setup so it can be merged now.

@VKTB VKTB merged commit affb151 into main Jan 30, 2024
2 checks passed
@VKTB VKTB deleted the implement-refresh-endpoint-#10 branch January 30, 2024 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement refresh endpoint
2 participants