Skip to content

LimeOtpauth 多因素认证插件

一个实用的多因素认证插件,目前支持基于时间的一次性密码(TOTP),符合 RFC 6238 标准。支持多种哈希算法、自定义位数和时间周期等配置,可用于双因素认证、安全登录等多种场景。组件提供了多种自定义选项,可以满足常见的认证需求。

功能特点

  • TOTP支持:基于时间的一次性密码生成,符合 RFC 6238 标准
  • 多种哈希算法:支持 SHA1、SHA256、SHA512 算法
  • 自定义配置:可自定义验证码位数、有效期等参数
  • URI生成:支持生成标准的 otpauth:// 协议 URI,可用于生成二维码

未来计划

  • HOTP支持:添加基于计数器的一次性密码支持,符合 RFC 4226 标准
  • Steam令牌:添加 Steam 令牌认证支持
  • 验证功能:增加验证功能,用于验证用户输入的一次性密码是否正确

文档链接

📚 组件详细文档请访问以下站点:

安装方法

  1. 在uni-app插件市场入口 中搜索并导入lime-otpauth
  2. 在页面中使用useTOTP函数
  3. 安卓需要自定义基座后使用

代码演示

基础用法

最简单的一次性密码生成用法,使用默认配置生成6位数字验证码。

html
<script setup lang="ts">
import { useTOTP } from '@/uni_modules/lime-otpauth'

const totp = useTOTP()
const code: string = totp.generate()
console.log('当前验证码:', code)
</script>

自定义配置

设置自定义密钥、算法和位数。

html
<script setup lang="ts">
import { useTOTP, type UseTOTPOptions } from '@/uni_modules/lime-otpauth'

const options: UseTOTPOptions = {
  secret: 'JBSWY3DPEHPK3PXP', // 自定义密钥
  algorithm: 'SHA256', // 使用SHA256算法
  digits: 8,                  // 生成8位验证码
  period: 30                  // 30秒有效期
}

const totp = useTOTP(options)
const code: string = totp.generate()
console.log('自定义验证码:', code)
</script>

指定时间戳生成验证码

为特定时间点生成验证码,用于测试或验证。

html
<script setup lang="ts">
import { useTOTP, type LimeTOTPGenerateOptions } from '@/uni_modules/lime-otpauth'

const totp = useTOTP()

// 使用当前时间生成验证码
const currentCode: string = totp.generate()

// 使用指定时间戳生成验证码
const options: LimeTOTPGenerateOptions = {
  secret: 'JBSWY3DPEHPK3PXP',
  timestamp: 1622234248000 // 指定时间戳(毫秒)
}
const specificCode: string = totp.generate(options)

console.log('当前验证码:', currentCode)
console.log('指定时间验证码:', specificCode)
</script>

完整配置示例

使用所有可用选项配置TOTP生成器。

html
<script setup lang="ts">
import { useTOTP, type UseTOTPOptions } from '@/uni_modules/lime-otpauth'

const options: UseTOTPOptions = {
  issuer: '我的应用',           // 发行者/提供商名称
  label: 'user@example.com',   // 账户标签(通常为用户邮箱)
  issuerInLabel: true,         // 在标签中包含发行者
  secret: 'JBSWY3DPEHPK3PXP',  // 密钥
  algorithm: 'SHA512', // 使用SHA512算法(更高安全性)
  digits: 8,                   // 8位验证码
  period: 60                   // 60秒有效期
}

const totp = useTOTP(options)
const code: string = totp.generate()
console.log('完整配置验证码:', code)
</script>

生成 otpauth:// URI

生成标准的 otpauth:// 协议 URI,可用于生成二维码供 Google Authenticator 等应用扫描导入。

html
<script setup lang="ts">
import { useTOTP, UseTOTPOptions } from '@/uni_modules/lime-otpauth'

const options: UseTOTPOptions = {
  issuer: '我的应用',
  label: 'user@example.com',
  secret: 'JBSWY3DPEHPK3PXP'
}

const totp = useTOTP(options)

// 生成 otpauth:// URI
const uri: string = totp.toString()
console.log('TOTP URI:', uri)
// 输出: otpauth://totp/我的应用:user@example.com?secret=JBSWY3DPEHPK3PXP&issuer=我的应用&algorithm=SHA1&digits=6&period=30

// 此 URI 可用于生成二维码,供 Google Authenticator 等 TOTP 应用扫描导入
</script>

API文档

类型导入

typescript
import { 
  useTOTP,
  LimeTOTP,
  UseTOTPOptions,
  LimeTOTPGenerateOptions,
  LimeHMACAlgorithm
} from '@/uni_modules/lime-otpauth'

useTOTP 函数参数

参数名说明类型默认值
issuer账户发行者/提供商名称string""
label账户标签/标识(通常为用户邮箱或用户名)string"OTPAuth"
issuerInLabel是否在标签中包含发行者前缀booleantrue
secret密钥(base32)string随机生成
algorithmHMAC哈希算法(支持:SHA1/SHA256/SHA512)LimeHMACAlgorithm"SHA1"
digits验证码位数(通常为6或8位数字)number6
period验证码有效期(单位:秒)number30

LimeTOTP 对象方法

方法名说明参数返回值
generate生成一次性密码无参数或 LimeTOTPGenerateOptionsstring
toString将TOTP配置转换为标准的otpauth://协议URI字符串string

LimeTOTPGenerateOptions 参数

参数名说明类型默认值
secret密钥(base32)string-
algorithmHMAC哈希算法LimeHMACAlgorithm"SHA1"
digits验证码位数number6
period时间步长(单位:秒)number30
timestamp时间戳(毫秒)numberDate.now()

支持的算法

组件支持以下HMAC哈希算法:

算法名称安全级别说明
SHA1基本RFC 6238默认算法,160位
SHA256推荐256位,提供更好的安全性
SHA512最高512位,提供最高级别安全性

支持与赞赏

如果你觉得本插件解决了你的问题,可以考虑支持作者:

支付宝赞助微信赞助

源代码

组件源码