43 lines
943 B
Go
43 lines
943 B
Go
package services
|
|
|
|
import (
|
|
"cmii-uav-watchdog-common/totp_tier_one"
|
|
"cmii-uav-watchdog/config"
|
|
)
|
|
|
|
// TOTPService TOTP服务
|
|
type TOTPService struct {
|
|
tierOneSecret string
|
|
}
|
|
|
|
// NewTOTPService 创建TOTP服务
|
|
func NewTOTPService() *TOTPService {
|
|
secret := config.GetConfig().TierOneAuth.TierOneSecret
|
|
if secret == "" {
|
|
panic("TierOne TOTP tierOneSecret is not set ! can not start the service!")
|
|
}
|
|
|
|
return &TOTPService{
|
|
tierOneSecret: secret,
|
|
}
|
|
}
|
|
|
|
// GenerateTierOneTOTP 生成一级TOTP验证码
|
|
func (ts *TOTPService) GenerateTierOneTOTP() (string, error) {
|
|
// 使用当前时间生成TOTP
|
|
code, err := totp_tier_one.GenerateTierOneTOTPCode(ts.tierOneSecret)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return code, nil
|
|
}
|
|
|
|
// VerifyTierOneTOTP 验证一级TOTP验证码
|
|
func (ts *TOTPService) VerifyTierOneTOTP(code string) bool {
|
|
// 验证TOTP
|
|
valid := totp_tier_one.VerifyTierOneTOTPCode(code, ts.tierOneSecret)
|
|
|
|
return valid
|
|
}
|