Python SDK for accessing the Hanko Authentication API

Overview

Hanko Authentication SDK for Python

This package is maintained by Hanko.

Contents

  1. Introduction
  2. Documentation
  3. Installation
  4. Usage
    1. Prerequisites
    2. Create a new Hanko API Client
    3. Register a WebAuthn credential
    4. Authenticate with a registered WebAuthn credential
    5. Making Transactions
    6. Credential Management
  5. Serializing and deserializing Hanko payloads and response
  6. Exception handling
  7. Enable debug logging
  8. Support

Introduction

This SDK provides an API client that lets you communicate with the Hanko Authentication API to easily integrate FIDO®-based authentication into your web application written in Python.

Documentation

Installation

Pip

pip install hanko_sdk

Building a wheel

py -m build
pip install dist/hanko_sdk-X.X.X-py3-none-any.whl

Usage

Prerequisites

In order to utilize the client provided by the SDK you need an API URL as well as API credentials in the form of an API key ID and an API secret. View our getting started guide in the official documentation on how to obtain these.

The minimum supported Python version is 3.7.

Create a new Hanko API Client

Once you have set up your account, create a HankoHttpClientConfig with the API URL, the API Key Id and the API secret and use it to construct a HankoHttpClient.

", " ", " ") hanko_client = HankoHttpClient(hanko_config) ">
hanko_config = HankoHttpClientConfig("
      
       "
      , "
      
       "
      , "
      
       "
      )
hanko_client = HankoHttpClient(hanko_config)

Register a WebAuthn credential

Registration of a WebAuthn credential involves retrieving credential creation options from the Hanko API (initialization), passing these options to the browser's Web Authentication API and lastly sending the WebAuthn response back to the Hanko API for validation (finalization).

For a more complete example of the authentication process, see the implementation guide in the Hanko documentation.

Registration initialization:

Using defaults
" username = " " display_name = " " request = RegistrationInitializationRequest( User( user_id, username, display_name ) ) response = hanko_client.initialize_registration(request) ">
# To create the user object you'll need a userId, a username, and optionally, a
# displayName. The username usually comes either from a form a user provides when
# registering for the first time, or from your existing session
# store or database, as well as a related userId, which may needs to be generated
# if it is a new user.

user_id = "
      
       "
      
username = "
      
       "
      
display_name = "
      
       "
      

request = RegistrationInitializationRequest(
     User(
         user_id,
         username,
         display_name
     )
 )

response = hanko_client.initialize_registration(request)
Modifying registration options

You can modify the default credential creation options for registration as follows:

" username = " " display_name = " " request = RegistrationInitializationRequest( User( user_id, username, display_name ), RegistrationInitializationRequestOptions( AuthenticatorSelection( AuthenticatorAttachment.from_json_serializable(authenticator), True, UserVerificationRequirement.REQUIRED ), ConveyancePreference.NONE ) ) response = hanko_client.initialize_registration(request) ">
user_id = "
      
       "
      
username = "
      
       "
      
display_name = "
      
       "
      

request = RegistrationInitializationRequest(
    User(
        user_id,
        username,
        display_name
    ),
    RegistrationInitializationRequestOptions(
        AuthenticatorSelection(
            AuthenticatorAttachment.from_json_serializable(authenticator),
            True,
            UserVerificationRequirement.REQUIRED
        ),
        ConveyancePreference.NONE
    )
)

response = hanko_client.initialize_registration(request)

Pass Hanko API response to the browser's Web Authentication API

Initialization with the Hanko API returns a response that represent PublicKeyCredentialCreationOptions that must be provided to the browser's WebAuthn Authentication API to create a credential. The WebAuthn Authentication API requires data that looks like JSON but contains binary data, represented as ArrayBuffers, that needs to be encoded. So we can't pass the Hanko API registrationInitializationResponse directly as PublicKeyCredentialCreationOptions, but you can use the Hanko JavaScript WebAuthn Library that wraps the WebAuthn Authentication API and encodes / decodes the data and allows you to easily pass Hanko API responses to the WebAuthn Authentication API and vice versa.

You can provide the registrationInitializationResponse obtained from the hanko_client.initialize_registration(request) directly to the create function of the Hanko JavaScript WebAuthn Library for creating a credential.

