bankid-sdk


Namebankid-sdk JSON
Version 0.0.1 PyPI version JSON
download
home_page
SummaryA Python SDK for BankID
upload_time2023-12-14 12:10:35
maintainer
docs_urlNone
author
requires_python>=3.9
license
keywords bankid client django rest
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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.

1. The first callback is named `initialize` and will be invoked just before any
  order is initialised via the BankID web API.
2. The second callback is named `finalize` and 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.

```python
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](https://docs.djangoproject.com/):

- Three predeclared and configurable Django views, all accepting a JSON request body:
  - `auth`
  - `check`
  - `cancel`
- 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`

```python
# 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

```python
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](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#writing-an-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

```python
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.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "bankid-sdk",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "bankid,client,django,rest",
    "author": "",
    "author_email": "Jonas Lundberg <jonas@5monkeys.se>",
    "download_url": "https://files.pythonhosted.org/packages/e7/2c/ebd353ec6764a18b7dc959c407f242df526401fe10de70ea530f588b13aa/bankid_sdk-0.0.1.tar.gz",
    "platform": null,
    "description": "# BankID-SDK\n\nA Python SDK for BankID\n\n## Getting started\n\n### Actions\n\nIn order to interact with the `auth` and `sign` BankID order flows, `bankid-sdk`\nis expected to be configured with actions. An action essentially declares two\ncallbacks that will be invoked during two different phases of order flows.\n\n1. The first callback is named `initialize` and will be invoked just before any\n  order is initialised via the BankID web API.\n2. The second callback is named `finalize` and will be invoked as soon as a\n  _completed_ order has been retrieved from the BankID web API.\n\nImplementing actions will be your main entrypoint for incorporating your\nrequired business logic with the BankID order flows.\n\n#### Action for a BankID authentication order\n\nTo implement an action designated for an authentication order you would create a\nsubclass of `bankid_sdk.AuthAction`.\n\n#### Action for a BankID sign order\n\nTo implement an action designated for a sign order you would create a subclass\nof `bankid_sdk.SignAction`.\n\n### Configuration\n\n`bankid-sdk` needs to be configured before it can work properly. Configuration\nis done by calling `bankid_sdk.configure(...)` with relevant values.\n\n```python\nfrom typing import Any\n\nimport bankid_sdk\n\n\nclass BankIDLoginAction(bankid_sdk.AuthAction):\n    \"\"\"\n    My fancy action that logs in a user.\n    \"\"\"\n    name = \"LOGIN\"\n\n    def initialize(\n        self, request: Any, context: Any\n    ) -> tuple[bankid_sdk.UserAuthData, dict[str, Any] | None]:\n        auth_data = bankid_sdk.UserAuthData(\n            visible=\"Login with BankID\", non_visible=None, visible_format=None\n        )\n        return auth_data, {}\n\n    def finalize(\n        self, response: bankid_sdk.CompleteCollect, request: Any, context: Any\n    ) -> None:\n        # Do login\n        ...\n\n\nbankid_sdk.configure(\n    api_base_url=\"https://appapi2.test.bankid.com/\",\n    storage=...,\n    actions=[BankIDLoginAction],\n    certificate=(\n        \"path/to/bankid/ssl/cert.pem\",\n        \"path/to/bankid/ssl/private_key.pem\",\n    ),\n    ca_cert=\"path/to/bankid/root.crt\",\n)\n```\n\n## Usage with Django\n\nThe `bankid-sdk` package includes a couple of contributed pieces for\n[Django](https://docs.djangoproject.com/):\n\n- Three predeclared and configurable Django views, all accepting a JSON request body:\n  - `auth`\n  - `check`\n  - `cancel`\n- A storage backend utilising Django's cache, called `CacheStorage`\n\n### Example setup\n\nTo quickly get up and running with your BankID integration with Django you can register\nthe predeclared JSON based views and configure `bankid-sdk` to store results in the\ncache.\n\n#### Register the Django views from `bankid-sdk`\n\n```python\n# urls.py\nfrom bankid_sdk.contrib.django import rest\nfrom django.urls import path\n\nurlpatterns = [\n    path(\"auth/\", rest.auth, name=\"auth\"),\n    path(\"check/\", rest.check, name=\"check\"),\n    path(\"cancel/\", rest.cancel, name=\"cancel\"),\n]\n```\n\n#### An example login action\n\n```python\nfrom typing import Any\n\nimport bankid_sdk\nfrom django.contrib.auth import authenticate, login\n\n\nclass BankIDLoginAction(bankid_sdk.AuthAction):\n    name = \"LOGIN\"\n\n    def initialize(\n        self, request: Any, context: Any\n    ) -> tuple[bankid_sdk.UserAuthData, dict[str, Any] | None]:\n        auth_data = bankid_sdk.UserAuthData(\n            visible=\"Login to my site\", non_visible=None, visible_format=None\n        )\n        return auth_data, context\n\n    def finalize(\n        self, response: bankid_sdk.CompleteCollect, request: Any, context: Any\n    ) -> None:\n        user = authenticate(\n            request, personal_number=response.completion_data.user.personal_number\n        )\n        if user is None:\n            raise bankid_sdk.FinalizeFailed(detail=\"No registered user found\")\n\n        login(request, user)\n```\n\nThe above `authenticate` call from Django requires [writing a custom\nauthentication backend](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#writing-an-authentication-backend)\nthat expects a `personal_number` keyword argument. As such you would probably\nalso need to store a personal number in relation to your user.\n\n#### Configuring\n\n```python\nimport bankid_sdk\nfrom bankid_sdk.contrib.django.storage import CacheStorage\n\nbankid_sdk.configure(\n    api_base_url=\"https://appapi2.test.bankid.com/\",\n    storage=CacheStorage(),\n    actions=[BankIDLoginAction],\n    certificate=(\n        \"path/to/bankid/ssl/cert.pem\",\n        \"path/to/bankid/ssl/private_key.pem\",\n    ),\n    ca_cert=\"path/to/bankid/root.crt\",\n)\n```\n\n### More about the included Django views\n\nAll endpoints expects a `POST` request with JSON content type body.\n\n#### `auth`\n\nOn success it initiates a new authentication order.\n\n#### `check`\n\nChecks for a result regarding an authentication or sign order.\n\n#### `cancel`\n\nCancels an ongoing sign or auth order.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A Python SDK for BankID",
    "version": "0.0.1",
    "project_urls": {
        "Homepage": "https://github.com/5monkeys/bankid-sdk",
        "Source": "https://github.com/5monkeys/bankid-sdk",
        "Tracker": "https://github.com/5monkeys/bankid-sdk/issues"
    },
    "split_keywords": [
        "bankid",
        "client",
        "django",
        "rest"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5178f2defdbb6554779a436757fef812ec368420ed46bb7ba44ea34f77cf219e",
                "md5": "e30dc3d7fdd3f314016377208732fff3",
                "sha256": "2de537fc971d2865709c9ebbf62cbfbc39fea71fa13be38e88c13d21ec0225a5"
            },
            "downloads": -1,
            "filename": "bankid_sdk-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e30dc3d7fdd3f314016377208732fff3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 21859,
            "upload_time": "2023-12-14T12:10:33",
            "upload_time_iso_8601": "2023-12-14T12:10:33.437604Z",
            "url": "https://files.pythonhosted.org/packages/51/78/f2defdbb6554779a436757fef812ec368420ed46bb7ba44ea34f77cf219e/bankid_sdk-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e72cebd353ec6764a18b7dc959c407f242df526401fe10de70ea530f588b13aa",
                "md5": "efd219afcd07f8cd3f21a37a13421969",
                "sha256": "4cb2b0fbb91af02edea3a106c24811658d4fd323b1ddcce8f46197f0bbd2db41"
            },
            "downloads": -1,
            "filename": "bankid_sdk-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "efd219afcd07f8cd3f21a37a13421969",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 34435,
            "upload_time": "2023-12-14T12:10:35",
            "upload_time_iso_8601": "2023-12-14T12:10:35.030655Z",
            "url": "https://files.pythonhosted.org/packages/e7/2c/ebd353ec6764a18b7dc959c407f242df526401fe10de70ea530f588b13aa/bankid_sdk-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-14 12:10:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "5monkeys",
    "github_project": "bankid-sdk",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "bankid-sdk"
}
        
Elapsed time: 0.62090s