跳转到主要内容

Python WebAuthn Relying Party库

项目描述

PyWARP是W3C WebAuthn标准的Relying Party组件在Python中的实现。WebAuthn标准通过使用专用硬件安全密钥和生物识别设备(如Yubico YubiKeyGoogle TitanTPMTouch ID)提供高级认证安全,用于两因素、多因素和无密码认证模型。

与更基础的二因素标准如HOTP(RFC 4226)和TOTP(RFC 6238)相比,WebAuthn的FIDO U2F配置文件使用非对称加密,以避免使用共享密钥设计,从而增强了您的认证解决方案对服务器端攻击的防御能力。硬件U2F还将客户端密钥隔离在专用单用途设备中,从而增强了客户端对客户端攻击的防御。通过自动将凭据范围限制为Relying Party ID(应用程序源/域名),U2F还增加了对网络钓鱼攻击的保护。

PyWARP实现了WebAuthn的Relying Party组件。Relying Party是一个服务器端应用程序,它指示浏览器(用户代理)使用WebAuthn API来验证其用户。

要查看PyWARP的实际应用示例,请检查 examples 目录。包含两个演示:一个 AWS Chalice 应用和一个 Flask 应用。

除了阅读 WebAuthn 标准 外,我们建议实现者阅读 OWASP 认证备忘录NIST SP 800-63-3:数字认证指南,以了解认证最佳实践的概览。

安装

pip install pywarp

PyWARP依赖于cryptography,而cryptography又需要OpenSSL和CFFI。

摘要

from pywarp import RelyingPartyManager, Credential
from pywarp.backends import DynamoBackend  # This is an example. See "storage backends" below for other databases.

rp_id = "myapp.example.com"  # This must match the origin domain of your app, as seen by the browser.
rp = RelyingPartyManager("PyWARP demo", rp_id=rp_id, credential_storage_backend=DynamoBackend())

# Get options for navigator.credentials.create() - pass these to your frontend when registering a user
rp.get_registration_options(email=str)

# Run the protocol in https://www.w3.org/TR/webauthn/#registering-a-new-credential,
# then call the credential storage backend to store the credential public key.
rp.register(attestation_object=bytes, client_data_json=bytes, email=bytes)

# Get options for navigator.credentials.get() - pass these to your frontend when logging in a user
rp.get_authentication_options(email=str)

# Run the protocol in https://www.w3.org/TR/webauthn/#verifying-assertion,
# calling the credential storage backend to retrieve the credential public key.
# If no exception is raised, proceed with user login.
rp.verify(authenticator_data=bytes, client_data_json=bytes, signature=bytes, user_handle=bytes, raw_id=bytes,
          email=bytes)

请参阅examples/chalice/app.pyexamples/chalice/chalicelib/index.html(前端)以获取完整示例。

存储后端

您的应用程序可能正在使用uWSGI等应用服务器、MySQL、PostgreSQL或MongoDB等数据库后端,以及Flask或Django等框架来将它们连接起来。PyWARP对您的数据库、模式或模型没有任何假设。相反,它提供了一个抽象类(pywarp.backends.CredentialStorageBackend),代表存储和检索用户WebAuthn凭证数据的接口。

要部署PyWARP,声明一个CredentialStorageBackend的子类。在您的子类中实现与数据库的绑定,然后将您的子类实例传递给pywarp.RelyingPartyManager(credential_storage_backend=...)

class CredentialStorageBackend:
    def __init__(self):
        self.database_client = ...

    def get_credential_by_email(self, email):
        user_record = self.database_client.get(email)
        return Credential(credential_id=user_record["cred_id"],
                          credential_public_key=user_record["cred_pub_key"])

    def save_credential_for_user(self, email, credential):
        self.database_client.update(email, {"cred_id": credential.credential_id,
                                            "cred_pub_key": bytes(credential.public_key)})

    def save_challenge_for_user(self, email, challenge, type):
        self.database_client.update(email, {type + "challenge": challenge})

    def get_challenge_for_user(self, email, type):
        user_record = self.database_client.get(email)
        return user_record[type + "challenge"]

示例:Chalice应用

当使用常规AWS账户凭据(通过AWS CLI中的aws configure配置)时,Chalice应用示例(在examples/chalice目录中)可以作为AWS Lambda应用程序部署。此示例使用DynamoDB作为存储后端。

有关更多信息,请参阅API文档

作者

  • Andrey Kislyuk

错误

请在GitHub上报告错误、问题、功能请求等。

许可证

根据Apache License,版本2.0授权。

https://img.shields.io/travis/pyauth/pywarp.svg https://codecov.io/github/pyauth/pywarp/coverage.svg?branch=master https://img.shields.io/pypi/v/pywarp.svg https://img.shields.io/pypi/l/pywarp.svg https://readthedocs.org/projects/pywarp/badge/?version=latest

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面