Fix bad jwt tokens

This commit is contained in:
Georges-Antoine Assi
2023-08-12 22:15:24 -04:00
parent 835065f642
commit 71709072de
4 changed files with 10 additions and 9 deletions

View File

@@ -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),

View File

@@ -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")],
)

View File

@@ -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)

View File

@@ -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,