Skip to content

Commit

Permalink
Return expiry datetime in the same tz as client
Browse files Browse the repository at this point in the history
Expiry datetime is returned in UTC but this is misleading since tz
isn't stated.

Fixes broamski#74
  • Loading branch information
Mark Abdel Malak committed May 13, 2022
1 parent 5334deb commit c5156ba
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions awsmfa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,26 @@ def validate(args, config):
# Unless we're forcing a refresh, check expiration.
if not force_refresh:
exp = datetime.datetime.strptime(
config.get(short_term_name, 'expiration'), '%Y-%m-%d %H:%M:%S')
diff = exp - datetime.datetime.utcnow()
config.get(short_term_name, 'expiration'), '%Y-%m-%d %H:%M:%S'
)
# add tz manually since strptime doesn't
exp.replace(datetime.timezone.utc)
current_datetime_with_tz = datetime.datetime.now().astimezone()
diff = exp - current_datetime_with_tz
if diff.total_seconds() <= 0:
logger.info("Your credentials have expired, renewing.")
else:
should_refresh = False
logger.info(
"Your credentials are still valid for %s seconds"
" they will expire at %s"
% (diff.total_seconds(), exp))
% (
diff.total_seconds(),
exp.astimezone(current_datetime_with_tz.tzinfo).replace(
microsecond=0
),
)
)

if should_refresh:
get_credentials(short_term_name, key_id, access_key, args, config)
Expand Down

0 comments on commit c5156ba

Please sign in to comment.