-
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.
- Loading branch information
Showing
8 changed files
with
49 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,7 @@ | ||
from fastapi import HTTPException, status, Security, Depends | ||
from fastapi.security import OAuth2AuthorizationCodeBearer | ||
from keycloak import KeycloakOpenID | ||
from api.config import config | ||
from api.models.auth import User | ||
from enacit4r_auth.services.auth import KeycloakService, User | ||
|
||
|
||
# This is used for fastapi docs authentification | ||
oauth2_scheme = OAuth2AuthorizationCodeBearer( | ||
authorizationUrl=f"{config.KEYCLOAK_URL}", | ||
tokenUrl=( | ||
f"{config.KEYCLOAK_URL}/realms/{config.KEYCLOAK_REALM}" | ||
"/protocol/openid-connect/token" | ||
), | ||
) | ||
|
||
|
||
keycloak_openid = KeycloakOpenID( | ||
server_url=config.KEYCLOAK_URL, | ||
client_id=config.KEYCLOAK_API_ID, | ||
client_secret_key=config.KEYCLOAK_API_SECRET, | ||
realm_name=config.KEYCLOAK_REALM, | ||
verify=True, | ||
) | ||
|
||
# Get the payload/token from keycloak | ||
|
||
|
||
async def get_payload(token: str = Security(oauth2_scheme)) -> dict: | ||
try: | ||
return keycloak_openid.decode_token( | ||
token, | ||
) | ||
except Exception as e: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail=str(e), # "Invalid authentication credentials", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
|
||
|
||
# Get user infos from the payload | ||
async def get_user_info(payload: dict = Depends(get_payload)) -> User: | ||
try: | ||
return User( | ||
id=payload.get("sub"), | ||
username=payload.get("preferred_username"), | ||
email=payload.get("email"), | ||
first_name=payload.get("given_name"), | ||
last_name=payload.get("family_name"), | ||
realm_roles=payload.get("realm_access", {}).get("roles", []), | ||
client_roles=payload.get("realm_access", {}).get("roles", []), | ||
) | ||
except Exception as e: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail=str(e), # "Invalid authentication credentials", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
|
||
|
||
async def require_admin(user: User = Depends(get_user_info)): | ||
if "icebreaker-admin" not in user.realm_roles: | ||
raise HTTPException( | ||
status_code=status.HTTP_403_FORBIDDEN, | ||
detail="You are not authorised to perform this operation", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
return user | ||
kc_service = KeycloakService(url=config.KEYCLOAK_URL, realm=config.KEYCLOAK_REALM, | ||
client_id=config.KEYCLOAK_API_ID, client_secret=config.KEYCLOAK_API_SECRET, | ||
admin_role="icebreaker-admin") |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
from api.config import config | ||
from enacit4r.services.s3 import S3Service | ||
from enacit4r_files.services.s3 import S3Service | ||
|
||
s3_client = S3Service(config.S3_ENDPOINT_PROTOCOL + config.S3_ENDPOINT_HOSTNAME, | ||
config.S3_ACCESS_KEY_ID, | ||
config.S3_SECRET_ACCESS_KEY,config.S3_REGION, | ||
config.S3_BUCKET, | ||
config.S3_PATH_PREFIX) | ||
config.S3_ACCESS_KEY_ID, | ||
config.S3_SECRET_ACCESS_KEY, config.S3_REGION, | ||
config.S3_BUCKET, | ||
config.S3_PATH_PREFIX) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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