-
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.
Merge pull request #107 from bento-platform/feat/authorized-katsu-calls
feat: beacon client-authorized requests to private data endpoints
- Loading branch information
Showing
6 changed files
with
136 additions
and
25 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,68 @@ | ||
import functools | ||
import requests | ||
from flask import current_app | ||
|
||
from .headers import auth_header_from_request | ||
|
||
__all__ = [ | ||
"get_access_token", | ||
"create_access_header_or_fall_back", | ||
] | ||
|
||
|
||
@functools.cache | ||
def get_token_endpoint_from_openid_config_url(url: str, validate_ssl: bool = True): | ||
r = requests.get(url, verify=validate_ssl) | ||
if not r.ok: | ||
raise Exception(f"Received not-OK response from OIDC config URL: {r.status_code}") | ||
return r.json()["token_endpoint"] | ||
|
||
|
||
def get_access_token() -> str | None: | ||
logger = current_app.logger | ||
|
||
oidc_config_url = current_app.config["OPENID_CONFIG_URL"] | ||
client_id = current_app.config["CLIENT_ID"] | ||
client_secret = current_app.config["CLIENT_SECRET"] | ||
validate_ssl = current_app.config["BENTO_VALIDATE_SSL"] | ||
|
||
if not all((oidc_config_url, client_id, client_secret)): | ||
logger.error("Could not retrieve access token; one of OPENID_CONFIG_URL | CLIENT_ID | CLIENT_SECRET is not set") | ||
return None | ||
|
||
try: | ||
token_endpoint = get_token_endpoint_from_openid_config_url(oidc_config_url, validate_ssl=validate_ssl) | ||
except Exception as e: | ||
logger.error(f"Could not retrieve access token; got exception from OpenID config URL: {e}") | ||
return None | ||
|
||
token_res = requests.post( | ||
token_endpoint, | ||
verify=validate_ssl, | ||
data={ | ||
"grant_type": "client_credentials", | ||
"client_id": client_id, | ||
"client_secret": client_secret, | ||
}, | ||
) | ||
|
||
if not token_res.ok: | ||
logger.error(f"Could not retrieve access token; got error response: {token_res.json()}") | ||
return None | ||
|
||
return token_res.json()["access_token"] | ||
|
||
|
||
def create_access_header_or_fall_back(): | ||
logger = current_app.logger | ||
|
||
if not current_app.config["AUTHZ_BENTO_REQUESTS_ENABLED"]: | ||
logger.warning("AUTHZ_BENTO_REQUESTS_ENABLED is false; falling back to request headers") | ||
return auth_header_from_request() | ||
|
||
access_token = get_access_token() | ||
if access_token is None: | ||
logger.error("create_access_header_or_fall_back: falling back to request headers") | ||
return auth_header_from_request() | ||
else: | ||
return {"Authorization": f"Bearer {access_token}"} |
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