refactor: refactor project with ruff
This commit is contained in:
@@ -24,7 +24,7 @@ from api.config import get_settings
|
||||
from api.db.connection.session import get_connection_dep
|
||||
from api.services.auth import authenticate_user
|
||||
|
||||
from api.db.logic.auth import add_new_refresh_token,upgrade_old_refresh_token
|
||||
from api.db.logic.auth import add_new_refresh_token, upgrade_old_refresh_token
|
||||
|
||||
from api.schemas.endpoints.auth import Auth
|
||||
|
||||
@@ -54,12 +54,11 @@ def get_config():
|
||||
|
||||
@api_router.post("")
|
||||
async def login_for_access_token(
|
||||
user: Auth,
|
||||
response: Response,
|
||||
connection: AsyncConnection = Depends(get_connection_dep),
|
||||
Authorize: AuthJWT = Depends(),
|
||||
):
|
||||
|
||||
user: Auth,
|
||||
response: Response,
|
||||
connection: AsyncConnection = Depends(get_connection_dep),
|
||||
Authorize: AuthJWT = Depends(),
|
||||
):
|
||||
"""Авторизирует, выставляет токены в куки."""
|
||||
|
||||
user = await authenticate_user(connection, user.login, user.password)
|
||||
@@ -73,44 +72,33 @@ async def login_for_access_token(
|
||||
# headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
access_token_expires = timedelta(
|
||||
minutes=get_settings().ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
access_token_expires = timedelta(minutes=get_settings().ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
|
||||
refresh_token_expires = timedelta(
|
||||
days=get_settings().REFRESH_TOKEN_EXPIRE_DAYS
|
||||
)
|
||||
refresh_token_expires = timedelta(days=get_settings().REFRESH_TOKEN_EXPIRE_DAYS)
|
||||
|
||||
logger.debug(f"refresh_token_expires {refresh_token_expires}")
|
||||
|
||||
access_token = Authorize.create_access_token(
|
||||
subject=user.login, expires_time=access_token_expires
|
||||
)
|
||||
refresh_token = Authorize.create_refresh_token(
|
||||
subject=user.login, expires_time=refresh_token_expires
|
||||
)
|
||||
access_token = Authorize.create_access_token(subject=user.login, expires_time=access_token_expires)
|
||||
refresh_token = Authorize.create_refresh_token(subject=user.login, expires_time=refresh_token_expires)
|
||||
|
||||
refresh_token_expires_time = datetime.now(timezone.utc) + refresh_token_expires
|
||||
|
||||
await add_new_refresh_token(connection,refresh_token,refresh_token_expires_time,user)
|
||||
await add_new_refresh_token(connection, refresh_token, refresh_token_expires_time, user)
|
||||
|
||||
Authorize.set_refresh_cookies(refresh_token)
|
||||
|
||||
|
||||
return {
|
||||
"access_token": access_token,
|
||||
# "access_token_expires": access_token_expires_time,
|
||||
# "refresh_token": refresh_token,
|
||||
# "refresh_token_expires": refresh_token_expires_time
|
||||
}
|
||||
"access_token": access_token,
|
||||
# "access_token_expires": access_token_expires_time,
|
||||
# "refresh_token": refresh_token,
|
||||
# "refresh_token_expires": refresh_token_expires_time
|
||||
}
|
||||
|
||||
|
||||
@api_router.post("/refresh")
|
||||
async def refresh(
|
||||
request: Request,
|
||||
connection: AsyncConnection = Depends(get_connection_dep),
|
||||
Authorize: AuthJWT = Depends()
|
||||
):
|
||||
|
||||
request: Request, connection: AsyncConnection = Depends(get_connection_dep), Authorize: AuthJWT = Depends()
|
||||
):
|
||||
refresh_token = request.cookies.get("refresh_token_cookie")
|
||||
# print("Refresh Token:", refresh_token)
|
||||
|
||||
@@ -118,29 +106,24 @@ async def refresh(
|
||||
raise HTTPException(status_code=401, detail="Refresh token is missing")
|
||||
|
||||
try:
|
||||
|
||||
Authorize.jwt_refresh_token_required()
|
||||
current_user = Authorize.get_jwt_subject()
|
||||
|
||||
|
||||
except Exception as e:
|
||||
|
||||
await upgrade_old_refresh_token(connection,current_user,refresh_token)
|
||||
await upgrade_old_refresh_token(connection, current_user, refresh_token)
|
||||
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid refresh token",
|
||||
)
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid refresh token",
|
||||
)
|
||||
|
||||
access_token_expires = timedelta(minutes=get_settings().ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
|
||||
new_access_token = Authorize.create_access_token(
|
||||
subject=current_user, expires_time=access_token_expires
|
||||
)
|
||||
new_access_token = Authorize.create_access_token(subject=current_user, expires_time=access_token_expires)
|
||||
|
||||
return {
|
||||
"access_token": new_access_token,
|
||||
# "access_token_expires": access_token_expires_time,
|
||||
# "refresh_token": refresh_token,
|
||||
# "refresh_token_expires": refresh_token_expires_time
|
||||
}
|
||||
"access_token": new_access_token,
|
||||
# "access_token_expires": access_token_expires_time,
|
||||
# "refresh_token": refresh_token,
|
||||
# "refresh_token_expires": refresh_token_expires_time
|
||||
}
|
||||
|
Reference in New Issue
Block a user