BankID.QRCode (bankid v0.0.2)

Copy Markdown View Source

QR code generation for BankID authentication.

This module provides helpers for generating animated QR codes that refresh according to BankID's time-based HMAC algorithm.

Usage

# Generate QR code content
qr_content = BankID.QRCode.generate_content(
  qr_start_token,
  start_time,
  qr_start_secret
)

# Generate QR code SVG
svg = BankID.QRCode.generate_svg(
  qr_start_token,
  start_time,
  qr_start_secret,
  width: 256
)

The QR code must be regenerated every second with the current time to maintain the time-based HMAC that BankID requires.

Summary

Types

Options for QR code generation.

Functions

Calculate elapsed seconds since authentication start.

Generate the time-based HMAC content for a BankID QR code.

Generate a QR code SVG from BankID authentication data.

Types

qr_options()

@type qr_options() :: keyword()

Options for QR code generation.

Options

  • :width - Width of the QR code in pixels (default: 256)
  • :shape - Shape of QR modules ("square" or "circle", default: "square")
  • :color - Color of the QR code modules (default: "#000")
  • :background_color - Background color of the QR code (default: "#FFF")

Functions

elapsed_seconds(start_time)

@spec elapsed_seconds(integer()) :: integer()

Calculate elapsed seconds since authentication start.

Parameters

  • start_time: Unix timestamp when authentication was initiated

Returns

Integer number of seconds elapsed

generate_content(qr_start_token, start_time, qr_start_secret)

@spec generate_content(String.t(), integer(), String.t()) :: String.t()

Generate the time-based HMAC content for a BankID QR code.

This uses BankID's algorithm: bankid.<qr_start_token>.<elapsed_time>.<hmac>

This is a native Elixir implementation that matches the behavior of the Python pybankid library's generate_qr_code_content function.

Parameters

  • qr_start_token: Token from authentication response
  • start_time: Timestamp when authentication was initiated (Unix timestamp in seconds)
  • qr_start_secret: Secret from authentication response

Returns

String containing the QR code content in BankID format

Algorithm

Based on pybankid library (https://github.com/hbldh/pybankid/blob/master/bankid/qr.py):

  1. Calculate elapsed seconds: floor(current_time - start_time)
  2. Generate HMAC-SHA256: hmac(secret, elapsed_seconds_as_string)
  3. Format: "bankid.<token>.<elapsed>.<hmac_hex>"

generate_svg(qr_start_token, start_time, qr_start_secret, opts \\ [])

@spec generate_svg(String.t(), integer(), String.t(), qr_options()) :: String.t()

Generate a QR code SVG from BankID authentication data.

Parameters

  • qr_start_token: Token from authentication response
  • start_time: Timestamp when authentication was initiated (Unix timestamp)
  • qr_start_secret: Secret from authentication response
  • opts: Optional keyword list for QR code generation:
    • :width - Width of the QR code (default: 256)
    • :shape - Shape of the QR code ("square" or "circle", default: "square")
    • :color - Color of the QR code (default: "#000")
    • :background_color - Background color (default: "#FFF")

Returns

SVG string ready to be embedded in HTML

Examples

# Basic QR code
svg = BankID.QRCode.generate_svg(token, start_time, secret)

# Customized QR code
svg = BankID.QRCode.generate_svg(token, start_time, secret,
  width: 300,
  color: "#0066CC",
  shape: "circle"
)