Download src/middlewares/authenticate.py from kbl2810/test: direct link, hf CLI and curl.
- Browser
- Download file 675 Bytes
-
https://huggingface.co/spaces/kbl2810/test/resolve/main/src/middlewares/authenticate.py
- Command line
-
hf download hf://spaces/kbl2810/test/src/middlewares/authenticate.py
-
curl -L -o authenticate.py https://huggingface.co/spaces/kbl2810/test/resolve/main/src/middlewares/authenticate.py
675 Bytes
| from fastapi import Request, HTTPException, Depends | |
| import jwt | |
| from env import config | |
| async def authenticate(request: Request): | |
| if config["app"]['ignore_authen']: | |
| return | |
| auth_header = request.headers.get("Authorization") | |
| if not auth_header or not auth_header.startswith("Bearer"): | |
| raise HTTPException(status_code=401, detail="token_invalid") | |
| token = auth_header.split("Bearer")[1].strip() | |
| payload = jwt.decode(token, config["jwt"]["secret_key"], algorithm=config["jwt"]["algorithm"]) | |
| server_id = payload.get("id") | |
| if server_id != config["app"]["server_id"]: | |
| raise HTTPException(status_code=401, detail="token_invalid") | |