Skip to main content

PyBankID

Build and Test Documentation Status PyPI Version PyPI License

PyBankID is a client for providing BankID services as a Relying Party, i.e., providing authentication and signing functionality to end users. This package provides a simplifying interface for initiating authentication and signing orders and then collecting the results from the BankID servers.

The only supported BankID API version supported by PyBankID from version 1.0.0 is v6.0, which means that the Secure Start solution is the only supported way of providing BankID services. PyBankID versions prior to 1.0.0 will not work after 1st of May 2024.

If you intend to use PyBankID in your project, you are advised to read the BankID Integration Guide before doing anything else. There, one can find information about how the BankID methods are defined and how to use them.

Installation

PyBankID can be installed through pip:

pip install pybankid

Usage

PyBankID provides both a synchronous and an asynchronous client for communication with BankID services. The example below will use the synchronous client, but the asynchronous client is used in an identical fashion, only using the await keyword before each function call.

Synchronous client

from bankid import BankIDClient
client = BankIDClient(certificates=(
    'path/to/certificate.pem',
    'path/to/key.pem',
))

Connection to the production server is the default in the client. If a test server is desired, send in the test_server=True keyword in the init of the client.

All authentication and signing calls require the end user's IP address to be included. An authentication order is initiated as such:

client.authenticate(end_user_ip='194.168.2.25')
{
    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',
    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',
    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',
    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'
}

and a sign order is initiated in a similar fashion:

client.sign(
    end_user_ip='194.168.2.25',
    user_visible_data="The information to sign."
)
{
    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',
    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',
    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',
    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'
}

If you want to ascertain that only one individual can authenticate or sign, you can specify this using the requirement keyword:

client.sign(
   end_user_ip='194.168.2.25',
   user_visible_data="The information to sign."
   requirement={"personalNumber": "YYYYMMDDXXXX"}
)
{
    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',
    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',
    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',
    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'
}

If someone other than the one you specified tries to authenticate or sign, the BankID app will state that the request is not intended for the user.

The status of an order can then be studied by polling with the collect method using the received orderRef:

client.collect(order_ref="a9b791c3-459f-492b-bf61-23027876140b")
{
    'hintCode': 'outstandingTransaction',
    'orderRef': 'a9b791c3-459f-492b-bf61-23027876140b',
    'status': 'pending'
}
client.collect(order_ref="a9b791c3-459f-492b-bf61-23027876140b")
{
    'hintCode': 'userSign',
    'orderRef': 'a9b791c3-459f-492b-bf61-23027876140b',
    'status': 'pending'
}
client.collect(order_ref="a9b791c3-459f-492b-bf61-23027876140b")
{
    'completionData': {
        'cert': {
            'notAfter': '1581289199000',
            'notBefore': '1518130800000'
        },
        'device': {
            'ipAddress': '0.0.0.0'
        },
        'ocspResponse': 'MIIHegoBAKCCB[...]',
        'signature': 'PD94bWwgdmVyc2lv[...]',
        'user': {
            'givenName': 'Namn',
            'name': 'Namn Namnsson',
            'personalNumber': 'YYYYMMDDXXXX',
            'surname': 'Namnsson'
        }
    },
    'orderRef': 'a9b791c3-459f-492b-bf61-23027876140b',
    'status': 'complete'
}

Please note that the collect method should be used sparingly: in the BankID Integration Guide it is specified that "collect should be called every two seconds and must not be called more frequent than once per second".

PyBankID also implements the phone/auth and phone/sign methods, for performing authentication and signing with users that are contacted through phone. For documentation on this, see PyBankID's Read the Docs page.

Asynchronous client

The asynchronous client is used in the same way as the synchronous client, with the difference that all request are performed asynchronously.

The synchronous guide above can be used as a reference for the asynchronous client as well, by simply adding the await keyword:

from bankid import BankIDAsyncClient
client = BankIDAsyncClient(certificates=(
    'path/to/certificate.pem',
    'path/to/key.pem',
))

await client.authenticate(end_user_ip='194.168.2.25')
{
    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',
    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',
    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',
    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'
}

PyBankID and QR codes

PyBankID can generate QR codes for you, and there is an example application in the examples folder of the repo, where a Flask application called qrdemo shows one way to do authentication with animated QR codes.

The QR code content generation is done with the generate_qr_code_content method on the BankID Client instances, or directly through the identically named method in bankid.qr module.

Certificates

Production certificates

If you want to use BankID in a production environment, then you will have to purchase this service from one of the selling banks. They will then provide you with a certificate that can be used to authenticate your company/application with the BankID servers.

This certificate has to be processed somewhat to be able to use with PyBankID, and how to do this depends on what the selling bank provides you with.

Test certificate

The certificate to use when developing against the BankID test servers can be obtained through PyBankID:

import os
import bankid
dir_to_save_cert_and_key_in = os.path.expanduser('~')
cert_and_key = bankid.create_bankid_test_server_cert_and_key(
    dir_to_save_cert_and_key_in)
print(cert_and_key)
['/home/hbldh/certificate.pem', '/home/hbldh/key.pem']
client = bankid.BankIDClient(
    certificates=cert_and_key, test_server=True)

Release files for pybankid 1.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybankid 1.5.0
File Size Uploaded
pybankid-1.5.0.tar.gz 48.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pybankid 1.5.0
File Interpreter ABI Platform
pybankid-1.5.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 84.5 kB

Release files / pybankid-1.5.0.tar.gz

Download URL pybankid-1.5.0.tar.gz
Size 48.2 kB
Tags Source
SHA-256 checksum
How to use checksums
67b817fec55afdec9ebe068462614515e5e72f929d917fe23e34139dd9f36bd5
BLAKE2b-256 checksum
How to use checksums
e1e9eec9e79d6dab679b691137b4587435535e6711904cfa8c61dd994193a90f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / pybankid-1.5.0-py2.py3-none-any.whl

Download URL pybankid-1.5.0-py2.py3-none-any.whl
Size 36.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8cddd77fcf248a483cc5249277623f5ce30bc248b543f2705511fb254eb71add
BLAKE2b-256 checksum
How to use checksums
b79636f8061b4a2a336a6f8741d5b47655bb23d5103c617dd53468bd86a0c857
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release history Release notifications | RSS feed

This release

1.5.0 This release

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.14.1

2 release files

0.13.1

2 release files

0.10.3

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

1 release file

0.6.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.4.0

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.1

1 release file

0.3.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page