check roles user

This commit is contained in:
Valentin CZERYBA 2023-10-20 23:38:12 +02:00
parent 6e7970d5e3
commit 68c578c449
2 changed files with 3 additions and 4 deletions

View File

@ -23,7 +23,6 @@ class OAuth2PasswordBearerWithCookie(OAuth2):
async def __call__(self, request: Request) -> Optional[str]: async def __call__(self, request: Request) -> Optional[str]:
authorization: str = request.cookies.get("access_token") #changed to accept access token from httpOnly Cookie authorization: str = request.cookies.get("access_token") #changed to accept access token from httpOnly Cookie
print("access_token is",authorization)
scheme, param = get_authorization_scheme_param(authorization) scheme, param = get_authorization_scheme_param(authorization)
if not authorization or scheme.lower() != "bearer": if not authorization or scheme.lower() != "bearer":

View File

@ -7,13 +7,13 @@ from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from ..dependencies import users_token, permissions_checker from ..dependencies import users_token, permissions_checker
from ..models import token, users from ..models import token, users
router = APIRouter() router = APIRouter()
ACCESS_TOKEN_EXPIRE_MINUTES = 30 ACCESS_TOKEN_EXPIRE_MINUTES = 30
@router.post("/token", tags=["token"]) @router.post("/token", tags=["token"])
async def login_for_access_token( async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()] form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
):
user = users_token.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(
@ -25,7 +25,7 @@ async def login_for_access_token(
access_token = users_token.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
) )
content = {"message": "Access token generated"} content = {"roles":user.roles,"message": "Access token generated"}
response = JSONResponse(content=content) response = JSONResponse(content=content)
response.set_cookie(key="access_token", value="Bearer {0}".format(access_token), httponly=True) response.set_cookie(key="access_token", value="Bearer {0}".format(access_token), httponly=True)
return response return response