-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add optional JWT auth, add basic logger, rename endpoints
- Loading branch information
1 parent
c03a9ec
commit 30d09b3
Showing
4 changed files
with
62 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
from datetime import datetime, timezone | ||
from fastapi import Request, HTTPException | ||
from jose import jwt, JWTError | ||
from config import logger | ||
|
||
async def security_middleware(request: Request, call_next): | ||
async def next(): | ||
response = await call_next(request) | ||
return response | ||
|
||
if request.url.path == "/docs" or request.url.path == "/openapi.json": | ||
return await next() | ||
|
||
jwt_secret = os.getenv('JWT_SECRET') | ||
|
||
if jwt_secret: | ||
authorization = request.headers.get('Authorization') | ||
if not authorization or not authorization.startswith('Bearer '): | ||
raise HTTPException(status_code=401, detail="Missing or invalid Authorization header") | ||
|
||
token = authorization.split(' ')[1] | ||
try: | ||
payload = jwt.decode(token, jwt_secret, algorithms=['HS256']) | ||
|
||
# Check if the token has expired | ||
exp_timestamp = payload.get('exp') | ||
if exp_timestamp: | ||
exp_datetime = datetime.fromtimestamp(exp_timestamp, tz=timezone.utc) | ||
current_datetime = datetime.now(tz=timezone.utc) | ||
if current_datetime > exp_datetime: | ||
raise HTTPException(status_code=401, detail="Token has expired") | ||
|
||
request.state.user = payload | ||
logger.debug(f"{request.url.path} - {payload}") | ||
except JWTError as e: | ||
raise HTTPException(status_code=401, detail=f"Invalid token: {str(e)}") | ||
else: | ||
logger.warn("JWT_SECRET not found in environment variables") | ||
|
||
return await next() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ pandas==2.2.1 | |
openpyxl==3.1.2 | ||
docx2txt==0.8 | ||
pypandoc==1.13 | ||
python-jose==3.3.0 |