{"id":17272496,"url":"https://github.com/hbldh/pybankid","last_synced_at":"2025-05-08T00:25:49.089Z","repository":{"id":20587353,"uuid":"23868031","full_name":"hbldh/pybankid","owner":"hbldh","description":"BankID Relying Party client for Python","archived":false,"fork":false,"pushed_at":"2024-04-24T20:31:49.000Z","size":339,"stargazers_count":45,"open_issues_count":2,"forks_count":19,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-04-28T15:49:01.358Z","etag":null,"topics":["authentication","bankid","python"],"latest_commit_sha":null,"homepage":"https://pybankid.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hbldh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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":"2014-09-10T09:08:34.000Z","updated_at":"2024-08-23T10:40:13.073Z","dependencies_parsed_at":"2023-12-15T14:26:30.306Z","dependency_job_id":"490cf0c9-af4b-4b2f-9417-49f3edcb38f8","html_url":"https://github.com/hbldh/pybankid","commit_stats":{"total_commits":156,"total_committers":12,"mean_commits":13.0,"dds":0.532051282051282,"last_synced_commit":"4587e43abcff0ce316a91f06905231e11b2bc335"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbldh%2Fpybankid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbldh%2Fpybankid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbldh%2Fpybankid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbldh%2Fpybankid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hbldh","download_url":"https://codeload.github.com/hbldh/pybankid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252975075,"owners_count":21834371,"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":["authentication","bankid","python"],"created_at":"2024-10-15T08:48:47.760Z","updated_at":"2025-05-08T00:25:49.066Z","avatar_url":"https://github.com/hbldh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PyBankID\n\n![Build and Test](https://github.com/hbldh/pybankid/workflows/Build%20and%20Test/badge.svg)\n![Documentation Status](https://readthedocs.org/projects/pybankid/badge/?version=latest)\n![PyPI Version](https://img.shields.io/pypi/v/pybankid)\n![PyPI License](https://img.shields.io/pypi/l/pybankid)\n\nPyBankID 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.\n\nThe 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.\n\nIf you intend to use PyBankID in your project, you are advised to read the [BankID Integration Guide](https://www.bankid.com/en/utvecklare/guider/teknisk-integrationsguide) before doing anything else. There, one can find information about how the BankID methods are defined and how to use them.\n\n## Installation\n\nPyBankID can be installed through pip:\n\n```bash\npip install pybankid\n```\n\n## Usage\n\nPyBankID 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.\n\n### Synchronous client\n\n```python\nfrom bankid import BankIDClient\nclient = BankIDClient(certificates=(\n    'path/to/certificate.pem',\n    'path/to/key.pem',\n))\n```\n\nConnection 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.\n\nAll authentication and signing calls require the end user's IP address to be included. An authentication order is initiated as such:\n\n```python\nclient.authenticate(end_user_ip='194.168.2.25')\n{\n    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',\n    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',\n    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',\n    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'\n}\n```\n\nand a sign order is initiated in a similar fashion:\n\n```python\nclient.sign(\n    end_user_ip='194.168.2.25',\n    user_visible_data=\"The information to sign.\"\n)\n{\n    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',\n    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',\n    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',\n    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'\n}\n```\n\nIf you want to ascertain that only one individual can authenticate or sign, you can specify this using the `requirement` keyword:\n\n```python\nclient.sign(\n   end_user_ip='194.168.2.25',\n   user_visible_data=\"The information to sign.\"\n   requirement={\"personalNumber\": \"YYYYMMDDXXXX\"}\n)\n{\n    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',\n    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',\n    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',\n    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'\n}\n```\n\nIf 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.\n\nThe status of an order can then be studied by polling with the `collect` method using the received `orderRef`:\n\n```python\nclient.collect(order_ref=\"a9b791c3-459f-492b-bf61-23027876140b\")\n{\n    'hintCode': 'outstandingTransaction',\n    'orderRef': 'a9b791c3-459f-492b-bf61-23027876140b',\n    'status': 'pending'\n}\n```\n\n```python\nclient.collect(order_ref=\"a9b791c3-459f-492b-bf61-23027876140b\")\n{\n    'hintCode': 'userSign',\n    'orderRef': 'a9b791c3-459f-492b-bf61-23027876140b',\n    'status': 'pending'\n}\n```\n\n```python\nclient.collect(order_ref=\"a9b791c3-459f-492b-bf61-23027876140b\")\n{\n    'completionData': {\n        'cert': {\n            'notAfter': '1581289199000',\n            'notBefore': '1518130800000'\n        },\n        'device': {\n            'ipAddress': '0.0.0.0'\n        },\n        'ocspResponse': 'MIIHegoBAKCCB[...]',\n        'signature': 'PD94bWwgdmVyc2lv[...]',\n        'user': {\n            'givenName': 'Namn',\n            'name': 'Namn Namnsson',\n            'personalNumber': 'YYYYMMDDXXXX',\n            'surname': 'Namnsson'\n        }\n    },\n    'orderRef': 'a9b791c3-459f-492b-bf61-23027876140b',\n    'status': 'complete'\n}\n```\n\nPlease note that the `collect` method should be used sparingly: in the [BankID Integration Guide](https://www.bankid.com/en/utvecklare/guider/teknisk-integrationsguide) it is specified that _\"collect should be called every two seconds and must not be called more frequent than once per second\"_.\n\nPyBankID also implements the `phone/auth` and `phone/sign` methods, for performing authentication and signing with\nusers that are contacted through phone. For documentation on this, see [PyBankID's Read the Docs page](https://pybankid.readthedocs.io/en/latest/).\n\n### Asynchronous client\n\nThe asynchronous client is used in the same way as the synchronous client, with the difference that all request are performed asynchronously.\n\nThe synchronous guide above can be used as a reference for the asynchronous client as well, by simply adding the `await` keyword:\n\n```python\nfrom bankid import BankIDAsyncClient\nclient = BankIDAsyncClient(certificates=(\n    'path/to/certificate.pem',\n    'path/to/key.pem',\n))\n\nawait client.authenticate(end_user_ip='194.168.2.25')\n{\n    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',\n    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',\n    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',\n    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'\n}\n```\n\n## PyBankID and QR codes\n\nPyBankID can generate QR codes for you, and 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.\n\nThe QR code content generation is done with the `generate_qr_code_content` method on the BankID Client instances, or directly\nthrough the identically named method in `bankid.qr` module.\n\n## Certificates\n\n### Production certificates\n\nIf you want to use BankID in a production environment, then you will have to purchase this service from one of the [selling banks](https://www.bankid.com/foretag/anslut-foeretag). They will then provide you with a certificate that can be used to authenticate your company/application with the BankID servers.\n\nThis 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.\n\n### Test certificate\n\nThe certificate to use when developing against the BankID test servers can be obtained through PyBankID:\n\n```python\nimport os\nimport bankid\ndir_to_save_cert_and_key_in = os.path.expanduser('~')\ncert_and_key = bankid.create_bankid_test_server_cert_and_key(\n    dir_to_save_cert_and_key_in)\nprint(cert_and_key)\n['/home/hbldh/certificate.pem', '/home/hbldh/key.pem']\nclient = bankid.BankIDClient(\n    certificates=cert_and_key, test_server=True)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhbldh%2Fpybankid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhbldh%2Fpybankid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhbldh%2Fpybankid/lists"}