优化jwt token解析方法

This commit is contained in:
白茶清欢 2023-02-12 00:42:27 +08:00
parent 1a6a3e1d31
commit 2560949681

20
jwt.go
View File

@ -46,27 +46,21 @@ func (j *ownJwt) Generate(signMethod jwt.SigningMethod, secret string, claims jw
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:13 2023/2/11
func (j *ownJwt) Validate(inputToken string, secret string, convert func(c jwt.Claims) error) error {
if nil == convert {
return errors.New("convert func is nil")
}
func (j *ownJwt) Validate(inputToken string, secret string, claims jwt.Claims) (jwt.Claims, error) {
var (
token *jwt.Token
err error
)
if token, err = jwt.Parse(inputToken, func(tokenInfo *jwt.Token) (interface{}, error) {
if token, err = jwt.ParseWithClaims(inputToken, claims, func(tokenInfo *jwt.Token) (interface{}, error) {
return []byte(secret), nil
}); nil != err {
return errors.New("token parse fail : " + err.Error())
return nil, errors.New("token parse fail : " + err.Error())
}
if !token.Valid {
return errors.New("token is valid : " + err.Error())
return nil, errors.New("token is valid : " + err.Error())
}
if nil != convert {
if err = convert(token.Claims); nil != err {
return err
}
}
return nil
return token.Claims, nil
}