For a more complete example of the registration process, see the implementation guide in the Hanko documentation.

Registration finalization

After you have executed the create() function mentioned before and the user has completed the process, you will receive back a response from the browser's WebAuthn API.

Deserialize and pass the Web Authentication API response as returned from the Hanko WebAuthn Library's create function to the finalize_registration client method.

webauthn_response = "{\"id\": \"ATIihVy...\", ...}";
from hanko_sdk import json_serializer

request = json_serializer.deserialize_string(webauthn_response, RegistrationFinalizationRequest)
response = hanko_client.finalize_registration(request)

Authenticate with a registered WebAuthn credential

For a more complete example of the authentication process, see the implementation guide in the Hanko documentation.

Authentication initialization

Using defaults
user_id = "e3be22a7-13cf-4235-a09c-380dfd44ac04"

request = AuthenticationInitializationRequest(
    User(
        user_id
    )
)

response = hanko_client.initialize_authentication(request)
Modifying authentication options

You can modify the default credential request options for authentication as follows:

user_id = "e3be22a7-13cf-4235-a09c-380dfd44ac04"

request = AuthenticationInitializationRequest(
    User(
        user_id
    ),
    AuthenticationInitializationRequestOptions(
        UserVerificationRequirement.REQUIRED,
        AuthenticatorAttachment.PLATFORM
    )
 )

response = hanko_client.initialize_authentication(request)

Pass Hanko API response to Web Authentication API

You can provide the response to the get() function of the Hanko WebAuthn Library for authenticating with a credential. For a more complete example of the authentication process, see the implementation guide in the Hanko documentation.

Authentication finalization

Deserialize and pass the Web Authentication API response as returned from the Hanko WebAutn Library's get() function to the finalize_authentication client method.

webauthn_response = "{\"id\": \"DaNOpBx...\", ...}";

from hanko_sdk import json_serializer

request = json_serializer.deserialize_string(webauthn_response, AuthenticationFinalizationRequest)
response = hankoClient.finalize_authentication(request)

Making transactions

A transaction is technically the equivalent of an authentication, with the difference being that when initializing a transaction, a transaction_text can be included, which becomes part of the authentication challenge.

Transaction initialization

Using defaults
user_id = "e3be22a7-13cf-4235-a09c-380dfd44ac04"

request = TransactionInitializationRequest(
    User(
        user_id
    ),
    "Pay $5 to Bob?"
)

response = hanko_client.initialize_transaction(request)

Pass Hanko API response to Web Authentication API

You can provide the response to the get() function of the Hanko WebAuthn Library for authenticating with a credential. For a more complete example of the authentication process, see the implementation guide in the Hanko documentation.

Transaction finalization

Deserialize and pass the Web Authentication API response as returned from the Hanko WebAutn Library's get() function to the finalize_transaction client method.

webauthn_response = "{\"id\": \"fSmpQnC...\", ...}";

from hanko_sdk import json_serializer

request = json_serializer.deserialize_string(webauthn_response, TransactionFinalizationRequest)
response = hanko_client.finalize_transaction(request)

Credential management

credential_id = "AQohBypyLBrx8R_UO0cWQuu7hhRGv7bPRRGtbQLrjl..."

# Get all details of the specified credential.
credential = hanko_client.get_credential(credential_id)

# Update the name of a credential.
update_request = CredentialUpdateRequest("MySecurityKey")
updated_credential = hanko_client.update_credential(credential_id, update_request)

# Delete the specified credential.
hanko_client.delete_credential(credential_id)

# Search for credentials filtering by userId and paginating results.
query = CredentialQuery(
    "65a3eba6-22cb-4c35-9881-b21fac6acfd0", # userId
    15, # page size
    1 # page
)

credentials = hanko_client.list_credentials(query)

Serializing and deserializing Hanko payloads and response

As the HankoHttpClient works with objects, you may need to serialize or deserialize Hanko payloads and responses. For that you can use the json_serializer module as follows:

# Import the serializer module
from hanko_sdk import json_serializer

# Serialize a TransactionInitializationResponse
transaction_initialization_response = TransactionInitializationResponse()
# ... code for generating the transaction initialization response

