A Python SDK for BankID
Project description
BankID-SDK
A Python SDK for BankID
Getting started
Actions
In order to interact with the auth and sign BankID order flows, bankid-sdk
is expected to be configured with actions. An action essentially declares two
callbacks that will be invoked during two different phases of order flows.
- The first callback is named
initializeand will be invoked just before any order is initialised via the BankID web API. - The second callback is named
finalizeand will be invoked as soon as a completed order has been retrieved from the BankID web API.
Implementing actions will be your main entrypoint for incorporating your required business logic with the BankID order flows.
Action for a BankID authentication order
To implement an action designated for an authentication order you would create a
subclass of bankid_sdk.AuthAction.
Action for a BankID sign order
To implement an action designated for a sign order you would create a subclass
of bankid_sdk.SignAction.
Configuration
bankid-sdk needs to be configured before it can work properly. Configuration
is done by calling bankid_sdk.configure(...) with relevant values.
from typing import Any
import bankid_sdk
class BankIDLoginAction(bankid_sdk.AuthAction):
"""
My fancy action that logs in a user.
"""
name = "LOGIN"
def initialize(
self, request: Any, context: Any
) -> tuple[bankid_sdk.UserAuthData, dict[str, Any] | None]:
auth_data = bankid_sdk.UserAuthData(
visible="Login with BankID", non_visible=None, visible_format=None
)
return auth_data, {}
def finalize(
self, response: bankid_sdk.CompleteCollect, request: Any, context: Any
) -> None:
# Do login
...
bankid_sdk.configure(
api_base_url="https://appapi2.test.bankid.com/",
storage=...,
actions=[BankIDLoginAction],
certificate=(
"path/to/bankid/ssl/cert.pem",
"path/to/bankid/ssl/private_key.pem",
),
ca_cert="path/to/bankid/root.crt",
)
Usage with Django
The bankid-sdk package includes a couple of contributed pieces for
Django:
- Three predeclared and configurable Django views, all accepting a JSON request body:
authcheckcancel
- A storage backend utilising Django's cache, called
CacheStorage
Example setup
To quickly get up and running with your BankID integration with Django you can register
the predeclared JSON based views and configure bankid-sdk to store results in the
cache.
Register the Django views from bankid-sdk
# urls.py
from bankid_sdk.contrib.django import rest
from django.urls import path
urlpatterns = [
path("auth/", rest.auth, name="auth"),
path("check/", rest.check, name="check"),
path("cancel/", rest.cancel, name="cancel"),
]
An example login action
from typing import Any
import bankid_sdk
from django.contrib.auth import authenticate, login
class BankIDLoginAction(bankid_sdk.AuthAction):
name = "LOGIN"
def initialize(
self, request: Any, context: Any
) -> tuple[bankid_sdk.UserAuthData, dict[str, Any] | None]:
auth_data = bankid_sdk.UserAuthData(
visible="Login to my site", non_visible=None, visible_format=None
)
return auth_data, context
def finalize(
self, response: bankid_sdk.CompleteCollect, request: Any, context: Any
) -> None:
user = authenticate(
request, personal_number=response.completion_data.user.personal_number
)
if user is None:
raise bankid_sdk.FinalizeFailed(detail="No registered user found")
login(request, user)
The above authenticate call from Django requires writing a custom
authentication backend
that expects a personal_number keyword argument. As such you would probably
also need to store a personal number in relation to your user.
Configuring
import bankid_sdk
from bankid_sdk.contrib.django.storage import CacheStorage
bankid_sdk.configure(
api_base_url="https://appapi2.test.bankid.com/",
storage=CacheStorage(),
actions=[BankIDLoginAction],
certificate=(
"path/to/bankid/ssl/cert.pem",
"path/to/bankid/ssl/private_key.pem",
),
ca_cert="path/to/bankid/root.crt",
)
More about the included Django views
All endpoints expects a POST request with JSON content type body.
auth
On success it initiates a new authentication order.
check
Checks for a result regarding an authentication or sign order.
cancel
Cancels an ongoing sign or auth order.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bankid_sdk-0.0.2.tar.gz.
File metadata
- Download URL: bankid_sdk-0.0.2.tar.gz
- Upload date:
- Size: 36.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c78357c90c25b4e9d22d5b59250faaa9dc13558391136b48ba70ca9c63313335
|
|
| MD5 |
51944dd8014d7c3984d5b74495ac6c29
|
|
| BLAKE2b-256 |
59c0ae8f7acea231f3b186c7b613146f5151559a1f73f122ed205f667c37b767
|
Provenance
The following attestation bundles were made for bankid_sdk-0.0.2.tar.gz:
Publisher:
release.yaml on 5monkeys/bankid-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bankid_sdk-0.0.2.tar.gz -
Subject digest:
c78357c90c25b4e9d22d5b59250faaa9dc13558391136b48ba70ca9c63313335 - Sigstore transparency entry: 654219470
- Sigstore integration time:
-
Permalink:
5monkeys/bankid-sdk@52dfce091eb3de18c2bbdf52a40615a18e436c9e -
Branch / Tag:
- Owner: https://github.com/5monkeys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@52dfce091eb3de18c2bbdf52a40615a18e436c9e -
Trigger Event:
release
-
Statement type:
File details
Details for the file bankid_sdk-0.0.2-py3-none-any.whl.
File metadata
- Download URL: bankid_sdk-0.0.2-py3-none-any.whl
- Upload date:
- Size: 22.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
053993f853f24c1a1de9551d8537cf48ee5fd3b0eb9546b17922b5f8b62283ff
|
|
| MD5 |
2030d70cea2ddde67ec021c885c33cf3
|
|
| BLAKE2b-256 |
e9723c053fc46f36cbcbdadc83d9b9bca66b06fc2bf47570018b0760558419bb
|
Provenance
The following attestation bundles were made for bankid_sdk-0.0.2-py3-none-any.whl:
Publisher:
release.yaml on 5monkeys/bankid-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bankid_sdk-0.0.2-py3-none-any.whl -
Subject digest:
053993f853f24c1a1de9551d8537cf48ee5fd3b0eb9546b17922b5f8b62283ff - Sigstore transparency entry: 654219473
- Sigstore integration time:
-
Permalink:
5monkeys/bankid-sdk@52dfce091eb3de18c2bbdf52a40615a18e436c9e -
Branch / Tag:
- Owner: https://github.com/5monkeys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@52dfce091eb3de18c2bbdf52a40615a18e436c9e -
Trigger Event:
release
-
Statement type: