1.0.3 • Published 2 years ago
steel-seal v1.0.3
steel-seal
简单、易用且支持防重放的签名工具。
安装模块
在你的项目下执行下面命令进行安装:
npm i steel-seal
快速开始
生成一个新的令牌
为了便于使用,steel_seal
实现了 generateToken
函数用于生成一个新的令牌。
import { SteelSeal } from "steel_seal"
const token = SteelSeal.generateToken()
console.log(token) // 输出: 2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je
对请求进行签名和验签
steel_seal
主要用于对 Http/Https
请求进行签名和验签。为保证当前请求不被篡改,建议对请求的 Query
参数以及 Body
参数进行拼接后并使用 signature
或 verify
方法对数据进行签名和验签。
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 { SteelSeal } from "steel_seal"
const token = "2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je"
const steelSeal = new SteelSeal({ token })
......
// 把查询参数组装成Object并序列化成JSON字符串
let queryStr = { self_only: 0 }
const signateData = JSON.stringify(queryStr) // 输出:'{"self_only":0}
// 使用 signature 方法对数据进行签名
// 输出:
// {
// timestamp: 1655956125,
// nonce: '5yqXByu9',
// signature: '6465a0b2ff69712c00a3430b26f4e1089c3ff851'
// }
const sigInfo = steelSeal.signature(signateData)
// 把签名对象内的timestamp、nonce、signature拼接到query参数内并向目标服务发起请求
// 输出:https://127.0.0.1:8080/api/message/list?self_only=0×tamp=1655956125&nonce=5yqXByu9&signature=6465a0b2ff69712c00a3430b26f4e1089c3ff851
const requestUrl = `https://127.0.0.1:8080/api/message/list?self_only=0×tamp=${sigInfo.timestamp}&nonce=${sigInfo.nonce}&signature=${sigInfo.signature}`
const result = await fetch(requestUrl)
// TODO: 实现相关的业务逻辑
console.log(result)
验签(伪代码):
// 使用令牌初始化实例
import { SteelSeal } from "steel_seal"
const token = "2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je"
const steelSeal = new SteelSeal({ token })
......
// 从请求的query中解析timestamp、nonce、signature参数组装成签名对象,比如:
const sigInfo = {
timestamp: 1655956125,
nonce: '5yqXByu9',
signature: '6465a0b2ff69712c00a3430b26f4e1089c3ff851'
}
// 从请求的query中解析其它参数组装成Object并序列化成JSON字符串
let query = { self_only: 0 }
const signateData = JSON.stringify(query) // 输出:'{"self_only":0}
// 使用 verify 方法对数据进行验签
const isValid = steelSeal.verify(signateData, sigInfo)
// 如果签名无效,则拒绝当前请求
if(!isValid) {
// TODO: 拒绝请求
return
}
// TODO: 实现具体的业务逻辑
console.log(result)
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 { SteelSeal } from "steel_seal"
const token = "2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je"
const steelSeal = new SteelSeal({ token })
......
// 把body参数组装成Dict并序列化成JSON字符串
let bodyStr = { id: 1, content: "hello, world" }
const signData = JSON.stringify(bodyStr) // 输出:'{"self_only":0}
// 使用 signature 方法对数据进行签名
// 输出:
// {
// timestamp: 1655956125,
// nonce: '5yqXByu9',
// signature: '6465a0b2ff69712c00a3430b26f4e1089c3ff851'
// }
const sigInfo = steelSeal.signature(signData)
// 把签名对象内的timestamp、nonce、signature拼接到query参数内并向目标服务发起请求
// 输出:https://127.0.0.1:8080/api/message/modify?timestamp=1655956125&nonce=5yqXByu9&signature=6465a0b2ff69712c00a3430b26f4e1089c3ff851
const requestUrl = `https://127.0.0.1:8080/api/message/modify?timestamp=${sigInfo.timestamp}&nonce=${sigInfo.nonce}&signature=${sigInfo.signature}`
const result = await fetch(requestUrl, {method: "POST", body=bodyStr})
// TODO: 实现相关的业务逻辑
console.log(result)
验签(伪代码):
// 使用令牌初始化实例
import { SteelSeal } from "steel_seal"
const token = "2sqlFObdoqqYRpUFAGiGQecCwJ3Qw9je"
const steelSeal = new SteelSeal({ token })
......
// 从请求的query中解析timestamp、nonce、signature参数组装成签名对象,比如:
const sigInfo = {
timestamp: 1655956125,
nonce: '5yqXByu9',
signature: '6465a0b2ff69712c00a3430b26f4e1089c3ff851'
}
// 从请求中读取原始的raw_body并使用verify方法进行验签
rawBody = "..."
const isValid = steelSeal.verify(rawBody, sigInfo)
// 如果签名无效,则拒绝当前请求
if(!isValid) {
// TODO: 拒绝请求
return
}
// TODO: 实现具体的业务逻辑
console.log(result)