Skip to content

Commit a1a9940

Browse files
authored
Tweaks to Auth Handling (#75)
Ignore iat verification exceptions for now and return False for login when it fails due to wrong credentials. Initialize the Auth object first, get tokens in a separate call.
1 parent 2ad9734 commit a1a9940

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

pyemvue/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "0.18.4"
1+
VERSION = "0.18.5"

pyemvue/auth.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
# These provide AWS cognito authentication support
88
from pycognito import Cognito
9+
from pycognito.exceptions import TokenVerificationException
910

1011
CLIENT_ID = "4qte47jbstod8apnfic0bunmrq"
1112
USER_POOL = "us-east-2_ghlOXVLi1"
13+
USER_POOL_URL = f"https://cognito-idp.us-east-2.amazonaws.com/{USER_POOL}"
1214

1315

1416
class Auth:
@@ -34,6 +36,8 @@ def __init__(
3436
self.max_retry_delay = max(max_retry_delay, 0)
3537
self.pool_wellknown_jwks = None
3638

39+
self._password = None
40+
3741
if (
3842
tokens
3943
and tokens["access_token"]
@@ -54,14 +58,24 @@ def __init__(
5458
self.cognito = Cognito(
5559
USER_POOL, CLIENT_ID, user_pool_region="us-east-2", username=username
5660
)
57-
self.cognito.authenticate(password=password)
58-
59-
self.tokens = self.refresh_tokens()
61+
self._password = password
6062

6163
def refresh_tokens(self) -> "dict[str, str]":
6264
"""Refresh and return new tokens."""
63-
self.cognito.renew_access_token()
65+
try:
66+
if self._password:
67+
self.cognito.authenticate(password=self._password)
68+
69+
self.cognito.renew_access_token()
70+
except TokenVerificationException as ex:
71+
# ignore iat errors (until https://github.com/NabuCasa/pycognito/issues/225 is fixed)
72+
if "The token is not yet valid (iat)" not in ex.args[0]:
73+
raise
74+
finally:
75+
self._password = None
76+
6477
tokens = self._extract_tokens_from_cognito()
78+
self.tokens = tokens
6579

6680
if self.token_updater is not None:
6781
self.token_updater(tokens)
@@ -131,11 +145,11 @@ def _do_request(self, method: str, path: str, **kwargs) -> requests.Response:
131145
timeout=(self.connect_timeout, self.read_timeout),
132146
)
133147

134-
def _decode_token(self, token: str) -> dict:
148+
def _decode_token(self, token: str, verify_exp: bool = False) -> dict:
135149
"""Decode a JWT token and return the payload as a dictionary, without a hard dependency on pycognito."""
136150
if not self.pool_wellknown_jwks:
137151
self.pool_wellknown_jwks = requests.get(
138-
f"https://cognito-idp.us-east-2.amazonaws.com/{USER_POOL}/.well-known/jwks.json",
152+
USER_POOL_URL + "/.well-known/jwks.json",
139153
timeout=5,
140154
).json()
141155

@@ -147,10 +161,10 @@ def _decode_token(self, token: str) -> dict:
147161
token,
148162
algorithms=["RS256"],
149163
key=hmac_key,
150-
options={"verify_exp": False, "verify_iat": False, "verify_nbf": False},
164+
issuer=self.cognito.user_pool_url,
165+
options={"verify_exp": verify_exp, "verify_iat": False, "verify_nbf": False},
151166
)
152167

153-
154168
class SimulatedAuth(Auth):
155169
def __init__(
156170
self, host: str, username: Optional[str] = None, password: Optional[str] = None
@@ -161,8 +175,6 @@ def __init__(
161175
self.connect_timeout = 6.03
162176
self.read_timeout = 10.03
163177

164-
self.tokens = self.refresh_tokens()
165-
166178
def refresh_tokens(self) -> dict[str, str]:
167179
return {"id_token": "simulator"}
168180

pyemvue/pyemvue.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ def login(
366366
token_updater=self._store_tokens,
367367
)
368368

369+
try:
370+
self.auth.refresh_tokens()
371+
except self.auth.cognito.client.exceptions.NotAuthorizedException as ex:
372+
return False
373+
369374
if self.auth.tokens:
370375
self.username = self.auth.get_username()
371376
self.customer = self.get_customer_details()

0 commit comments

Comments
 (0)