diff --git a/backend/endpoints/oauth.py b/backend/endpoints/oauth.py index 9fa4d7c3c..ab0d84867 100644 --- a/backend/endpoints/oauth.py +++ b/backend/endpoints/oauth.py @@ -36,7 +36,7 @@ async def token(form_data: Annotated[OAuth2RequestForm, Depends()]): access_token = create_oauth_token( data={ "sub": user.username, - "scope": payload.get("scope"), + "scopes": payload.get("scopes"), "type": "access", }, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES), @@ -81,7 +81,7 @@ async def token(form_data: Annotated[OAuth2RequestForm, Depends()]): access_token = create_oauth_token( data={ "sub": user.username, - "scope": form_data.scopes.join(" "), + "scopes": " ".join(form_data.scopes), "type": "access", }, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES), @@ -90,7 +90,7 @@ async def token(form_data: Annotated[OAuth2RequestForm, Depends()]): refresh_token = create_oauth_token( data={ "sub": user.username, - "scope": form_data.scopes.join(" "), + "scopes": " ".join(form_data.scopes), "type": "refresh", }, expires_delta=timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS), diff --git a/backend/main.py b/backend/main.py index c0924df69..edd71ab67 100644 --- a/backend/main.py +++ b/backend/main.py @@ -8,7 +8,7 @@ from fastapi_pagination import add_pagination from starlette.middleware.authentication import AuthenticationMiddleware from starlette.middleware.sessions import SessionMiddleware -from config import DEV_PORT, DEV_HOST +from config import DEV_PORT, DEV_HOST, ROMM_AUTH_SECRET_KEY from endpoints import search, platform, rom, identity, oauth, scan # noqa from utils.socket import socket_app from utils.auth import BasicAuthBackend, CustomCSRFMiddleware, create_default_admin_user @@ -34,7 +34,7 @@ app.add_middleware( ) app.add_middleware( CustomCSRFMiddleware, - secret=secrets.token_hex(32), + secret=ROMM_AUTH_SECRET_KEY, exempt_urls=[re.compile(r"^/token.*"), re.compile(r"^/ws")], ) diff --git a/backend/utils/auth.py b/backend/utils/auth.py index fb6e819e0..3f5959280 100644 --- a/backend/utils/auth.py +++ b/backend/utils/auth.py @@ -98,8 +98,8 @@ class BasicAuthBackend(AuthenticationBackend): return None # Only grant access to resources with overlapping scopes - token_scopres = set(list(payload.get("scope"))) - overlapping_scopes = list(token_scopres & set(user.oauth_scopes)) + token_scopes = set(list(payload.get("scopes").split(" "))) + overlapping_scopes = list(token_scopes & set(user.oauth_scopes)) return (AuthCredentials(overlapping_scopes), user) diff --git a/backend/utils/oauth.py b/backend/utils/oauth.py index d5e7e75c7..911d75465 100644 --- a/backend/utils/oauth.py +++ b/backend/utils/oauth.py @@ -55,9 +55,9 @@ async def get_current_active_user_from_token(token: str): try: payload = jwt.decode(token, ROMM_AUTH_SECRET_KEY, algorithms=[ALGORITHM]) - except (JWTError): + except JWTError: raise credentials_exception - + username: str = payload.get("sub") if username is None: raise credentials_exception @@ -96,6 +96,7 @@ class OAuth2RequestForm: oauth2_password_bearer = OAuth2PasswordBearer( tokenUrl="/token", + auto_error=False, scopes={ **DEFAULT_SCOPES_MAP, **WRITE_SCOPES_MAP,