Warning
这是一个早期版本,可能会有较大的 API 变动。
为 Koishi 提供了一次性密码服务。
OTP 服务支持以下算法:
更多算法欢迎提交 PR。
插件市场搜索 otp 并安装。
otp [name]: 列出用户保存的某个或所有密码。otp.add <name> <token>: 添加认证账号。otp.rm <name>: 移除令牌。otp.up <name> <token>: 更新(覆盖)令牌。
Note
最大步长在 TOTP 算法中表示每隔多少秒更新一次密码,HOTP 算法中表示每隔多少次更新一次密码。
- qrcode (
boolean): 是否在添加令牌时显示二维码,默认值:true。 - tokenizer (
random、uuid、timestamp): 公共令牌生成方式,默认值:uuid。 - maxStep (
number): 最大步长,默认值:30。 - maxThreshold (
number): 最大重试阈值,默认值:5。
npm i koishi-plugin-otp -D
# or yarn
yarn add koishi-plugin-otp -D可通过 ctx.otp 访问。
method: 认证算法,支持totp和hotp。options: 认证配置项,支持以下属性:secret: 共享密钥。algorithm: 哈希算法,支持sha1、sha256和sha512。digits: 密码长度。period: 密码更新周期。step: 最大步长。
生成密码。
tokenizer: 密钥生成器,支持random、uuid和timestamp。salt: 密钥生成器的盐。
创建共享密钥。
插件计划支持在 WebUI 中使用,但需要自行导入 HMAC 算法库(为确保兼容性,请确认算法库支持 SHA1、SHA256、SHA512 算法)。下面是计划中的使用方式:
import { hmac } from 'your hmac library'
import { useOTP } from 'koishi-plugin-otp/dist'
const otp = useOTP('totp', {
secret: 'your secret',
algorithm: 'sha1',
digits: 6,
period: 30,
step: 1
}, (secret, algorithm) => hmac(secret, algorithm));This project is licensed under the MIT license.