py_webauthn

A WebAuthn Python module.

Github星跟蹤圖

PyWebAuthn

.. image:: https://img.shields.io/pypi/v/webauthn.svg
:target: https://pypi.python.org/pypi/webauthn
:alt: PyPI

.. image:: https://img.shields.io/badge/license-BSD-blue.svg
:target: https://raw.githubusercontent.com/duo-labs/py_webauthn/master/LICENSE
:alt: GitHub license

PyWebAuthn is a Python module which can be used to handle WebAuthn <https://www.w3.org/TR/webauthn/>_ registration and assertion. Currently, WebAuthn is supported in Firefox <https://www.mozilla.org/en-US/firefox/new/>\ , Chrome <https://www.google.com/chrome/>\ , and Edge <https://www.microsoft.com/en-us/windows/microsoft-edge>_.

Installation

pip install webauthn

Usage

Generating credential options, (to be passed to navigator.credentials.create\ ):

.. code-block:: python

make_credential_options = webauthn.WebAuthnMakeCredentialOptions(
challenge,
rp_name,
rp_id,
user_id,
username,
display_name,
icon_url)

Creating a WebAuthnUser object. Used during the assertion (login) process:

.. code-block:: python

webauthn_user = webauthn.WebAuthnUser(
user.id,
user.username,
user.display_name,
user.icon_url,
user.credential_id,
user.pub_key,
user.sign_count,
user.rp_id)

Generating assertion options, (to be passed to navigator.credentials.get\ ):

.. code-block:: python

webauthn_assertion_options = webauthn.WebAuthnAssertionOptions(
webauthn_user,
challenge)

Verifying a registration response, (result of navigator.credentials.create\ ):

.. code-block:: python

webauthn_registration_response = webauthn.WebAuthnRegistrationResponse(
RP_ID,
ORIGIN,
registration_response,
challenge,
trust_anchor_dir,
trusted_attestation_cert_required,
self_attestation_permitted,
none_attestation_permitted,
uv_required=False) # User Verification

try:
webauthn_credential = webauthn_registration_response.verify()
except Exception as e:
return jsonify({'fail': 'Registration failed. Error: {}'.format(e)})

Create User

Verifying an assertion response, (result of navigator.credentials.get\ ):

.. code-block:: python

webauthn_user = webauthn.WebAuthnUser(
user.ukey,
user.username,
user.display_name,
user.icon_url,
user.credential_id,
user.pub_key,
user.sign_count,
user.rp_id)

webauthn_assertion_response = webauthn.WebAuthnAssertionResponse(
webauthn_user,
assertion_response,
challenge,
origin,
uv_required=False) # User Verification

try:
sign_count = webauthn_assertion_response.verify()
except Exception as e:
return jsonify({'fail': 'Assertion failed. Error: {}'.format(e)})

Update counter.

user.sign_count = sign_count

Flask Demo

There is a Flask <http://flask.pocoo.org/>_ demo available in the flask_demo directory. Follow these steps to run the Flask web app:

#. cd flask_demo
#. pip install -r requirements.txt
#. python create_db.py
#. python app.py
#. Go to https://localhost:5000 <https://localhost:5000>_ in your web browser. Try registering and logging in with a compatible U2F or WebAuthn authenticator.
#. Profit?

Flask Demo (Docker)

To run the Flask <http://flask.pocoo.org/>_ demo with Docker <https://www.docker.com/>_\ :

#. Install Docker.
#. docker-compose up -d
#. Go to https://localhost:5000 <https://localhost:5000>_ in your web browser. Try registering and logging in with a compatible U2F or WebAuthn authenticator.

Unit Tests

To run the unit tests, use the following command:

python3 -m unittest tests/webauthn_test.py

Note

Currently, PyWebAuthn does not support performing the following verifications.

  • Token Binding ID <https://www.w3.org/TR/webauthn/#dom-collectedclientdata-tokenbindingid>_
  • Authenticator Extensions <https://www.w3.org/TR/webauthn/#dom-collectedclientdata-authenticatorextensions>_

主要指標

概覽
名稱與所有者duo-labs/py_webauthn
主編程語言Python
編程語言Python (語言數: 1)
平台
許可證BSD 3-Clause "New" or "Revised" License
所有者活动
創建於2017-11-11 02:02:28
推送於2025-10-01 07:28:42
最后一次提交2025-10-01 06:18:18
發布數47
最新版本名稱v2.8.0-alpha1 (發布於 )
第一版名稱v0.1 (發布於 2018-03-06 03:59:09)
用户参与
星數1k
關注者數33
派生數181
提交數560
已啟用問題?
問題數107
打開的問題數0
拉請求數99
打開的拉請求數2
關閉的拉請求數44
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?