Skip to content

Commit 717f593

Browse files
authored
Merge pull request #44 from Authress/prevent-set-token
Prevent usage of set_token when service client access_token was set. …
2 parents 19eef10 + 3d7768e commit 717f593

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

authress/api/invites_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def respond_to_invite_with_http_info(self, invite_id : Annotated[constr(strict=T
590590
_auth_settings = ['oauth2'] # noqa: E501
591591

592592
_response_types_map = {
593-
'200': "Account",
593+
'200': None,
594594
'401': None,
595595
'403': None,
596596
'404': None,

authress/authress_client.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ class AuthressClient(object):
2626
def __init__(self, authress_api_url=None, service_client_access_key=None, user_agent=None):
2727
self._host = authress_api_url if authress_api_url.startswith('http') else f"https://{authress_api_url}"
2828
self._host = re.sub(r'/+$', '', self._host)
29+
self._service_client_access_key = service_client_access_key
2930

3031
self._http_client = HttpClient(host=self._host, access_key=service_client_access_key, user_agent=user_agent)
3132
self._token_verifier = token_verifier.TokenVerifier(http_client=self._http_client)
3233

3334
def set_token(self, token: str):
34-
self._http_client.set_token(token)
35+
if self._service_client_access_key is None:
36+
self._http_client.set_token(token)
37+
return
38+
39+
raise Exception("An AuthressClient cannot use set_token, when the client has been instantiated with a service client access key. It must either be used for User tokens or with Service Client Access Keys, but not both.")
3540

3641
def get_client_token(self) -> str:
3742
"""Generates a Service Client Machine JWT to be used for securing machine to machine requests."""

authress/http_client.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ def __init__(self, host=None, access_key=None, user_agent=None):
6565
def set_token(self, token):
6666
self.default_headers['Authorization'] = f'Bearer {token.replace("Bearer", "").strip()}'
6767

68-
def get_user_from_token(self):
69-
token = self.default_headers['Authorization'].replace("Bearer", "").strip()
70-
jwtData = jwt.decode(token, options={"verify_signature": False})
71-
if 'aud' in jwtData and 'https://api.authress.io' in jwtData['aud']:
72-
return f"Authress|{jwtData['sub']}"
73-
74-
return jwtData['sub']
75-
7668
def __enter__(self):
7769
return self
7870

@@ -762,4 +754,7 @@ def __deserialize_model(self, data, klass):
762754
return klass.from_dict(data)
763755

764756
def _get_client_token(self) -> str:
765-
return self.service_client_token_provider.get_client_token()
757+
if self.service_client_token_provider is None:
758+
return None
759+
760+
return self.service_client_token_provider.get_client_token()

docs/EXAMPLES.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ from authress import AuthressClient
77
# create an instance of the API class during service initialization
88
# Authress custom domain or if there isn't one yet, use the authress account specific url
99
authress_api_url = "https://authress.yourdomain.com" # or "https://ACCOUNT_ID.api.authress.io"
10-
11-
# The Service Client Access Key for your service client.
12-
service_client_access_key = "sc_key_001"
13-
authress_client = AuthressClient(authress_api_url=authress_api_url , service_client_access_key=service_client_access_key)
10+
authress_client = AuthressClient(authress_api_url=authress_api_url)
1411

1512
# on api route
1613
from flask import request
@@ -46,7 +43,7 @@ authress_api_url = "https://authress.yourdomain.com" # or "https://ACCOUNT_ID.ap
4643

4744
# Create a service client in the Authress management portal and past the access token here
4845
service_client_access_key = 'eyJrZXlJ....'
49-
authress_client = AuthressClient(authress_api_url=authress_api_url , service_client_access_key=service_client_access_key)
46+
authress_client = AuthressClient(authress_api_url=authress_api_url, service_client_access_key=service_client_access_key)
5047

5148
# on api route
5249
from flask import request

0 commit comments

Comments
 (0)