-
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.
Restructure code for smaller files (#12)
* Restructure code for smaller files * missed lint
- Loading branch information
1 parent
4795461
commit c0a4194
Showing
13 changed files
with
492 additions
and
351 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
|
||
import requests | ||
from fastapi import ( | ||
Depends, | ||
HTTPException, | ||
status, | ||
) | ||
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBearer | ||
|
||
get_bearer_token = HTTPBearer(auto_error=True) | ||
|
||
oidc_user_info_endpoint = os.environ.get("OIDC_USER_INFO_ENDPOINT") | ||
dev = False | ||
|
||
env_value = os.environ.get("FASTAPI_APP_ENV") | ||
|
||
if env_value and env_value == "development": | ||
print("RUNNING IN DEV MODE") | ||
dev = True | ||
|
||
|
||
async def get_current_user( | ||
auth: HTTPAuthorizationCredentials = Depends(get_bearer_token), | ||
): | ||
|
||
if auth is None: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid user token", | ||
) | ||
|
||
if dev: | ||
return auth.credentials | ||
|
||
if oidc_user_info_endpoint is None: | ||
raise HTTPException( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
detail="User info endpoint error", | ||
) | ||
|
||
response = requests.get( | ||
url=oidc_user_info_endpoint, | ||
headers={"Authorization": f"Bearer {auth.credentials}"}, | ||
) | ||
|
||
if response.status_code == 401: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid user token", | ||
) | ||
|
||
return response.json()["id"] |
Oops, something went wrong.