Skip to content

Commit 6fbddba

Browse files
downdawn66wu-clan
authored andcommitted
fix jwt parameter parsing error
1 parent 4c12c53 commit 6fbddba

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

backend/app/common/jwt.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,21 @@ def password_verify(plain_password: str, hashed_password: str) -> bool:
4343
return pwd_context.verify(plain_password, hashed_password)
4444

4545

46-
async def create_access_token(sub: int | Any, data: dict, expires_delta: timedelta | None = None) -> str:
46+
async def create_access_token(sub: int | Any, expires_delta: timedelta | None = None, **kwargs) -> str:
4747
"""
4848
Generate encryption token
4949
5050
:param sub: The subject/userid of the JWT
51-
:param data: Data transferred to the token
5251
:param expires_delta: Increased expiry time
5352
:return:
5453
"""
5554
if expires_delta:
5655
expires = datetime.utcnow() + expires_delta
5756
expire_seconds = expires_delta.total_seconds()
5857
else:
59-
expires = datetime.utcnow() + timedelta(seconds=settings.TOKEN_EXPIRE_MINUTES)
58+
expires = datetime.utcnow() + timedelta(seconds=settings.TOKEN_EXPIRE_SECONDS)
6059
expire_seconds = settings.TOKEN_EXPIRE_SECONDS
61-
to_encode = {'exp': expires, 'sub': str(sub), **data}
60+
to_encode = {'exp': expires, 'sub': str(sub), **kwargs}
6261
token = jwt.encode(to_encode, settings.TOKEN_SECRET_KEY, settings.TOKEN_ALGORITHM)
6362
if sub not in settings.TOKEN_WHITE_LIST:
6463
await redis_client.delete(f'token:{sub}:*')

backend/app/services/user_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def swagger_login(form_data: OAuth2PasswordRequestForm):
3232
# 获取最新用户信息
3333
user = await UserDao.get_user_by_id(db, current_user.id)
3434
# 创建token
35-
access_token = await jwt.create_access_token(user.id, {'role_ids': user_role_ids})
35+
access_token = await jwt.create_access_token(user.id, role_ids=user_role_ids)
3636
return access_token, user
3737

3838
@staticmethod
@@ -48,7 +48,7 @@ async def login(obj: Auth):
4848
await UserDao.update_user_login_time(db, obj.username)
4949
user_role_ids = await UserDao.get_user_role_ids(db, current_user.id)
5050
user = await UserDao.get_user_by_id(db, current_user.id)
51-
access_token = await jwt.create_access_token(user.id, {'role_ids': user_role_ids})
51+
access_token = await jwt.create_access_token(user.id, role_ids=user_role_ids)
5252
return access_token, user
5353

5454
@staticmethod

0 commit comments

Comments
 (0)