Skip to content

Commit

Permalink
Adapt to JWT user instance
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan committed Aug 16, 2024
1 parent 0021109 commit 0ee4c14
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/app/admin/api/v1/sys/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def password_reset(request: Request, obj: ResetPasswordParam) -> ResponseM

@router.get('/me', summary='获取当前用户信息', dependencies=[DependsJwtAuth], response_model_exclude={'password'})
async def get_current_user(request: Request) -> ResponseModel:
data = GetCurrentUserInfoDetail(**select_as_dict(request.user))
data = GetCurrentUserInfoDetail(**request.user.model_dump())
return response_base.success(data=data)


Expand Down
5 changes: 3 additions & 2 deletions backend/app/admin/service/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ async def add(*, request: Request, obj: AddUserParam) -> None:
@staticmethod
async def pwd_reset(*, request: Request, obj: ResetPasswordParam) -> int:
async with async_db_session.begin() as db:
if not password_verify(f'{obj.old_password}{request.user.salt}', request.user.password):
user = await user_dao.get(db, request.user.id)
if not password_verify(f'{obj.old_password}{user.salt}', user.password):
raise errors.ForbiddenError(msg='原密码错误')
np1 = obj.new_password
np2 = obj.confirm_password
if np1 != np2:
raise errors.ForbiddenError(msg='密码输入不一致')
new_pwd = get_hash_password(f'{obj.new_password}{request.user.salt}')
new_pwd = get_hash_password(f'{obj.new_password}{user.salt}')
count = await user_dao.reset_password(db, request.user.id, new_pwd)
key_prefix = [
f'{settings.TOKEN_REDIS_PREFIX}:{request.user.id}',
Expand Down
4 changes: 2 additions & 2 deletions backend/middleware/jwt_auth_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def auth_exception_handler(conn: HTTPConnection, exc: _AuthenticationError) -> R
"""覆盖内部认证错误处理"""
return MsgSpecJSONResponse(content={'code': exc.code, 'msg': exc.msg, 'data': None}, status_code=exc.code)

async def authenticate(self, request: Request):
async def authenticate(self, request: Request) -> tuple[AuthCredentials, CurrentUserIns] | None:
auth = request.headers.get('Authorization')
if not auth:
return
Expand All @@ -59,7 +59,7 @@ async def authenticate(self, request: Request):
user.model_dump_json(),
)
else:
# 在恰当的时机,应替换为使用 model_validate_json
# TODO: 在恰当的时机,应替换为使用 model_validate_json
# https://docs.pydantic.dev/latest/concepts/json/#partial-json-parsing
user = CurrentUserIns.model_validate(from_json(cache_user, allow_partial=True))
except TokenError as exc:
Expand Down

0 comments on commit 0ee4c14

Please sign in to comment.