Skip to content

Commit

Permalink
Merge branch 'develop' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
hbldh authored Apr 12, 2024
2 parents 46a8447 + 4392874 commit f63f9be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,5 @@ The PyBankID solution can be tested with `pytest <https://pytest.org/>`_:

.. code-block:: bash
pytest tests/
pytest tests/

3 changes: 3 additions & 0 deletions bankid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
from bankid.certutils import create_bankid_test_server_cert_and_key
from bankid.syncclient import BankIDClient
from bankid.asyncclient import BankIDAsyncClient
from bankid.baseclient import generate_qr_code_content


__all__ = [
"BankIDClient",
"BankIDAsyncClient",
"exceptions",
"create_bankid_test_server_cert_and_key",
"generate_qr_code_content",
"__version__",
"version",
]
30 changes: 17 additions & 13 deletions bankid/baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,8 @@ def __init__(
self.client = None

@staticmethod
def generate_qr_code_content(qr_start_token: str, start_t: [float, datetime], qr_start_secret: str):
"""Given QR start token, time.time() or UTC datetime when initiated authentication call was made and the
QR start secret, calculate the current QR code content to display.
"""
if isinstance(start_t, datetime):
start_t = start_t.timestamp()
elapsed_seconds_since_call = int(floor(time.time() - start_t))
qr_auth_code = hmac.new(
qr_start_secret.encode(),
msg=str(elapsed_seconds_since_call).encode(),
digestmod=hashlib.sha256,
).hexdigest()
return f"bankid.{qr_start_token}.{elapsed_seconds_since_call}.{qr_auth_code}"
def generate_qr_code_content(qr_start_token: str, start_t: [float, datetime], qr_start_secret: str) -> str:
return generate_qr_code_content(qr_start_token, start_t, qr_start_secret)

@staticmethod
def _encode_user_data(user_data):
Expand Down Expand Up @@ -83,3 +72,18 @@ def _create_payload(
if user_visible_data_format and user_visible_data_format == "simpleMarkdownV1":
data["userVisibleDataFormat"] = "simpleMarkdownV1"
return data


def generate_qr_code_content(qr_start_token: str, start_t: [float, datetime], qr_start_secret: str) -> str:
"""Given QR start token, time.time() or UTC datetime when initiated authentication call was made and the
QR start secret, calculate the current QR code content to display.
"""
if isinstance(start_t, datetime):
start_t = start_t.timestamp()
elapsed_seconds_since_call = int(floor(time.time() - start_t))
qr_auth_code = hmac.new(
qr_start_secret.encode(),
msg=str(elapsed_seconds_since_call).encode(),
digestmod=hashlib.sha256,
).hexdigest()
return f"bankid.{qr_start_token}.{elapsed_seconds_since_call}.{qr_auth_code}"

0 comments on commit f63f9be

Please sign in to comment.