Skip to content

Commit

Permalink
Expose QR code helper explicitly.
Browse files Browse the repository at this point in the history
This simplifies making use of it without having access to a client instance.
  • Loading branch information
Majsvaffla committed Apr 8, 2024
1 parent cba58a6 commit 4913557
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 2 additions & 0 deletions bankid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
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 4913557

Please sign in to comment.