From 68c578c449c7aea601c410adb8e1b2aad82a4909 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 20 Oct 2023 23:38:12 +0200 Subject: [PATCH] check roles user --- app/dependencies/cookie.py | 1 - app/routers/token.py | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/dependencies/cookie.py b/app/dependencies/cookie.py index ed78336..2078dac 100644 --- a/app/dependencies/cookie.py +++ b/app/dependencies/cookie.py @@ -23,7 +23,6 @@ class OAuth2PasswordBearerWithCookie(OAuth2): async def __call__(self, request: Request) -> Optional[str]: 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) if not authorization or scheme.lower() != "bearer": diff --git a/app/routers/token.py b/app/routers/token.py index 8b13860..037f6e6 100644 --- a/app/routers/token.py +++ b/app/routers/token.py @@ -7,13 +7,13 @@ from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from ..dependencies import users_token, permissions_checker from ..models import token, users + router = APIRouter() ACCESS_TOKEN_EXPIRE_MINUTES = 30 @router.post("/token", tags=["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) if not user: raise HTTPException( @@ -25,7 +25,7 @@ async def login_for_access_token( access_token = users_token.create_access_token( 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.set_cookie(key="access_token", value="Bearer {0}".format(access_token), httponly=True) return response