Skip to content

Commit dee12e5

Browse files
committed
make threshold configurable
1 parent c8164ba commit dee12e5

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ export STAX_ACCESS_KEY=<your_access_key>
2121
export STAX_SECRET_KEY=<your_secret_key>
2222
```
2323

24+
*Optional configuration:*
25+
26+
##### Authentication token expiry
27+
28+
Allows configuration of the threshold to when the Auth library should re-cache the credentials
29+
*Suggested use when running within CI/CD tools to reduce overall auth calls*
30+
~~~bash
31+
export TOKEN_EXPIRY_THRESHOLD_IN_MINS=2 # Type: Integer representing minutes
32+
~~~
33+
2434
## Usage
2535

2636
### Read Accounts

staxapp/auth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/local/bin/python3
22
from datetime import datetime, timedelta, timezone
3+
from os import environ
34

45
import boto3
56
from aws_requests_auth.aws_auth import AWSRequestsAuth
@@ -141,7 +142,7 @@ class ApiTokenAuth:
141142
def requests_auth(username, password, **kwargs):
142143
# Minimize the potentical for token to expire while still being used for auth (say within a lambda function)
143144
if StaxConfig.expiration and StaxConfig.expiration - timedelta(
144-
minutes=10
145+
minutes=int(environ.get("TOKEN_EXPIRY_THRESHOLD_IN_MINS", 1))
145146
) > datetime.now(timezone.utc):
146147
return StaxConfig.auth
147148

tests/test_auth.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from botocore.client import Config as BotoConfig
1717
from botocore.stub import Stubber, ANY
1818
from datetime import datetime, timedelta, timezone
19+
from os import environ
1920

2021
from staxapp.api import Api
2122
from staxapp.auth import StaxAuth, ApiTokenAuth, RootAuth
@@ -325,9 +326,24 @@ def testApiTokenAuthExpiring(self, requests_auth_mock):
325326
)
326327
requests_auth_mock.assert_not_called()
327328

329+
requests_auth_mock.reset_mock()
330+
## expiration in 5 seconds from now, refresh to avoid token becoming stale used
331+
StaxConfig.expiration = datetime.now(timezone.utc) + timedelta(seconds=5)
332+
333+
ApiTokenAuth.requests_auth(
334+
"username",
335+
"password",
336+
srp_client=self.aws_srp_client,
337+
cognito_client=self.cognito_client,
338+
)
339+
requests_auth_mock.assert_called_once()
340+
341+
328342
requests_auth_mock.reset_mock()
329343
## expiration in 5 minutes from now, refresh to avoid token becoming stale used
330-
StaxConfig.expiration = datetime.now(timezone.utc) + timedelta(minutes=5)
344+
## override default triggering library to not refresh
345+
environ["TOKEN_EXPIRY_THRESHOLD_IN_MINS"] = "10"
346+
StaxConfig.expiration = datetime.now(timezone.utc) + timedelta(minutes=2)
331347

332348
ApiTokenAuth.requests_auth(
333349
"username",

0 commit comments

Comments
 (0)