跳转到主要内容

HTTP消息签名的Requests认证模块

项目描述

requests-http-signature 是一个 Requests 认证插件requests.auth.AuthBase 子类),实现了 IETF HTTP消息签名草案标准

安装

$ pip install requests-http-signature

使用

import requests
from requests_http_signature import HTTPSignatureAuth, algorithms

preshared_key_id = 'squirrel'
preshared_secret = b'monorail_cat'
url = 'https://example.com/'

auth = HTTPSignatureAuth(key=preshared_secret,
                         key_id=preshared_key_id,
                         signature_algorithm=algorithms.HMAC_SHA256)
requests.get(url, auth=auth)

默认情况下,对于GET等无体的请求,只有Date头和@method@authority@target-uri派生组件标识被签名。如果不存在,将设置Date头。此外,如果存在,则对Authorization头进行签名,对于带有体的请求(如POST),将使用IETF摘要字段草案中描述的格式将请求体的SHA256设置为Content-Digest头,并进行签名。要添加其他头到签名中,传递一个包含头名称的数组到covered_component_ids关键字参数。请参阅API文档以获取完整选项和详细信息。

验证响应

可以使用类方法HTTPSignatureAuth.verify()来验证从服务器接收到的响应

class MyKeyResolver:
    def resolve_public_key(self, key_id):
        assert key_id == 'squirrel'
        return 'monorail_cat'

response = requests.get(url, auth=auth)
verify_result = HTTPSignatureAuth.verify(response,
                                         signature_algorithm=algorithms.HMAC_SHA256,
                                         key_resolver=MyKeyResolver())

更一般地,您可以使用Requests API重建任意请求,并将其传递给verify()

request = requests.Request(...)  # Reconstruct the incoming request using the Requests API
prepared_request = request.prepare()  # Generate a PreparedRequest
HTTPSignatureAuth.verify(prepared_request, ...)

要在HTTP服务器环境中验证传入请求并签名响应,请参阅flask-http-signaturehttp-message-signatures包。

请参阅API文档以获取详细信息。

非对称密钥算法

要使用非对称密钥算法签名或验证消息,请将signature_algorithm关键字参数设置为algorithms.ED25519algorithms.ECDSA_P256_SHA256algorithms.RSA_V1_5_SHA256algorithms.RSA_PSS_SHA512。请注意,由于cryptography库的限制,目前不支持使用rsa-pss-sha512进行签名。

对于非对称密钥算法,您可以将私钥作为PEM格式的字节数据作为key参数传递给HTTPSignatureAuth()构造函数,或如下配置密钥解析器

with open('key.pem', 'rb') as fh:
    auth = HTTPSignatureAuth(algorithm=algorithms.RSA_V1_5_SHA256,
                             key=fh.read(),
                             key_id=preshared_key_id)
requests.get(url, auth=auth)

class MyKeyResolver:
    def resolve_public_key(self, key_id: str):
        return public_key_pem_bytes[key_id]

    def resolve_private_key(self, key_id: str):
        return private_key_pem_bytes[key_id]

auth = HTTPSignatureAuth(algorithm=algorithms.RSA_V1_5_SHA256,
                         key=fh.read(),
                         key_resolver=MyKeyResolver())
requests.get(url, auth=auth)

摘要算法

要使用SHA-512代替默认的SHA-256生成内容摘要(Content-Digest)头,如下子类化HTTPSignatureAuth

class MySigner(HTTPSignatureAuth):
    signing_content_digest_algorithm = "sha-512"

许可

根据Apache许可证第2版许可。

项目详情


下载文件

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

源分发

requests-http-signature-0.7.1.tar.gz (19.0 kB 查看哈希)

上传时间

构建分发

requests_http_signature-0.7.1-py3-none-any.whl (12.5 kB 查看哈希值)

上传时间 Python 3

由以下支持

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误记录StatusPageStatusPage状态页面