2025-03-27 16:27:41 +08:00

35 lines
798 B
Go

package config
import (
"fmt"
)
// AuthConfig 认证配置
type AuthConfig struct {
SecretKey string `mapstructure:"secretKey"` // 用于JWT签名的密钥
AdminToken string `mapstructure:"adminToken"` // 管理员登录口令
TokenExpiry int `mapstructure:"tokenExpiry"` // Token过期时间(小时)
}
// Validate 验证认证配置
func (c *AuthConfig) Validate() error {
if c.SecretKey == "" {
return fmt.Errorf("auth secret key cannot be empty")
}
if c.AdminToken == "" {
return fmt.Errorf("admin token cannot be empty")
}
if c.TokenExpiry <= 0 {
c.TokenExpiry = 24 // 默认24小时
}
return nil
}
// IsValidToken 检查提供的token是否与配置的管理员token匹配
func (c *AuthConfig) IsValidToken(token string) bool {
return token == c.AdminToken
}