diff --git a/app/dependencies/permissions_checker.py b/app/dependencies/permissions_checker.py index 896679f..9c5e414 100644 --- a/app/dependencies/permissions_checker.py +++ b/app/dependencies/permissions_checker.py @@ -1,4 +1,4 @@ -from ..dependencies import users_active +from ..dependencies import users_token from fastapi import Depends, HTTPException, status from ..models import users @@ -8,7 +8,7 @@ class PermissionChecker: def __init__(self, roles: list[str]) -> None: self.roles = roles - def __call__(self, user: users.User = Depends(users_active.get_current_active_user)) -> bool: + def __call__(self, user: users.User = Depends(users_token.get_current_active_user)) -> bool: for role in self.roles: if role == user.roles: return True diff --git a/app/dependencies/users_active.py b/app/dependencies/users_token.py similarity index 100% rename from app/dependencies/users_active.py rename to app/dependencies/users_token.py diff --git a/app/routers/token.py b/app/routers/token.py index 55ffdc4..7ef5419 100644 --- a/app/routers/token.py +++ b/app/routers/token.py @@ -3,7 +3,7 @@ from datetime import datetime, timedelta from typing import Annotated from fastapi import Depends, FastAPI, HTTPException, status, APIRouter from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm -from ..dependencies import users_active +from ..dependencies import users_token from ..models import token router = APIRouter() @@ -13,7 +13,7 @@ ACCESS_TOKEN_EXPIRE_MINUTES = 30 async def login_for_access_token( form_data: Annotated[OAuth2PasswordRequestForm, Depends()] ): - user = users_active.authenticate_user(form_data.username, form_data.password) + user = users_token.authenticate_user(form_data.username, form_data.password) if not user: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, @@ -21,7 +21,7 @@ async def login_for_access_token( headers={"WWW-Authenticate": "Bearer"}, ) access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) - access_token = users_active.create_access_token( + access_token = users_token.create_access_token( data={"sub": user.username}, expires_delta=access_token_expires ) return {"access_token": access_token, "token_type": "bearer"} \ No newline at end of file diff --git a/app/routers/users.py b/app/routers/users.py index f97970c..8b1b7be 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -1,5 +1,5 @@ from fastapi import APIRouter, Depends, HTTPException, status -from ..dependencies import users_active, permissions_checker, database +from ..dependencies import users_token, permissions_checker, database from ..models import users from typing import Annotated from bson import ObjectId @@ -43,7 +43,7 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P @router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["password"]) -async def read_users_me(current_user: Annotated[users.User, Depends(users_active.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): +async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): return current_user @router.get("/users/{item_id}", tags=["users"], response_model=users.User)