跳转到主要内容

Pythonic WebAuthn

项目描述

py_webauthn

PyPI GitHub license Pythonic WebAuthn

这是一个Python3的实现,关注于WebAuthn API的服务器端,旨在使利用WebAuthn的力量变得容易。WebAuthn API

此库支持所有FIDO2兼容的认证器,包括安全密钥、Touch ID、Face ID、Windows Hello、Android生物识别……几乎所有其他东西。

安装

此模块可在PyPI上使用

pip安装webauthn

要求

  • Python 3.8及以上

用法

该库在根webauthn模块上仅公开了几个核心方法

  • generate_registration_options()
  • verify_registration_response()
  • generate_authentication_options()
  • verify_authentication_response()

还公开了两个额外的辅助方法

  • options_to_json()
  • base64url_to_bytes()

webauthn.helpers.structs上还有其他数据结构可用。这些数据类对于构建上述方法的输入以及为提供类型提示以帮助确保传递数据的形状的一致性非常有用。

通常,该库假定实施此库的受信任方将以以下方式与处理调用WebAuthn API的网页进行交互

  • JSON是用于从服务器将注册和认证选项传输到网页,并分别提供给navigator.credentials.create()navigator.credentials.get()的推荐数据类型。
  • JSON是用于从浏览器将WebAuthn响应传输到服务器的推荐数据类型。
  • 字节不是作为JSON直接传输的,因此应将其编码为base64url和解码,以避免引入WebAuthn规范中未指定的任何依赖。

下面提到的示例包括使用options_to_json()辅助函数(见上文)来展示如何轻松地将注册和认证选项中的bytes值编码为base64url,以便传输到前端。

示例还包括演示如何使用base64url编码将JSON化的响应(包括ArrayBuffer值)传递到parse_registration_credential_jsonparse_authentication_credential_json中,以便由该库中的方法自动解析。RP可以将此与相应的自定义前端逻辑或几个特定于前端库(例如@simplewebauthn/browser)结合使用,以处理这些值到和从JSON的编码和解码。

此库方法中定义为bytes的其他参数旨在存储在服务器上。这些值可以作为bytes存在,而无需进行可能多余的编码和解码到其他格式。任何编码或解码这些值以在WebAuthn仪式的步骤之间存储的操作,均由RP以特定于实现的方 式完成。

注册

有关使用generate_registration_options()verify_registration_response()的实际示例,请参阅examples/registration.py

您也可以使用以下命令运行这些示例

# See "Development" below for venv setup instructions
venv $> python -m examples.registration

认证

有关使用generate_authentication_options()verify_authentication_response()的实际示例,请参阅examples/authentication.py

您也可以使用以下命令运行这些示例

# See "Development" below for venv setup instructions
venv $> python -m examples.authentication

开发

安装

设置虚拟环境,然后安装项目的需求

$> python3 -m venv venv
$> source venv/bin/activate
venv $> pip install -r requirements.txt

测试

可以使用Python的unittest模块执行tests/目录中的所有内容

venv $> python -m unittest

可以使用nodemon等工具实现自动监视unittests。

所有测试

venv $> nodemon --exec "python -m unittest" --ext py

单个测试文件

venv $> nodemon --exec "python -m unittest tests/test_aaguid_to_string.py" --ext py

代码检查和格式化

代码检查通过mypy处理

venv $> python -m mypy webauthn
Success: no issues found in 52 source files

整个库使用black进行格式化

venv $> python -m black webauthn --line-length=99
All done!  🍰 52 files left unchanged.

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

webauthn-2.2.0.tar.gz (114.0 kB 查看散列值)

上传时间

构建分发

webauthn-2.2.0-py3-none-any.whl (67.7 kB 查看散列值)

上传时间 Python 3

支持