generated from MetroStar/opensource-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bearer token endpoint example to project. Updated configs and rea…
…dme for oidc provider.
- Loading branch information
Showing
7 changed files
with
67 additions
and
0 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
Empty file.
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,21 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends | ||
from fastapi.security import HTTPBearer | ||
from starlette import status | ||
|
||
from app.auth import validate_jwt | ||
|
||
router = APIRouter( | ||
prefix="/api", | ||
tags=["Admin"], | ||
) | ||
|
||
|
||
@router.get( | ||
"/current-user", | ||
dependencies=[Depends(HTTPBearer())], | ||
status_code=status.HTTP_200_OK, | ||
) | ||
async def get_current_user(current_user: Annotated[dict, Depends(validate_jwt)]): | ||
return {"user": current_user} |
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,40 @@ | ||
import requests | ||
from fastapi import Depends, HTTPException | ||
from jose.backends import RSAKey | ||
from jose.jwt import decode, get_unverified_header | ||
|
||
from app.config import settings | ||
|
||
|
||
def get_keycloak_jwks(): | ||
keycloak_well_known_url = settings.OIDC_CONFIG_URL | ||
response = requests.get(keycloak_well_known_url) | ||
well_known_config = response.json() | ||
jwks_url = well_known_config["jwks_uri"] | ||
jwks_response = requests.get(jwks_url) | ||
jwks = jwks_response.json() | ||
return jwks["keys"] | ||
|
||
|
||
def validate_jwt(token: str = Depends(get_keycloak_jwks)): | ||
header = get_unverified_header(token) | ||
jwks = get_keycloak_jwks() | ||
|
||
# Find the RSA key with the matching kid in the JWKS | ||
rsa_key = None | ||
for key in jwks: | ||
if key["kid"] == header["kid"]: | ||
rsa_key = RSAKey( | ||
key=key["n"], alg=key["alg"], use=key["use"], kid=key["kid"] | ||
) | ||
break | ||
|
||
if rsa_key is None: | ||
raise HTTPException(status_code=401, detail="RSA Key not found in JWKS") | ||
|
||
try: | ||
payload = decode(token, rsa_key, algorithms=[header["alg"]]) | ||
except Exception: | ||
raise HTTPException(status_code=401, detail="Invalid JWT token") from Exception | ||
|
||
return payload |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
class Settings(BaseSettings): | ||
DATABASE_URL: str | ||
OIDC_CONFIG_URL: str | ||
|
||
class Config: | ||
env_file = ".env" | ||
|
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