-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from christianstubbe/feature-jwt
Feature: Add handling of JWT
- Loading branch information
Showing
7 changed files
with
44 additions
and
30 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,24 @@ | ||
from fastapi import Request, HTTPException | ||
from jose import jwt, JWTError | ||
import requests | ||
import os | ||
|
||
|
||
project_nb = os.getenv("GCP_PROJECT_NB") | ||
project_name = os.getenv("GCP_PROJECT_NAME") | ||
AUDIENCE = f"/projects/{project_nb}/apps/{project_name}" | ||
GOOGLE_CERTS_URL = "https://www.googleapis.com/oauth2/v1/certs" | ||
|
||
|
||
async def iap_jwt_middleware(request: Request, call_next): | ||
token = request.headers.get('x-goog-iap-jwt-assertion') | ||
if token: | ||
try: | ||
certs = requests.get(GOOGLE_CERTS_URL).json() | ||
payload = jwt.decode(token, certs, algorithms=['RS256'], audience=AUDIENCE) | ||
request.state.user = payload.get("email") | ||
except JWTError as e: | ||
raise HTTPException(status_code=401, detail="Invalid token") | ||
response = await call_next(request) | ||
return response | ||
|
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
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