添加配置加载和授权服务初始化,更新授权模型引用,重构授权文件生成和处理逻辑,优化 TOTP 密钥生成及错误处理

This commit is contained in:
zeaslity
2025-03-13 10:36:23 +08:00
parent 25dbc87a73
commit 34147b2f69
5 changed files with 133 additions and 55 deletions

View File

@@ -3,8 +3,10 @@ package services
import (
"cmii-uav-watchdog/config"
"errors"
"log"
"time"
otp "cmii-uav-watchdog-otp"
"cmii-uav-watchdog-otp/totp"
)
@@ -41,3 +43,23 @@ func (ts *TOTPService) VerifyTOTP(code string) error {
return nil
}
// GenerateTOTPSecret 生成TOTP密钥
func (ts *TOTPService) GenerateTOTPSecret() (string, error) {
secret, err := totp.Generate(totp.GenerateOpts{
SecretSize: 32,
Issuer: "cmii-uav-watchdog",
AccountName: "cmii-uav-watchdog",
Period: 30,
Secret: []byte{},
Digits: otp.DigitsSix,
Algorithm: otp.AlgorithmSHA1,
Rand: nil,
})
if err != nil {
log.Printf("生成TOTP密钥失败: %v", err)
return "", err
}
return secret.Secret(), nil
}