Skip to content

Commit 19aa927

Browse files
committed
[fix] Fix authentication
1 parent 72fd80e commit 19aa927

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

api/src/api/auth.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,22 @@ def create_access_token(data: dict, expires_delta: timedelta | None = None):
7676
async def validate_token(
7777
token: Annotated[Optional[str], Depends(oauth2_scheme)]
7878
) -> Optional[bool]:
79+
credentials_exception = HTTPException(
80+
status_code=status.HTTP_401_UNAUTHORIZED,
81+
detail="Could not validate credentials",
82+
headers={"WWW-Authenticate": "Bearer"},
83+
)
7984
if token:
80-
credentials_exception = HTTPException(
81-
status_code=status.HTTP_401_UNAUTHORIZED,
82-
detail="Could not validate credentials",
83-
headers={"WWW-Authenticate": "Bearer"},
84-
)
8585
try:
86-
jwt.decode(token, get_secret("SECRET_KEY"), algorithms=[ALGORITHM])
86+
payload = jwt.decode(
87+
token, get_secret("SECRET_KEY"), algorithms=[ALGORITHM]
88+
)
89+
user = payload.get("sub")
90+
if user is None:
91+
raise credentials_exception
92+
if not user == get_env_variable("ADMIN_USER")
93+
raise credentials_exception
8794
return True
8895
except JWTError:
8996
raise credentials_exception
90-
return None
97+
return credentials_exception

0 commit comments

Comments
 (0)