Skip to content

Instantly share code, notes, and snippets.

@gjyoung1974
Created September 23, 2018 16:52
Show Gist options
  • Save gjyoung1974/436a351c634c594bc76e4c08bfb0d6da to your computer and use it in GitHub Desktop.
Save gjyoung1974/436a351c634c594bc76e4c08bfb0d6da to your computer and use it in GitHub Desktop.
import requests
import json
# Get an access token from Auth0
def auth0_login():
login_data = {
'client_id': '...',
'client_secret': '...',
'grant_type': 'http://auth0.com/oauth/grant-type/password-realm',
'username': '[email protected]',
'password': 'SomePassword',
'scope': 'openid',
'realm': 'Username-Password-Authentication'
}
login_headers = {'Content-Type': 'application/json'}
token_response = requests.post(
'https://tenant.auth0.com/oauth/token', headers=login_headers, data=json.dumps(login_data)
)
return token_response
# get a token From KeyCloak
def keycloak_token_exchange(auth0_access_token):
data = {
'grant_type': 'urn:ietf:params:oauth:grant-type:token-exchange',
'subject_token': auth0_access_token,
'subject_issuer': 'oidc',
'subject_token_type': 'urn:ietf:params:oauth:token-type:access_token',
'audience': 'dashboard'
}
response = requests.post(
keycloak_url('token'), data=data, auth=keycloak_auth()
)
return response
def keycloak_url(action):
return '{}/realms/{}/protocol/openid-connect/{}'.format('https://auth.somewebsite.com/auth', 'someRealm', action)
def keycloak_auth():
return 'someClientID', 'SomeClientSecret'
foo = auth0_login()
print foo.content
foo2 = keycloak_token_exchange(foo)
print foo2.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment