-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
127 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,71 @@ | ||
.. _examples: | ||
|
||
=================== | ||
Generating QR codes | ||
------------------- | ||
=================== | ||
|
||
PyBankID cannot generate QR codes for you, but there is an example application in the | ||
PyBankID can generate QR codes for you. There is an example application in the | ||
`examples folder of the repo <https://github.com/hbldh/pybankid/tree/master/examples>`_ where a | ||
Flask application called ``qrdemo`` shows one way to do authentication with animated QR codes. | ||
|
||
The content for the QR code is generated by this method: | ||
Below follows the app's README file: | ||
|
||
.. code-block:: python | ||
QR Authentication Example | ||
------------------------- | ||
|
||
import hashlib | ||
import hmac | ||
from math import floor | ||
import time | ||
Making a simple authentication via QR code solution using Flask, Flask-Caching and PyBankID. | ||
|
||
def generate_qr_code_content(qr_start_token: str, start_t: float, qr_start_secret: str): | ||
"""Given QR start token, time.time() when initiated authentication call was made and the | ||
QR start secret, calculate the current QR code content to display. | ||
""" | ||
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}" | ||
Running the application | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
1. Navigate your terminal to the same folder that this README resides in. | ||
2. Create a virtualenv: ``python -m venv .venv`` | ||
3. Activate it. | ||
4. Install requirements: ``pip install -r requirements.txt`` | ||
5. Run Flask app: | ||
|
||
1. From Bash: | ||
|
||
```bash | ||
$ export FLASK_APP=qrdemo.app:app | ||
$ flask run -h 0.0.0.0 | ||
``` | ||
2. From Powershell: | ||
|
||
```powershell | ||
> $env:FLASK_APP = "qrdemo.app:app" | ||
> flask run -h 0.0.0.0 | ||
``` | ||
|
||
The app can now be accessed from the running computer on ``http://127.0.0.1:5000``, ``http://localhost:5000`` or from an | ||
external device on the same network on ``http://<ip for the running computer>:5000``. | ||
|
||
|
||
Basic workflow | ||
~~~~~~~~~~~~~~ | ||
|
||
These are the steps that the application takes: | ||
|
||
1. Ask the user for Swedish Personal Identity Number (PN) or initiate an authentication without. | ||
2. Upon POSTing that PN to the backend, initiate a BankID ``authenticate`` session. This generates tokens that | ||
one can create QR codes from using the ``generate_qr_code_content`` method. | ||
3. Continuously update the QR code according to the description in the BankID Relying Party Guidelines | ||
Version: 3.6 (see below, Chapter 4). The new QR code content to display MUST be fetched from the backend since | ||
the ``qrStartSecret`` must never be shown to the user for the authentication to be trustworthy. | ||
4. Also make ``collect`` calls to the BankID servers continuously and monitor if signing is complete or failed. | ||
5. Redirect when complete or failed. | ||
|
||
|
||
Missing components | ||
~~~~~~~~~~~~~~~~~~ | ||
|
||
There are a few shortcuts taken here: | ||
|
||
- There is no error handling of ``status: failed`` results when collecting the authentication response. | ||
- There is no ``Recommended User Messages (RFA)`` handling. It merely displays the ``status`` and ``hintCode`` from the collect response. | ||
- The Cache is a memory cache on this single instance web app. | ||
|
||
References | ||
~~~~~~~~~~ | ||
|
||
[BankID Integration Guide](https://www.bankid.com/en/utvecklare/guider/teknisk-integrationsguide/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters