Vestmore_GO/utils/auth/auth.go
2024-04-08 18:13:01 +08:00

28 lines
673 B
Go

package auth
import (
"fmt"
"github.com/golang-jwt/jwt/v5"
)
func ParseToken(tokenstring string, password string, payload jwt.Claims) error {
_, err := jwt.ParseWithClaims(tokenstring, payload, func(t *jwt.Token) (interface{}, error) {
// 验证令牌的签名方法
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
}
return []byte(password), nil
})
if err != nil {
return err
}
return nil
}
func SignToken(password string, payload jwt.Claims) (tokenstring string, err error) {
return jwt.NewWithClaims(jwt.SigningMethodHS256, payload).SignedString([]byte(password))
}