Skip to main content

简单、易用且支持防重放的签名工具

Project description

steel-seal

简单、易用且支持防重放的签名工具。

安装模块

在你的项目下执行下面命令进行安装:

pip install steel-seal

快速开始

生成一个新的令牌

为了便于使用,steel_seal 实现了 generate_token 函数用于生成一个新的令牌。

from steel_seal import SteelSeal

token = SteelSeal.generateToken()

print(token) # 输出: 2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je

对请求进行签名和验签

steel_seal主要用于对 Http/Https请求进行签名和验签。为保证当前请求不被篡改,建议对请求的 Query 参数以及 Body 参数进行拼接后并使用 signatureverify 方法对数据进行签名和验签。

1、对Get请求进行签名和验签

示例:

fetch("https://127.0.0.1:8080/api/message/list?self_only=0", {
  "headers": {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
  },
  "method": "GET",
  "mode": "cors",
  "credentials": "include"
});

签名(伪代码):

# 使用令牌初始化实例
import json
import requests
from steel_seal import SteelSeal

token = "2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je"
steel_ins = SteelSeal(token)

# ......

# 把查询参数组装成Dict并序列化成JSON字符串
query_str = dict(self_only=0)
sig_data = json.dumps(query_str) # 输出:'{"self_only":0}

# 使用 signature 方法对数据进行签名
# 输出:
#       {
#         timestamp: 1655956125,
#         nonce: '5yqXByu9',
#         signature: '6465a0b2ff69712c00a3430b26f4e1089c3ff851'
#       }
sig_info = steel_ins.signature(sig_data)

# 把签名对象内的timestamp、nonce、signature拼接到query参数内并向目标服务发起请求
# 输出:https://127.0.0.1:8080/api/message/list?self_only=0&timestamp=1655956125&nonce=5yqXByu9&signature=6465a0b2ff69712c00a3430b26f4e1089c3ff851
request_url = f'https://127.0.0.1:8080/api/message/list?self_only=0&timestamp={sig_info.timestamp}&nonce={sig_info.nonce}&signature={sig_info.signature}'
result = requests.get(request_url)

# TODO: 实现相关的业务逻辑
print(result)

验签(伪代码):

# 使用令牌初始化实例
import json
from steel_seal import SteelSeal

token = "2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je"
steel_ins = SteelSeal(token)

# ......

# 从请求的query中解析timestamp、nonce、signature参数组装成签名对象,比如:
sig_info = dict(
    timestamp=1655956125,
    nonce='5yqXByu9',
    signature='6465a0b2ff69712c00a3430b26f4e1089c3ff851'
)

# 从请求的query中解析其它参数组装成Object并序列化成JSON字符串
query_str = dict(self_only=0)
sig_data = json.dumps(query_str) # 输出:{"self_only":0}

# 使用 verify 方法对数据进行验签
is_valid = steel_ins.verify(sig_data, sig_info)

# 如果签名无效,则拒绝当前请求
if not is_valid:
    # TODO:拒绝请求
    return

# TODO: 实现具体的业务逻辑

2、对Post请求进行签名和验签

示例:

fetch("https://127.0.0.1:8080/api/message/modify", {
  "headers": {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
  }, 
    "data": '{"id":1,"content":"hello, world"}',
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});

签名(伪代码):

# 使用令牌初始化实例
import json
import requests
from steel_seal import SteelSeal

token = "2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je"
steel_ins = SteelSeal(token)

# ......

# 把body参数组装成Dict并序列化成JSON字符串
body_str = dict(id=1, content="hello, world")
sig_data = json.dumps(body_str) # 输出:{"id":1,"content":"hello, world"}

# 使用 signature 方法对数据进行签名
# 输出:
#       {
#         timestamp: 1655956125,
#         nonce: '5yqXByu9',
#         signature: '6465a0b2ff69712c00a3430b26f4e1089c3ff851'
#       }
sig_info = steel_ins.signature(sig_data)

# 把签名对象内的timestamp、nonce、signature拼接到query参数内并向目标服务发起请求
# 输出:https://127.0.0.1:8080/api/message/modify?timestamp=1655956125&nonce=5yqXByu9&signature=6465a0b2ff69712c00a3430b26f4e1089c3ff851
request_url = f'https://127.0.0.1:8080/api/message/modify?timestamp={sig_info.timestamp}&nonce={sig_info.nonce}&signature={sig_info.signature}'
result = requests.post(request_url, data=body_str, headers={"Content-Type": "application/json;"})

# TODO: 实现相关的业务逻辑
print(result)

验签(伪代码):

# 使用令牌初始化实例
import json
from steel_seal import SteelSeal

token = "2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je"
steel_ins = SteelSeal(token)

# ......

# 从请求的query中解析timestamp、nonce、signature参数组装成签名对象,比如:
sig_info = dict(
    timestamp=1655956125,
    nonce='5yqXByu9',
    signature='6465a0b2ff69712c00a3430b26f4e1089c3ff851'
)

# 从请求中读取原始的raw_body并使用verify方法进行验签
raw_body = "..."
is_valid = steel_ins.verify(raw_body, sig_info)

# 如果签名无效,则拒绝当前请求
if not is_valid:
    # TODO:拒绝请求相关逻辑
    return

# TODO: 实现具体的业务逻辑

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

steel_seal-0.0.4-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file steel_seal-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: steel_seal-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for steel_seal-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 856848392ebf1886c634f36ff11e2ccb5fb2e356d66e2108af64d62afe3e1b7f
MD5 e3fcfa150377489a667b3d21ab6e1580
BLAKE2b-256 76212af6732c0c4a47334ec8ff2b4f9a5f6b12100cf6816a48272ff892a475c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page