Skip to content

Commit 54df36b

Browse files
authored
fix(invalid_token): Retry cognito signed token issues (#57)
* fix(invalid_token): Retry cognito signed token issues
1 parent 49dc2b4 commit 54df36b

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

staxapp/auth.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414

1515
class StaxAuth:
16-
def __init__(self, config_branch):
16+
def __init__(self, config_branch, max_retries: int = 3):
1717
config = StaxConfig.api_config
1818

1919
self.identity_pool = config.get(config_branch).get("identityPoolId")
2020
self.user_pool = config.get(config_branch).get("userPoolId")
2121
self.client_id = config.get(config_branch).get("userPoolWebClientId")
2222
self.aws_region = config.get(config_branch).get("region")
23+
self.max_retries = max_retries
2324

2425
def requests_auth(self, username, password, **kwargs):
2526
if username is None:
@@ -83,23 +84,35 @@ def sts_from_cognito_identity_pool(self, token, cognito_client=None, **kwargs):
8384
region_name=self.aws_region,
8485
config=BotoConfig(signature_version=UNSIGNED),
8586
)
86-
try:
87-
id = cognito_client.get_id(
88-
IdentityPoolId=self.identity_pool,
89-
Logins={
90-
f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token
91-
},
92-
)
93-
id_creds = cognito_client.get_credentials_for_identity(
94-
IdentityId=id["IdentityId"],
95-
Logins={
96-
f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token
97-
},
98-
)
99-
except ClientError as e:
87+
88+
for i in range(self.max_retries):
89+
try:
90+
id = cognito_client.get_id(
91+
IdentityPoolId=self.identity_pool,
92+
Logins={
93+
f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token
94+
},
95+
)
96+
id_creds = cognito_client.get_credentials_for_identity(
97+
IdentityId=id["IdentityId"],
98+
Logins={
99+
f"cognito-idp.{self.aws_region}.amazonaws.com/{self.user_pool}": token
100+
},
101+
)
102+
break
103+
except ClientError as e:
104+
# AWS eventual consistency, attempt to retry up to 3 times
105+
if "Couldn't verify signed token" in str(e):
106+
continue
107+
else:
108+
raise InvalidCredentialsException(
109+
f"Unexpected Client Error. Error details: {e}"
110+
)
111+
else:
100112
raise InvalidCredentialsException(
101-
f"Unexpected Client Error. Error details: {e}"
113+
"Retries Exceeded: Unexpected Client Error"
102114
)
115+
103116
return id_creds
104117

105118
def sigv4_signed_auth_headers(self, id_creds):

tests/test_auth.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,39 @@ def testCredsClient(self):
134134
Test the cognito client is invoked and throws an error
135135
"""
136136
sa = StaxAuth("ApiAuth")
137+
138+
# Test Invalid Credentials
137139
token = jwt.encode({"sub": "unittest"}, "secret", algorithm="HS256")
138140
jwt_token = jwt.decode(token, verify=False)
139141
with self.assertRaises(InvalidCredentialsException):
140142
sa.sts_from_cognito_identity_pool(jwt_token.get("sub"))
141143

144+
# Test "Couldn't verify signed token" retry
145+
expected_parameters = {
146+
"IdentityPoolId": sa.identity_pool,
147+
"Logins": {
148+
f"cognito-idp.{sa.aws_region}.amazonaws.com/{sa.user_pool}": "unittest"
149+
}
150+
}
151+
for i in range(sa.max_retries):
152+
self.cognito_stub.add_client_error(
153+
"get_id",
154+
service_error_code="NotAuthorizedException",
155+
service_message="Invalid login token. Couldn't verify signed token.",
156+
expected_params=expected_parameters,
157+
)
158+
self.cognito_stub.activate()
159+
160+
with self.assertRaises(InvalidCredentialsException) as e:
161+
sa.sts_from_cognito_identity_pool(jwt_token.get("sub"), cognito_client=self.cognito_client)
162+
163+
self.assertEqual(str(e.exception), "InvalidCredentialsException: Retries Exceeded: Unexpected Client Error")
164+
self.assertEqual(len(self.cognito_stub._queue), 0)
165+
142166
def testAuthErrors(self):
143167
"""
144-
Test that errors are thrown when keys are invalid
145-
"""
168+
Test that errors are thrown when keys are invalid
169+
"""
146170
sa = StaxAuth("ApiAuth")
147171
# Test with no username
148172
with self.assertRaises(InvalidCredentialsException):

0 commit comments

Comments
 (0)