Skip to content

Instantly share code, notes, and snippets.

@BenjamenMeyer
Created June 6, 2016 22:14
Show Gist options
  • Save BenjamenMeyer/4b25fb4cae71a78bf068019d38d11005 to your computer and use it in GitHub Desktop.
Save BenjamenMeyer/4b25fb4cae71a78bf068019d38d11005 to your computer and use it in GitHub Desktop.
Example Mimic Token Validation
#!/bin/bash
import json
import requests
def get_creds(ad):
resp = requests.post(
'http://localhost:8900/identity/v2.0/tokens',
data=json.dumps(ad)
)
if resp.status_code == 200:
return resp.json()
else:
raise RuntimeError('Failed to get token from Mimic Identity: {0} => {1}'.format(resp.status_code, resp.content))
def validate_token(service_data, user_data):
service_token = service_data['access']['token']['id']
token_to_validate = user_data['access']['token']['id']
tenant_id_to_validate = user_data['access']['token']['tenant']['id']
resp = requests.get(
'http://localhost:8900/identity/v2.0/tokens/{0}'.format(token_to_validate),
headers={
'x-auth-token': service_token
}
)
if resp.status_code == 200:
auth_data = resp.json()
returned_token = auth_data['access']['token']['id']
if returned_token != token_to_validate:
raise RuntimeError('Mimic failed to match up the token: {0} != {1}\nUser Data: {2}\nService Data: {3}\nValidation Data: {4}'.format(returned_token, token_to_validate, user_data, service_data, auth_data))
returned_tenant_id = auth_data['access']['token']['tenant']['id']
if returned_tenant_id != tenant_id_to_validate:
raise RuntimeError('Mimic failed to match up the tenant-id: {0} != {1}\nUser Data: {2}\nService Data: {3}\nValidation Data: {4}'.format(returned_tenant_id, tenant_id_to_validate, user_data, service_data, auth_data))
return auth_data
else:
raise RuntimeError('Failed to get token validation info: {0} => {1}'.format(resp.status_code, resp.content))
# 1. Get an Auth Token from Mimic
auth_data_user = {
"auth": {
"RAX-KSKEY:apiKeyCredentials": {
"username": "validator",
"apiKey": "stoplight"
}
}
}
auth_data_service = {
"auth": {
"RAX-KSKEY:apiKeyCredentials": {
"username": "myMimoServer",
"apiKey": "the-terminator"
}
}
}
authed_user = get_creds(auth_data_user)
authed_service = get_creds(auth_data_service)
# 2. Try to validate the user's token via the service
# This fails with the tenantid's not matching.
validated_user_info = validate_token(authed_service, authed_user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment