12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package jwt
- import (
- "errors"
- "github.com/golang-jwt/jwt/v5"
- "strings"
- )
- // Option describes the jwt extra data
- type Option struct {
- Key string
- Val any
- }
- // WithOption returns the option from k/v
- func WithOption(key string, val any) Option {
- return Option{
- Key: key,
- Val: val,
- }
- }
- // NewJwtToken returns the jwt token from the given data.
- func NewJwtToken(secretKey string, iat, seconds int64, opt ...Option) (string, error) {
- claims := make(jwt.MapClaims)
- claims["exp"] = iat + seconds
- claims["iat"] = iat
- for _, v := range opt {
- claims[v.Key] = v.Val
- }
- token := jwt.New(jwt.SigningMethodHS256)
- token.Claims = claims
- return token.SignedString([]byte(secretKey))
- }
- // ParseJwtToken parse the real content from jwt string.
- func ParseJwtToken(secretKey, tokenStr string) (map[string]interface{}, error) {
- if secretKey == "" || tokenStr == "" {
- return nil, errors.New("invalid parameters")
- }
- claimsMap := make(jwt.MapClaims)
- token, err := jwt.ParseWithClaims(tokenStr, &claimsMap, func(token *jwt.Token) (interface{}, error) {
- return []byte(secretKey), nil
- })
- if err != nil {
- return nil, err
- }
- if claims, ok := token.Claims.(*jwt.MapClaims); ok {
- return *claims, nil
- }
- return nil, errors.New("unknown error")
- }
- // StripBearerPrefixFromToken remove the bearer prefix in token string.
- func StripBearerPrefixFromToken(token string) string {
- if len(token) > 6 && strings.ToUpper(token[0:7]) == "BEARER " {
- return token[7:]
- }
- return token
- }
|