迁移
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package adminauth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
type Claims struct {
|
||||
UUID string
|
||||
ID uint
|
||||
Username string
|
||||
NickName string
|
||||
AuthorityID uint `json:"AuthorityId"`
|
||||
BufferTime int64
|
||||
MustChangePwd bool `json:"mustChangePwd"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func Generate(secret, issuer string, expires time.Duration, userID, authorityID uint, uuid, username, nickname string, mustChange bool) (string, *Claims, error) {
|
||||
if secret == "" {
|
||||
return "", nil, errors.New("empty JWT signing key")
|
||||
}
|
||||
now := time.Now()
|
||||
claims := &Claims{UUID: uuid, ID: userID, Username: username, NickName: nickname, AuthorityID: authorityID, MustChangePwd: mustChange, RegisteredClaims: jwt.RegisteredClaims{Issuer: issuer, IssuedAt: jwt.NewNumericDate(now), NotBefore: jwt.NewNumericDate(now.Add(-time.Second)), ExpiresAt: jwt.NewNumericDate(now.Add(expires))}}
|
||||
token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(secret))
|
||||
return token, claims, err
|
||||
}
|
||||
|
||||
func Parse(tokenString, secret string) (*Claims, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (any, error) {
|
||||
if token.Method != jwt.SigningMethodHS256 {
|
||||
return nil, errors.New("unexpected signing method")
|
||||
}
|
||||
return []byte(secret), nil
|
||||
})
|
||||
if err != nil || !token.Valid {
|
||||
return nil, errors.New("token is invalid")
|
||||
}
|
||||
claims, ok := token.Claims.(*Claims)
|
||||
if !ok {
|
||||
return nil, errors.New("token claims are invalid")
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
Reference in New Issue
Block a user