Skip to content

Commit

Permalink
[fix] Fix authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
georgejkaye committed Mar 17, 2024
1 parent 72fd80e commit 19aa927
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions api/src/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,22 @@ def create_access_token(data: dict, expires_delta: timedelta | None = None):
async def validate_token(
token: Annotated[Optional[str], Depends(oauth2_scheme)]
) -> Optional[bool]:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
if token:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
jwt.decode(token, get_secret("SECRET_KEY"), algorithms=[ALGORITHM])
payload = jwt.decode(
token, get_secret("SECRET_KEY"), algorithms=[ALGORITHM]
)
user = payload.get("sub")
if user is None:
raise credentials_exception
if not user == get_env_variable("ADMIN_USER")
raise credentials_exception
return True
except JWTError:
raise credentials_exception
return None
return credentials_exception

0 comments on commit 19aa927

Please sign in to comment.