{"id":21816591,"url":"https://github.com/5monkeys/bankid-sdk","last_synced_at":"2025-04-14T01:15:12.348Z","repository":{"id":210058424,"uuid":"724549170","full_name":"5monkeys/bankid-sdk","owner":"5monkeys","description":"A Python SDK for BankID","archived":false,"fork":false,"pushed_at":"2025-03-07T14:46:18.000Z","size":65,"stargazers_count":5,"open_issues_count":5,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T01:15:02.076Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/5monkeys.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-28T10:02:22.000Z","updated_at":"2024-04-25T19:52:28.000Z","dependencies_parsed_at":"2024-11-27T15:46:48.427Z","dependency_job_id":null,"html_url":"https://github.com/5monkeys/bankid-sdk","commit_stats":null,"previous_names":["5monkeys/bankid-sdk"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fbankid-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fbankid-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fbankid-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5monkeys%2Fbankid-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/5monkeys","download_url":"https://codeload.github.com/5monkeys/bankid-sdk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248804824,"owners_count":21164135,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-27T15:35:26.754Z","updated_at":"2025-04-14T01:15:12.328Z","avatar_url":"https://github.com/5monkeys.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 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    ) -\u003e 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    ) -\u003e 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    ) -\u003e 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    ) -\u003e 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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5monkeys%2Fbankid-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F5monkeys%2Fbankid-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5monkeys%2Fbankid-sdk/lists"}