mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.2 KiB
53 lines
1.2 KiB
3 years ago
|
package app
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
|
||
|
"github.com/dgrijalva/jwt-go"
|
||
3 years ago
|
"github.com/rocboss/paopao-ce/global"
|
||
|
"github.com/rocboss/paopao-ce/internal/model"
|
||
3 years ago
|
)
|
||
|
|
||
|
type Claims struct {
|
||
|
UID int64 `json:"uid"`
|
||
|
Username string `json:"username"`
|
||
|
jwt.StandardClaims
|
||
|
}
|
||
|
|
||
|
func GetJWTSecret() []byte {
|
||
|
return []byte(global.JWTSetting.Secret)
|
||
|
}
|
||
|
|
||
|
func GenerateToken(User *model.User) (string, error) {
|
||
|
nowTime := time.Now()
|
||
|
expireTime := nowTime.Add(global.JWTSetting.Expire)
|
||
|
claims := Claims{
|
||
|
UID: User.ID,
|
||
|
Username: User.Username,
|
||
|
StandardClaims: jwt.StandardClaims{
|
||
|
ExpiresAt: expireTime.Unix(),
|
||
|
Issuer: global.JWTSetting.Issuer + ":" + User.Salt,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||
|
token, err := tokenClaims.SignedString(GetJWTSecret())
|
||
|
return token, err
|
||
|
}
|
||
|
|
||
|
func ParseToken(token string) (*Claims, error) {
|
||
|
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
||
|
return GetJWTSecret(), nil
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if tokenClaims != nil {
|
||
|
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
|
||
|
return claims, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil, err
|
||
|
}
|