mirror of
https://github.com/wonli/aqi.git
synced 2025-09-26 20:51:23 +08:00
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package jwt
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func GenerateToken(uid, authCode, channel string) (string, error) {
|
|
|
|
if viper.GetString("jwtSecurity") == "" {
|
|
return "", errors.New("jwt security not configured")
|
|
}
|
|
|
|
jwtLifetime := viper.GetDuration("jwtLifetime")
|
|
if jwtLifetime == 0 {
|
|
jwtLifetime = time.Hour * 48
|
|
}
|
|
|
|
expire := time.Now().Add(jwtLifetime)
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, &LoginClaims{
|
|
UID: uid,
|
|
Channel: channel,
|
|
AuthCode: authCode,
|
|
StandardClaims: jwt.StandardClaims{
|
|
ExpiresAt: expire.Unix(),
|
|
},
|
|
})
|
|
|
|
return token.SignedString([]byte(viper.GetString("jwtSecurity")))
|
|
}
|
|
|
|
func ValidToken(t string) (*LoginClaims, error) {
|
|
token, err := jwt.ParseWithClaims(t, &LoginClaims{}, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(viper.GetString("jwtSecurity")), nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if claims, ok := token.Claims.(*LoginClaims); ok && token.Valid {
|
|
return claims, nil
|
|
} else {
|
|
return nil, errors.New("failed to validate token")
|
|
}
|
|
}
|