mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2026-11-03 03:31:58 +08:00
37 lines
806 B
Go
37 lines
806 B
Go
package utils
|
|
|
|
import (
|
|
"SamWaf/global"
|
|
"fmt"
|
|
"github.com/pquerna/otp"
|
|
"github.com/pquerna/otp/totp"
|
|
)
|
|
|
|
func GenOtpSecret(userName string, issuer string) (string, string, error) {
|
|
// 如果没有提供 issuer,使用默认值
|
|
if issuer == "" {
|
|
issuer = "SamWaf-" + global.GWAF_CUSTOM_SERVER_NAME
|
|
}
|
|
key, err := totp.Generate(totp.GenerateOpts{
|
|
Issuer: issuer,
|
|
AccountName: userName,
|
|
Period: 30,
|
|
Digits: otp.DigitsSix,
|
|
Algorithm: otp.AlgorithmSHA1,
|
|
})
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return key.Secret(), key.URL(), nil
|
|
}
|
|
func ValidateOtpCode(passCode string, secret string) bool {
|
|
// 验证一次性密码
|
|
valid := totp.Validate(passCode, secret)
|
|
if valid {
|
|
fmt.Println("Valid passcode!")
|
|
} else {
|
|
fmt.Println("Invalid passcode!")
|
|
}
|
|
return valid
|
|
}
|