Open devtj3 opened 1 year ago
To get the OIDC public key for token validation, you generally fetch it from the identity provider's JWKS (JSON Web Key Set) URL. The JWKS URL is often found in the OIDC discovery document, which is usually located at an endpoint like https://<identity_provider_domain>/.well-known/openid-configuration
.
Here's an example using Python to fetch the public key:
Install the required package:
pip install requests
Fetch the public key:
import requests
import json
# Fetch OIDC configuration
oidc_config_url = 'https://<identity_provider_domain>/.well-known/openid-configuration'
response = requests.get(oidc_config_url)
config = response.json()
# Fetch JWKS
jwks_url = config['jwks_uri']
response = requests.get(jwks_url)
jwks = response.json()
# Extract RSA public keys
keys = {}
for key_data in jwks['keys']:
if key_data['kty'] == 'RSA' and 'kid' in key_data:
kid = key_data['kid']
keys[kid] = key_data
# Now `keys` contains the public keys, indexed by 'kid' (Key ID)
Once you have the public keys, you can use them to validate incoming JWTs by matching the 'kid' in the JWT header to one of the public keys you've fetched.
Basic sample
Certainly, here's a Python script that uses the OIDC (OpenID Connect) flow to authenticate against a BeyondTrust (BT) API, assuming that BT supports OIDC. The script uses the
requests
library to handle HTTP interactions.First, install the
requests
library:Here's the script:
Replace placeholder values like
your-oidc-provider.com
,your-client-id
, andyour-client-secret
with your actual OIDC configuration. Run the script after these changes.This script first obtains an OIDC token and then uses that token to authenticate against the BeyondTrust API to retrieve secrets.
AWS Sample
If the BeyondTrust (BT) API doesn't support OIDC yet, then you'd have to modify the API server to include OIDC authentication. Normally, you'd need access to the source code of the API to add such functionality.
However, if you don't have that access, you can create a "proxy" service that stands between the client and the BT API. The proxy would handle OIDC authentication and forward valid requests to the BT API.
Here is a simplified example using Python and Flask to build such a proxy:
First, install the required packages:
Here's the Python code:
Replace
"Your OIDC Public Key Here"
and'Your BT API credentials'
with your actual OIDC public key and BT API credentials.This Flask application acts as a proxy, validating incoming requests for a valid OIDC token and then forwarding them to the actual BT API. Run this Flask app, and your clients should make API calls to this service instead of directly to the BT API. This way, you're adding OIDC support "in front of" the existing API.