transaction_initialization_response_json = hanko_serializer.serialize(transaction_initialization_response)
# ... process the transaction initialization response

# Deserialize a TransactionFinalizationRequest
webauthn_response = "{\"id\": \"fSmpQnC...\", ...}";

transaction_finalization_request = json_serializer.deserialize_string(webauthn_response, TransactionFinalizationRequest)
finalization_response = hanko_client.finalize_transaction(transaction_finalization_request)

Exception handling

" username = " " display_name = " " request = RegistrationInitializationRequest( User( user_id, username, display_name ) ) response hanko_client.initialize_registration(request) except HankoApiException as hanko_api_exception: print(hanko_api_exception) ">
try:
    user_id = "
      
       "
      
    username = "
      
       "
      
    display_name = "
      
       "
      

    request = RegistrationInitializationRequest(
        User(
            user_id,
            username,
            display_name
        )
    )

    response hanko_client.initialize_registration(request)
except HankoApiException as hanko_api_exception:
    print(hanko_api_exception)

Enable debug logging

The HankoHttpClient accepts a logging.Logger instance as an optional constructor parameter, which if not none, will be used for debug logging.

You might also like...
Python SDK for the Buycoins API.

This library provides easy access to the Buycoins API using the Python programming language. It provides all the feature of the API so that you don't need to interact with the API directly. This library can be used with Python 3.6+

qualysclient - a python SDK for interacting with the Qualys API

qualysclient - a python SDK for interacting with the Qualys API

A Python 2.7/3.x module for Amcrest Cameras using the SDK HTTP API.

A Python 2.7/3.x module for Amcrest Cameras using the SDK HTTP API. Amcrest and Dahua devices share similar firmwares. Dahua Cameras and NVRs also work with this module.

An Python SDK for QQ based on mirai-api-http v2.

Argon 一个基于 graia-broadcast 和 mirai-api-http v2 的 Python SDK。 本项目适用于 mirai-api-http 2.0 以上版本。 目前仍处于开发阶段,内部接口可能会有较大的变化。 The Stasis / 停滞 为维持 GraiaProject

An elegant mirai-api-http v2 Python SDK.

Ariadne 一个适用于 mirai-api-http v2 的 Python SDK。 本项目适用于 mirai-api-http 2.0 以上版本。 目前仍处于开发阶段,内部接口可能会有较大的变化。 安装 poetry add graia-ariadne 或 pip install graia

Balsam Python client API & SDK

balsam No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) This Python package is automatically

Python SDK for interacting with the Frame.io API.
Python SDK for interacting with the Frame.io API.

python-frameio-client Frame.io Frame.io is a cloud-based collaboration hub that allows video professionals to share files, comment on clips real-time,

DongTai API SDK For Python

