Package: bankid-sdk
BankID-SDKA Python SDK for BankIDGetting startedActionsIn order to interact with theauthandsignBankID order flows,bankid-sdkis 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 namedinitializeand will be invoked just before any
order is initialised via the BankID web API.The second callback is namedfinalizeand will be invoked as soon as acompletedorder 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 orderTo implement an action designated for an authentication order you would create a
subclass ofbankid_sdk.AuthAction.Action for a BankID sign orderTo implement an action designated for a sign order you would create a subclass
ofbankid_sdk.SignAction.Configurationbankid-sdkneeds to be configured before it can work properly. Configuration
is done by callingbankid_sdk.configure(...)with relevant values.fromtypingimportAnyimportbankid_sdkclassBankIDLoginAction(bankid_sdk.AuthAction):"""My fancy action that logs in a user."""name="LOGIN"definitialize(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)returnauth_data,{}deffinalize(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 DjangoThebankid-sdkpackage includes a couple of contributed pieces forDjango:Three predeclared and configurable Django views, all accepting a JSON request body:authcheckcancelA storage backend utilising Django's cache, calledCacheStorageExample setupTo quickly get up and running with your BankID integration with Django you can register
the predeclared JSON based views and configurebankid-sdkto store results in the
cache.Register the Django views frombankid-sdk# urls.pyfrombankid_sdk.contrib.djangoimportrestfromdjango.urlsimportpathurlpatterns=[path("auth/",rest.auth,name="auth"),path("check/",rest.check,name="check"),path("cancel/",rest.cancel,name="cancel"),]An example login actionfromtypingimportAnyimportbankid_sdkfromdjango.contrib.authimportauthenticate,loginclassBankIDLoginAction(bankid_sdk.AuthAction):name="LOGIN"definitialize(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)returnauth_data,contextdeffinalize(self,response:bankid_sdk.CompleteCollect,request:Any,context:Any)->None:user=authenticate(request,personal_number=response.completion_data.user.personal_number)ifuserisNone:raisebankid_sdk.FinalizeFailed(detail="No registered user found")login(request,user)The aboveauthenticatecall from Django requireswriting a custom
authentication backendthat expects apersonal_numberkeyword argument. As such you would probably
also need to store a personal number in relation to your user.Configuringimportbankid_sdkfrombankid_sdk.contrib.django.storageimportCacheStoragebankid_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 viewsAll endpoints expects aPOSTrequest with JSON content type body.authOn success it initiates a new authentication order.checkChecks for a result regarding an authentication or sign order.cancelCancels an ongoing sign or auth order.