Skip to content

Commit

Permalink
fix: Generate timestamps independently from current locale (NabuCasa#127
Browse files Browse the repository at this point in the history
)

* fix: Generate timestamps independently from current locale

Inspired by wsgiref.handlers.format_date_time()

* refactor: Set WEEKDAY_NAMES and MONTH_NAMES as top-level constants

* Remove unused import
  • Loading branch information
marty-se authored Apr 28, 2022
1 parent 971314c commit 085bfd1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
35 changes: 28 additions & 7 deletions pycognito/aws_srp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import hashlib
import hmac
import os
import re

import boto3
import six
Expand Down Expand Up @@ -33,6 +32,21 @@
# https://github.com/aws/amazon-cognito-identity-js/blob/master/src/AuthenticationHelper.js#L49
G_HEX = "2"
INFO_BITS = bytearray("Caldera Derived Key", "utf-8")
WEEKDAY_NAMES = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
MONTH_NAMES = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
]


def hash_sha256(buf):
Expand Down Expand Up @@ -202,18 +216,25 @@ def get_secret_hash(username, client_id, client_secret):
hmac_obj = hmac.new(bytearray(client_secret, "utf-8"), message, hashlib.sha256)
return base64.standard_b64encode(hmac_obj.digest()).decode("utf-8")

@staticmethod
def get_cognito_formatted_timestamp(input_datetime):
return "%s %s %d %02d:%02d:%02d UTC %d" % (
WEEKDAY_NAMES[input_datetime.weekday()],
MONTH_NAMES[input_datetime.month - 1],
input_datetime.day,
input_datetime.hour,
input_datetime.minute,
input_datetime.second,
input_datetime.year,
)

def process_challenge(self, challenge_parameters):
internal_username = challenge_parameters["USERNAME"]
user_id_for_srp = challenge_parameters["USER_ID_FOR_SRP"]
salt_hex = challenge_parameters["SALT"]
srp_b_hex = challenge_parameters["SRP_B"]
secret_block_b64 = challenge_parameters["SECRET_BLOCK"]
# re strips leading zero from a day number (required by AWS Cognito)
timestamp = re.sub(
r" 0(\d) ",
r" \1 ",
datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S UTC %Y"),
)
timestamp = self.get_cognito_formatted_timestamp(datetime.datetime.utcnow())
hkdf = self.get_password_authentication_key(
user_id_for_srp, self.password, hex_to_long(srp_b_hex), salt_hex
)
Expand Down
29 changes: 29 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,35 @@ def test_authenticate_user(self, _):
self.assertTrue("RefreshToken" in tokens["AuthenticationResult"])
stub.assert_no_pending_responses()

def test_cognito_formatted_timestamp(self):
self.assertEqual(
self.aws.get_cognito_formatted_timestamp(
datetime.datetime(2022, 1, 1, 0, 0, 0)
),
"Sat Jan 1 00:00:00 UTC 2022",
)

self.assertEqual(
self.aws.get_cognito_formatted_timestamp(
datetime.datetime(2022, 1, 2, 12, 0, 0)
),
"Sun Jan 2 12:00:00 UTC 2022",
)

self.assertEqual(
self.aws.get_cognito_formatted_timestamp(
datetime.datetime(2022, 1, 3, 9, 0, 0)
),
"Mon Jan 3 09:00:00 UTC 2022",
)

self.assertEqual(
self.aws.get_cognito_formatted_timestamp(
datetime.datetime(2022, 12, 31, 23, 59, 59)
),
"Sat Dec 31 23:59:59 UTC 2022",
)


@moto.mock_cognitoidp
class UtilsTestCase(unittest.TestCase):
Expand Down

0 comments on commit 085bfd1

Please sign in to comment.