一个非常简单的jwt库
simplejwt的Python项目详细描述
简单重量
一个非常简单的jwt库。
支持的算法
- HS256
- HS384
- HS512
用法
编码
返回新令牌。
from simplejwt import encode
token = encode('secret', {'my_payload': 'some_data'}, 'HS256')
# eyJ0eXBlIjogIkpXVCIsICJhbGciOiAiSFMyNTYifQ.eyJteV9wYXlsb2FkIjogInNvbWVfZGF0YSJ9.BXAs5tYkARpGHhegb8g8bfj8KhjFUTTjdEf81Ma1VhY
Name | Type | Default | Description |
---|---|---|---|
^{ | ^{ | N/A | The secret used to create the token. |
^{ | ^{ | N/A | The payload data contained within the token. |
^{ | ^{ | ^{ | The algorithm to use to create the token. |
制造
返回新令牌。此函数具有在rfc7519中指定的已注册声明的参数。
有效负载中提供的任何已注册声明将优先于作为参数提供的任何声明。
from simplejwt import make
token = make('secret', {'my_payload': 'some_data'}, 'HS256', issuer='acme', valid_to=1234567)
# eyJ0eXBlIjogIkpXVCIsICJhbGciOiAiSFMyNTYifQ.eyJteV9wYXlsb2FkIjogInNvbWVfZGF0YSIsICJpc3MiOiAiYWNtZSIsICJleHAiOiAxMjM0NTY3fQ.Nr5IADzsOhlzjxnghquBrRwewg10srDHu__-HN7GGGA
Name | Type | Default | Description |
---|---|---|---|
^{ | ^{ | N/A | The secret used to create the token. |
^{ | ^{ | N/A | The payload data contained within the token. |
^{ | ^{ | ^{ | The algorithm to use to create the token. |
^{ | ^{ | ^{ | The issuer of the token. |
^{ | ^{ | ^{ | The subject of the token. |
^{ | ^{ | ^{ | The audience of the token. |
^{ | ^{ | ^{ | The expiry date of the token as a timestamp. |
^{ | ^{ | ^{ | The date the token is valid from as a timestamp. |
^{ | ^{ | ^{ | The date the token was issued as a timestamp. |
^{ | ^{ | ^{ | The id of the token. |
解码
从令牌返回有效负载。
from simplejwt import encode, decode
token = encode('secret', {'my_payload': 'some_data'}, 'HS256')
payload = decode('secret', token, 'HS256')
# {'my_payload': 'some_data'}
Name | Type | Default | Description |
---|---|---|---|
^{ | ^{ | N/A | The secret used to decode the token. |
^{ | ^{ | N/A | The token to decode. |
^{ | ^{ | ^{ | The algorithm to use to create the token. |
运行测试
使用测试依赖项安装软件包
pip install -e ".[test]"
运行tox
tox