Skip to content

update token handling logic #83

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

Merged
merged 16 commits into from
May 31, 2023
Merged

update token handling logic #83

merged 16 commits into from
May 31, 2023

Conversation

wu-clan
Copy link
Member

@wu-clan wu-clan commented May 29, 2023

WIP.

@wu-clan wu-clan requested a review from downdawn May 29, 2023 23:16
@wu-clan
Copy link
Member Author

wu-clan commented May 30, 2023

For related comments, please see: #44 (comment)

@wu-clan
Copy link
Member Author

wu-clan commented May 30, 2023

Hi, @downdawn

Now it is ready for review.

@downdawn
Copy link
Collaborator

Hi, @wu-clan
Please refer to the comment above.

@wu-clan
Copy link
Member Author

wu-clan commented May 31, 2023

Hi, @wu-clan Please refer to the comment above.

The token processing logic there is controlled by the front end and should not be implemented in the back end, which is only responsible for the interface

@wu-clan
Copy link
Member Author

wu-clan commented May 31, 2023

Or did I miss your point?

@downdawn
Copy link
Collaborator

Or did I miss your point?

The refresh token is not controlled by the front end, so it is not safe. unless there is a special need

@downdawn
Copy link
Collaborator

As discussed here, the first login returns the token and the refresh token, and the subsequent front-end can generate a new token based on the refresh token. We only need to provide an interface for generating a new token.
#44

@wu-clan
Copy link
Member Author

wu-clan commented May 31, 2023

Yes, but it already exists in this PR.

@downdawn
Copy link
Collaborator

Ok, we can merge this pr first, and make adjustments later

token = get_token(request)
user_id, _ = jwt_decode(token)
refresh_token, refresh_expire = await UserService.refresh_token(user_id=user_id, custom_time=custom_time)
@router.get('/refresh_token', summary='获取刷新 token', dependencies=[DependsUser])
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the purpose of providing an interface for refreshing token creation and acquisition?

Isn't the refresh token only created and returned when logging in?

return token, expire
refresh_token = jwt.encode(to_encode, settings.TOKEN_SECRET_KEY, settings.TOKEN_ALGORITHM)
prefix = f'{settings.TOKEN_REFRESH_REDIS_PREFIX}:{sub}:'
await redis_client.delete_prefix(prefix)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why delete all refresh tokens of this user?

if not current_user.is_superuser:
if not pk == current_user.id:
raise errors.AuthorizationError
if await UserDao.get(db, pk):
Copy link
Collaborator

Choose a reason for hiding this comment

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

if not await UserDao.get(db, pk):
raise errors.NotFoundError(msg='用户不存在')

Copy link
Collaborator

Choose a reason for hiding this comment

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

There is already get_multi_login inside, it seems a bit confusing here?

@@ -218,4 +161,10 @@ async def delete(*, username: str, current_user: User) -> int:
if not input_user:
raise errors.NotFoundError(msg='用户不存在')
count = await UserDao.delete(db, input_user.id)
prefix = [
Copy link
Collaborator

Choose a reason for hiding this comment

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

Log out here to delete all tokens of the user. If it is a multi-point login, is it also logged out in other places?

@wu-clan
Copy link
Member Author

wu-clan commented May 31, 2023

Now it's ready

@wu-clan wu-clan merged commit 227d76c into fastapi-practices:master May 31, 2023
@wu-clan wu-clan deleted the update-token-logic branch May 31, 2023 07:24
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.

2 participants