DongTai-SDK-Python Quick start You need a config file config.json { "DongTai":{ "token":"your token", "url":"http://127.0.0.1:90"

Python 3 SDK/Wrapper for Huobi Crypto Exchange Api

This packages intents to be an idiomatic PythonApi wrapper for https://www.huobi.com/ Huobi Api Doc: https://huobiapi.github.io/docs Showcase TODO Con

Releases(v1.0.1)
Owner
Hanko.io
All the tools you need for passwordless authentication.
Hanko.io
A Telegram bot that can stream Telegram files to users over HTTP.

T.ME_FILE_TO_LINK Hi iam a file to link bot....best Bot telegram Telegram File To Link Generation Bot A Telegram bot that can stream Telegram files to

1 Oct 24, 2021
𝗖𝝠𝝦𝝩𝝠𝝞𝝥 𝝦𝗥𝝞𝗖𝝽°™️ 🇱🇰 Is An All In One Media Inline Bot Made For Inline Your Media Effectively With Some Advance Security Tools♥️

𝗖𝝠𝝦𝝩𝝠𝝞𝝥 𝝦𝗥𝝞𝗖𝝽° ™️ 🇱🇰 𝗙𝗘𝝠𝝩𝗨𝗥𝗘𝗦 Auto Filter IMDB Admin Commands Broadcast Index IMDB Search Inline Search Random Pics Ids & User I

Kɪꜱᴀʀᴀ Pᴇꜱᴀɴᴊɪᴛʜ Pᴇʀᴇʀᴀ 〄 13 Jun 21, 2022
A minimal open source mtg-like tcg game made in python that can be played on a terminal emulator using a keyboard.

TCG-TERM Project state: 🔧 🚧 🚧 🚧 Incomplete, In development 🚧 🚧 🚧 👷 (Keep in mind that at the moment, This project is currently undone, and wil

Amos 3 Aug 29, 2021
Discord-selfbot - Very basic discord self bot

discord-selfbot Very basic discord self bot still being actively developed requi

nana 4 Apr 07, 2022
A simple Python library to integrate with the Heron Data API

Heron Python This library provides easy access to the Heron Data API from applications written in Python. Documentation No language-specific docs are

Heron Data 11 Nov 11, 2022
SIGIT - Simple Information Gathering Toolkit

SIGIT - Simple Information Gathering Toolkit Features userrecon - username reconnaissance facedumper - dump facebook information mailfinder - find ema

Termux Hackers 437 Dec 29, 2022
Ice-Userbot adalah userbot Telegram modular yang berjalan di Python3 dengan database sqlalchemy

Ice-Userbot Telegram Ice-Userbot adalah userbot Telegram modular yang berjalan di Python3 dengan database sqlalchemy. Berbasis Paperplane dan ProjectB

6 Apr 29, 2022
Python API for British Geological Survey magnetic field calculator

Magnetic field calculator Python API for British Geological Survey magnetic field calculator. Description This project magnetic field calculator. It u

Filip Š 3 Mar 11, 2022
Python tool to Check running WebClient services on multiple targets based on @leechristensen

WebClient Service Scanner Python tool to Check running WebClient services on multiple targets based on @tifkin_ idea. This tool uses impacket project.

Pixis 153 Dec 28, 2022
AWS Auto Inventory allows you to quickly and easily generate inventory reports of your AWS resources.

Photo by Denny Müller on Unsplash AWS Automated Inventory ( aws-auto-inventory ) Automates creation of detailed inventories from AWS resources. Table

AWS Samples 123 Dec 26, 2022
CnCL - CnCLess it's an Easy to deploy Botnet without CnC/C2

CnCL CnCLess it's an Easy to deploy Botnet without CnC/C2, Harder to track and t

ZSendokame 2 Jan 10, 2022
Deezer client for python

Deezer Python Client A friendly Python wrapper around the Deezer API. Installation The package is published on PyPI and can be installed by running: p

Bruno Alla 103 Dec 19, 2022
A python script that changes our background based on current weather and time of the day.

Desktop background on Windows 10, based on current weather and time A python script that changes our background based on current weather and time of t

Maj Gaberšček 1 Nov 16, 2021
Tesseract Open Source OCR Engine (main repository)

Tesseract OCR About This package contains an OCR engine - libtesseract and a command line program - tesseract. Tesseract 4 adds a new neural net (LSTM

48.3k Jan 05, 2023
Messing around with GitHub API to look at omicron build times

gh-workflow-runs This is a very simple tool to dump out basic information about workflow runs for a GitHub repo. The structure is based on gh-subscrip

David Pacheco 1 Nov 30, 2021
Nyon-stream - A python script that uses webtorrent to stream nyaa videos directly to mpv

nyon-stream A rather shitty script that uses webtorrent to stream nyaa videos di

18 Feb 08, 2022
TG-Url-Uploader-Bot - Telegram RoBot to Upload Links

MW-URL-Uploader Bot Telegram RoBot to Upload Links. Features: 👉 Only Auth Users

Aadhi 3 Jun 27, 2022
A discord token grabber made in Python 3

Discord Token Grabber A Discord token grabber written in Python 3. This version of the grabber only supports Windows. Features Transfers via Discord w

Mega145 4 Aug 04, 2022
Discord Token Creator 🥵

Discord Token Creator 🥵

dropout 304 Jan 03, 2023
A bot to get Statistics like the Playercount from your Minecraft-Server on your Discord-Server

Hey Thanks for reading me. Warning: My English is not the best I have programmed this bot to show me statistics about the player numbers and ping of m

spaffel 12 Sep 24, 2022