跳转到主要内容

使用HTTP签名草案规范进行安全HTTP请求签名

项目描述

httpsig_cffi
============

.. image:: https://travis-ci.org/hawkowl/httpsig_cffi.svg?branch=master
:target: https://travis-ci.org/hawkowl/httpsig_cffi

根据IETF HTTP签名规范(`草案3`_)安全地签名HTTP请求。
这是原始模块_的分支,该分支是为了完全支持RSA和HMAC方案以及单元测试这两种方案以证明它们是有效的。
这个特定的分支从PyCrypto迁移到Cryptography,后者提供了PyPy支持。

有关签名方案的更多详细信息,请参阅原始项目_、原始Python模块_、原始规范_和`当前IETF草案`_。

.. _project: https://github.com/joyent/node-http-signature
.. _module: https://github.com/zzsnzmn/py-http-signature
.. _fork: https://github.com/ahknight/httpsig
.. _spec: https://github.com/joyent/node-http-signature/blob/master/http_signing.md
.. _`current IETF draft`: https://datatracker.ietf.org/doc/draft-cavage-http-signatures/
.. _`Draft 3`: http://tools.ietf.org/html/draft-cavage-http-signatures-03
.. _`a fork`: https://github.com/hawkowl/httpsig_cffi


需求
------------

* Python 2.7, 3.2, 3.3, 3.4, PyPy, PyPy3
* Cryptography_

可选

* requests_

.. _Cryptography: https://pypi.python.org/pypi/cryptography
.. _requests: https://pypi.python.org/pypi/requests


用法
-----

实际文档将随后提供,但目前这应该能帮助您开始。

对于简单的原始签名

.. code:: python

import httpsig_cffi as httpsig

secret = open('rsa_private.pem', 'rb').read()

sig_maker = httpsig.Signer(secret=secret, algorithm='rsa-sha256')
sig_maker.sign('hello world!')

对于与Web框架的一般使用

.. code:: python

import httpsig_cffi as httpsig

key_id = "Some Key ID"
secret = b'some big secret'

hs = httpsig.HeaderSigner(key_id, secret, algorithm="hmac-sha256", headers=['(request-target)', 'host', 'date'])
signed_headers_dict = hs.sign({"Date": "Tue, 01 Jan 2014 01:01:01 GMT", "Host": "example.com"}, method="GET", path="/api/1/object/1")

与requests一起使用

.. code:: python

import json
import requests
from httpsig_cffi.requests_auth import HTTPSignatureAuth

secret = open('rsa_private.pem', 'rb').read()

auth = HTTPSignatureAuth(key_id='Test', secret=secret)
z = requests.get('https://api.example.com/path/to/endpoint',
auth=auth, headers={'X-Api-Version': '~6.5'})

类初始化参数
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

注意,密钥和密钥应该为字节对象。将尝试将它们转换为字节,但如果失败,则将抛出异常。

.. code:: python

httpsig_cffi.Signer(secret, algorithm='rsa-sha256')

``secret``,在RSA签名的情况下,是包含私用RSA pem的字符串。在HMAC的情况下,是秘密密码。
``algorithm``是六种允许的签名中的一种:``rsa-sha1``、``rsa-sha256``、``rsa-sha512``、``hmac-sha1``、``hmac-sha256``、
``hmac-sha512``。


.. code:: python

httpsig_cffi.requests_auth.HTTPSignatureAuth(key_id, secret, algorithm='rsa-sha256', headers=None)

``key_id``是服务器系统通过它知道您的RSA签名或密码的标签。
``headers``是要连接并用作签名对象的HTTP头列表。默认情况下,它是规范的最小值,即``Date`` HTTP头。
``secret``和``algorithm``与上面相同。


测试
-----

要运行测试:

tox


许可证
-------

本模块和原始模块_均受MIT许可证许可。


httpsig_cffi
============

15.0.0 (2015年1月16日)
--------------------

* 从PyCrypto迁移到Cryptography。
* 迁移到py.test。


httpsig (先前)
==================

1.1.0 (2014年7月24日)
-------------------

* 将"(request-line)"更改为"(request-target)",以符合草案3。

1.0.3 (2014年7月9日)
-------------------

* 在一个设置下统一默认签名算法。设置httpsig.sign.DEFAULT_SIGN_ALGORITHM将更改所有未来实例的算法。
* 更好地处理无效参数。

1.0.2 (2014年7月2日)
-------------------

* 确保我们将头视为ASCII字符串。
* 处理授权头中的情况,其中方法名称后有垃圾(非键对)。

1.0.1 (2014年7月2日)
-------------------

* 支持Python 3(2.7 + 3.2-3.4)。
* 更新tox和Travis CI配置以测试支持的Python版本。
* 更新README。

1.0.0 (2014年7月1日)
-------------------
* 根据 http://tools.ietf.org/html/draft-cavage-http-signatures-02 编写。
* 添加"setup.py test"和tox支持。
* 为所有当前支持算法添加了签名/验证单元测试。
* HeaderSigner和HeaderVerifier现在共享相同的信息构建逻辑。
* 消息中的HTTP方法现在是正确的小写。
* 解决单元测试失败。
* 更新验证器和HeaderVerifier以处理验证RSA和HMAC签名。
* 更新versioneer。
* 更新联系/作者信息。
* 从测试目录中删除了多余的密钥对。
* 删除了SSH代理支持。
* 删除了对从磁盘读取密钥文件的支持,因为这如果用于像drf-httpsig这样的服务器框架,将是一个巨大的安全漏洞。

1.0b1 (2014年6月23日)
-------------------
* 根据规范从请求行中删除HTTP版本(破坏了向后兼容性)。
* 删除了自动生成缺少的Date头(确保客户端兼容性)。


http-signature (先前)
=========================

0.2.0 (未发布)
~~~~~~~~~~~~~~~~~~

* 更新到较新的规范(与先前版本不兼容)。
* 处理`request-line`元头。
* 允许秘密是PEM编码的字符串。
* 添加规范中的测试用例。

0.1.4 (2012-10-03)
~~~~~~~~~~~~~~~~~~

考虑到ssh现在正在合并到paramiko中:任一包均可接受(但paramiko的理想版本应大于等于1.8.0)

0.1.3 (2012-10-02)
~~~~~~~~~~~~~~~~~~

停止默认启用`allow_agent`
停止默认要求`ssh`包 -- 它仅在`allow_agent=True`时导入
修改了ssh-agent的逻辑:如果有一个密钥可用,则无需使用其他认证方法
修改了密钥文件使用的逻辑:如果解密失败,则提示输入密码
错误修复:如果ssh-agent找不到正确的密钥,则会出现无意义的错误(感谢petervolpe)
引入了versioneer.py

项目详情


下载文件

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

源分发

httpsig_cffi-15.0.0.tar.gz (22.3 kB 查看哈希值)

上传时间

构建分发

httpsig_cffi-15.0.0-py2.py3-none-any.whl (19.3 kB 查看哈希值)

上传时间 Python 2 Python 3

支持