更新项目配置,修改 Go 版本至 1.24,添加依赖项,重构主程序以加载配置和初始化服务,删除不再使用的远程目标配置文件。
This commit is contained in:
43
cmii-uav-watchdog/services/totp_service.go
Normal file
43
cmii-uav-watchdog/services/totp_service.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"cmii-uav-watchdog/config"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"cmii-uav-watchdog-otp/totp"
|
||||
)
|
||||
|
||||
// TOTPService TOTP服务
|
||||
type TOTPService struct {
|
||||
secret string
|
||||
}
|
||||
|
||||
// NewTOTPService 创建TOTP服务
|
||||
func NewTOTPService() *TOTPService {
|
||||
return &TOTPService{
|
||||
secret: config.GetConfig().Auth.Secret,
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateTOTP 生成TOTP验证码
|
||||
func (ts *TOTPService) GenerateTOTP() (string, error) {
|
||||
// 使用当前时间生成TOTP
|
||||
code, err := totp.GenerateCode(ts.secret, time.Now())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return code, nil
|
||||
}
|
||||
|
||||
// VerifyTOTP 验证TOTP验证码
|
||||
func (ts *TOTPService) VerifyTOTP(code string) error {
|
||||
// 验证TOTP
|
||||
valid := totp.Validate(code, ts.secret)
|
||||
if !valid {
|
||||
return errors.New("无效的TOTP验证码")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user