rename users_active to users_token

This commit is contained in:
Valentin CZERYBA 2023-10-14 17:34:34 +02:00
parent f8f2c83d6d
commit f2e7ca1e93
4 changed files with 7 additions and 7 deletions

View File

@ -1,4 +1,4 @@
from ..dependencies import users_active from ..dependencies import users_token
from fastapi import Depends, HTTPException, status from fastapi import Depends, HTTPException, status
from ..models import users from ..models import users
@ -8,7 +8,7 @@ class PermissionChecker:
def __init__(self, roles: list[str]) -> None: def __init__(self, roles: list[str]) -> None:
self.roles = roles 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: for role in self.roles:
if role == user.roles: if role == user.roles:
return True return True

View File

@ -3,7 +3,7 @@ from datetime import datetime, timedelta
from typing import Annotated from typing import Annotated
from fastapi import Depends, FastAPI, HTTPException, status, APIRouter from fastapi import Depends, FastAPI, HTTPException, status, APIRouter
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from ..dependencies import users_active from ..dependencies import users_token
from ..models import token from ..models import token
router = APIRouter() router = APIRouter()
@ -13,7 +13,7 @@ ACCESS_TOKEN_EXPIRE_MINUTES = 30
async def login_for_access_token( async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()] 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: if not user:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
@ -21,7 +21,7 @@ async def login_for_access_token(
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) 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 data={"sub": user.username}, expires_delta=access_token_expires
) )
return {"access_token": access_token, "token_type": "bearer"} return {"access_token": access_token, "token_type": "bearer"}

View File

@ -1,5 +1,5 @@
from fastapi import APIRouter, Depends, HTTPException, status 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 ..models import users
from typing import Annotated from typing import Annotated
from bson import ObjectId 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"]) @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 return current_user
@router.get("/users/{item_id}", tags=["users"], response_model=users.User) @router.get("/users/{item_id}", tags=["users"], response_model=users.User)