Пример #1
0
def create_user_session(
        response: Response,
        access_code_user: User = Depends(access_code_user),
        jwt_service: JWTService = Depends(jwt_service),
        session_service: SessionService = Depends(session_service),
        settings: Settings = Depends(get_settings),
):
    session_id = session_service.generate_session(access_code_user.id)
    refresh_token_payload = RefreshTokenPayload.from_info(
        settings.SESSION_EXPIRATION_SECONDS,
        session_id,
    )
    refresh_token = jwt_service.generate_token(refresh_token_payload.dict())
    access_token_payload = AccessTokenPayload.from_info(
        settings.ACCESS_TOKEN_EXPIRATION_SECONDS,
        session_id,
        access_code_user,
    )
    access_token = jwt_service.generate_token(access_token_payload.dict())
    response.set_cookie(
        key="refresh_token",
        value=refresh_token,
        httponly=True,
        expires=settings.SESSION_EXPIRATION_SECONDS,
    )
    return AccessToken(access_token=access_token)
Пример #2
0
def get_fresh_token(
        jwt_service: JWTService = Depends(jwt_service),
        refresh_token: RefreshTokenPayload = Depends(refresh_token),
        session_service: SessionService = Depends(session_service),
        settings: Settings = Depends(get_settings),
        users_repository: UsersRepository = Depends(users_repository),
):
    user_id = session_service.verify_session(refresh_token.jti)
    if not user_id:
        raise_unauthorized("Invalid session")
    user = find_user_by_id(user_id, users_repository)
    payload = AccessTokenPayload.from_info(
        settings.ACCESS_TOKEN_EXPIRATION_SECONDS,
        refresh_token.jti,
        user,
    )
    token = jwt_service.generate_token(payload.dict())
    return AccessToken(access_token=token)