|
1 | | -"""Systemathics APIs Token Helpers |
| 1 | +"""Systemathics Ganymede APIs Token Helpers |
2 | 2 |
|
3 | | -This module helps to create tokens to access Systemathics authenticated APIs |
| 3 | +This module helps to create tokens to access Systemathics Ganymede authenticated APIs. |
4 | 4 |
|
5 | 5 | functions: |
6 | | - * get_token - get token by autodecting environment variables |
7 | | - * create_bearer_token - create a bearer token (used by get_token when AUTH0_TOKEN env variable is set) |
8 | | - * create_bearer_token_using_rest - create beared token using REST API (used by get_token when AUTH0_TOKEN env variable is not set and CLIENT_ID, CLIENT_SECRET, AUDIENCE and TENANT environment variables are set) |
| 6 | + * get_token - Get a JWT Authorization token suitable to call Ganymede gRPC APIs. |
9 | 7 | """ |
10 | 8 |
|
11 | 9 | import os |
12 | 10 | import http.client |
13 | 11 | import json |
14 | 12 |
|
| 13 | +DEFAULT_AUDIENCE = "https://prod.ganymede-prod" |
| 14 | + |
| 15 | +DEFAULT_TENANT = "ganymede-prod.eu.auth0.com" |
| 16 | + |
15 | 17 | def get_token() -> str: |
16 | | - auth0_token = os.getenv('AUTH0_TOKEN','') |
17 | | - client_id = os.getenv('CLIENT_ID','') |
18 | | - client_secret = os.getenv('CLIENT_SECRET','') |
19 | | - audience = os.getenv('AUDIENCE','') |
20 | | - tenant = os.getenv('TENANT','') |
| 18 | + """ |
| 19 | + Get a JWT Authorization token suitable to call Ganymede gRPC APIs. |
| 20 | + We either use 'AUTH0_TOKEN' environment variable (if present) to create a bearer token from it. |
| 21 | + Or 'CLIENT_ID' and 'CLIENT_SECRET' environment variables (optionally 'AUDIENCE' can override DEFAULT_AUDIENCE, and 'TENANT' can override DEFAULT_TENANT). |
| 22 | + Returns: |
| 23 | + A JWT Authorization token suitable to call Ganymede gRPC APIs. |
| 24 | + """ |
| 25 | + auth0_token = os.getenv("AUTH0_TOKEN","") |
| 26 | + client_id = os.getenv("CLIENT_ID","") |
| 27 | + client_secret = os.getenv("CLIENT_SECRET","") |
| 28 | + audience = os.getenv("AUDIENCE","") |
| 29 | + tenant = os.getenv("TENANT","") |
21 | 30 |
|
22 | 31 | # If we have AUTH0_TOKEN, generate a bearer token |
23 | | - if(auth0_token != ''): |
24 | | - if (client_id != ''): |
25 | | - print(f"print: AUTH0_TOKEN environment variable is set, CLIENT_ID environment variable will be ignored") |
26 | | - if (client_secret != ''): |
27 | | - print(f"print: AUTH0_TOKEN environment variable is set, CLIENT_SECRET environment variable will be ignored") |
28 | | - if (audience != ''): |
29 | | - print(f"print: AUTH0_TOKEN environment variable is set, AUDIENCE environment variable will be ignored") |
30 | | - if (tenant != ''): |
31 | | - print(f"print: AUTH0_TOKEN environment variable is set, TENANT environment variable will be ignored") |
32 | | - return create_bearer_token(auth0_token) |
| 32 | + if(auth0_token != ""): |
| 33 | + return f"Bearer {auth0_token}" |
33 | 34 |
|
34 | | - # If we don't, look for CLIENT_ID, CLIENT_SECRET, AUDIENCE and TENANT to create a token using Auth0 API |
35 | | - missing=[] |
36 | | - if(client_id == ''): |
37 | | - missing.append("CLIENT_ID") |
38 | | - if(client_secret == ''): |
39 | | - missing.append("CLIENT_SECRET") |
40 | | - if(audience == ''): |
41 | | - missing.append("AUDIENCE") |
42 | | - if(tenant == ''): |
43 | | - missing.append("TENANT") |
44 | | - |
45 | | - if (len(missing) == 0): |
46 | | - return create_bearer_token_using_rest(client_id, client_secret, audience, tenant) |
| 35 | + # If we don't, use Auth0 REST API to request one (we need CLIENT_ID and CLIENT_SECRET; Optionally AUDIENCE and TENANT) |
| 36 | + if (client_id and client_secret): |
| 37 | + return _create_bearer_token_using_rest( |
| 38 | + client_id, |
| 39 | + client_secret, |
| 40 | + audience if audience else DEFAULT_AUDIENCE, |
| 41 | + tenant if tenant else DEFAULT_TENANT) |
47 | 42 | else: |
48 | | - raise Exception(f"AUTH0_TOKEN environment variable is not set, therefore CLIENT_ID, CLIENT_SECRET, AUDIENCE and TENANT environment variables must be set. Missing env variables {missing}") |
49 | | - |
50 | | -def create_bearer_token(auth0_token) -> str: |
51 | | - if (auth0_token == ''): |
52 | | - raise Exception(f"auth0_token cannot be null") |
| 43 | + raise Exception(f"AUTH0_TOKEN environment variable is not set, therefore CLIENT_ID and CLIENT_SECRET (and optionally AUDIENCE and TENANT) environment variables must be set") |
53 | 44 |
|
54 | | - return f"Bearer {auth0_token}" |
55 | | - |
56 | | -def create_bearer_token_using_rest(client_id, client_secret, audience, tenant) -> str: |
57 | | - if (client_id == ''): |
| 45 | +def _create_bearer_token_using_rest(client_id, client_secret, audience, tenant) -> str: |
| 46 | + if (client_id == ""): |
58 | 47 | raise Exception(f"client_id cannot be null") |
59 | | - if (client_secret == ''): |
| 48 | + if (client_secret == ""): |
60 | 49 | raise Exception(f"client_secret cannot be null") |
61 | | - if (audience == ''): |
| 50 | + if (audience == ""): |
62 | 51 | raise Exception(f"audience cannot be null") |
63 | | - if (tenant == ''): |
| 52 | + if (tenant == ""): |
64 | 53 | raise Exception(f"tenant cannot be null") |
65 | 54 |
|
66 | | - try: |
67 | | - # Setup connection and payload |
68 | | - conn = http.client.HTTPSConnection(tenant) |
69 | | - headers = { 'content-type': "application/json" } |
70 | | - params = {"client_id": client_id, "client_secret": client_secret, "grant_type" : "client_credentials", "audience": audience } |
71 | | - payload = json.dumps(params) |
| 55 | + # Setup connection and payload |
| 56 | + conn = http.client.HTTPSConnection(tenant) |
| 57 | + headers = { "content-type": "application/json" } |
| 58 | + params = {"client_id": client_id, "client_secret": client_secret, "grant_type" : "client_credentials", "audience": audience } |
| 59 | + payload = json.dumps(params) |
72 | 60 |
|
73 | | - # Send Request |
74 | | - conn.request("POST", "/oauth/token", payload, headers) |
75 | | - res = conn.getresponse() |
76 | | - data = res.read() |
77 | | - |
78 | | - json_data = json.loads(data.decode("utf-8")) |
| 61 | + # Send Request |
| 62 | + conn.request("POST", "/oauth/token", payload, headers) |
| 63 | + res = conn.getresponse() |
| 64 | + data = res.read() |
| 65 | + |
| 66 | + json_data = json.loads(data.decode("utf-8")) |
79 | 67 |
|
80 | | - # Get access token to be used to authenticate against API |
81 | | - try: |
82 | | - token = f"{json_data['token_type']} {json_data['access_token']}" |
83 | | - return token |
84 | | - except Exception as ee: |
85 | | - print(f"create_bearer_token_using_rest: Returned JSON doesn't contain 'token_type' and/or 'access_token'. Check your client_id, client_secret, audience and tenant: {json_data}") |
86 | | - return "" |
87 | | - |
88 | | - except Exception as e: |
89 | | - print(f"create_bearer_token_using_rest: Got exception {e}") |
90 | | - return "" |
| 68 | + # Get access token to be used to authenticate against API |
| 69 | + try: |
| 70 | + token = f"{json_data['token_type']} {json_data['access_token']}" |
| 71 | + return token |
| 72 | + except Exception as ee: |
| 73 | + raise Exception(f"Returned JSON doesn't contain 'token_type' and/or 'access_token'. Check your client ID, client secret, audience and tenant: {json_data}") |
0 commit comments