JWT公证服务支持
ns-jwt的Python项目详细描述
ns_jwt
:公证服务的json web令牌
我们将使用rs256(公钥/私钥)jwt签名的变体。(来源:https://pyjwt.readthedocs.io/en/latest/usage.html;encoding-decoding-tokens-with-rs256-rsa" rel="nofollow">https://pyjwt.readthedocs.io/en/latest/usage.html;encoding-decoding-tokens-with-rs256-rsa)。对于签名,假定ns拥有一个公私密钥对。presidio可以通过静态配置访问公钥,也可以通过查询令牌中指定的ns上的端点来访问公钥。
NS令牌带有以下声明:
<表><广告>对于日期,表示从1970-01-01t00:00:00z utc
到指定utc日期/时间的秒数的json数值,忽略闰秒。这相当于ieee std 1003.1,2013版定义"自纪元以来的秒数",其中,除可以表示非整数值外,每天精确地占86400秒。有关日期/时间的详细信息,请参见RFC 3339,尤其是UTC。
设置和配置
除了依赖项(pyjwt、密码学、python dateutil),没有外部配置。
如上所述,使用虚拟环境
virtualenv -p $(which python3) venv
source venv/bin/activate
pip install --editable ns_jwt
pip install pytest
测试
只需执行下面的命令。测试依赖于在tests/
目录中存在public.pem
和private.pem
(rsa密钥的公共和私有部分)。您可以使用tests/gen keypair.sh
(依赖于openssl安装)生成新对。
pytest -v ns_jwt
拆卸和清理
不需要。
故障排除
由于binasci.error:incorrect padding
和jwt.exceptions.decodeerror:invalid crypto padding
的原因,CI登录或其他JWT可能无法使用PYJWT直接解码。这是由于令牌末尾缺少base64填充。将其作为字符串读入,然后在解码之前添加填充:
import jwt
with open('token_file.jwt') as f:
token_string = f.read()
jwt.decode(token_string + "==", verify=False)
可以添加任意数量的=
来修复填充(至少2个)。如果令牌是作为字节字符串读入的,则首先转换为utf-8
:jwt_str=str(jwt_bin,'utf-8')
,然后添加填充(source:https://gist.github.com/perrygeo/ee7c65bb1541f6ac770)