library-go/jwt/token.go

92 lines
2.1 KiB
Go
Raw Permalink Normal View History

2024-08-27 05:35:08 +00:00
package jwt
import (
"fmt"
"time"
2024-08-27 05:49:17 +00:00
"github.com/golang-jwt/jwt/v4"
2024-08-27 05:35:08 +00:00
)
func GenerateAccessToken(token Token, data any, sign string, issuer string) (string, error) {
claim := Claims{
Data: data,
RegisteredClaims: jwt.RegisteredClaims{
ID: token.ID,
ExpiresAt: jwt.NewNumericDate(time.Unix(int64(token.ExpiresIn), 0)),
Issuer: issuer,
},
}
accessToken, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claim).
SignedString([]byte(sign))
if err != nil {
return "", err
}
return accessToken, nil
}
func ParseToken(accessToken string, secret string, validate bool) (jwt.MapClaims, error) {
// 跳過驗證的解析
var token *jwt.Token
var err error
if validate {
token, err = jwt.Parse(accessToken, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("token unexpected signing method: %v", token.Header["alg"])
}
2024-08-27 05:49:17 +00:00
2024-08-27 05:35:08 +00:00
return []byte(secret), nil
})
if err != nil {
return jwt.MapClaims{}, err
}
} else {
parser := jwt.NewParser(jwt.WithoutClaimsValidation())
2024-08-27 05:49:17 +00:00
token, err = parser.Parse(accessToken, func(_ *jwt.Token) (interface{}, error) {
2024-08-27 05:35:08 +00:00
return []byte(secret), nil
})
if err != nil {
return jwt.MapClaims{}, err
}
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok && token.Valid {
return jwt.MapClaims{}, fmt.Errorf("token valid error")
}
return claims, nil
}
func ParseClaims(accessToken string, secret string, validate bool) (DataClaims, error) {
claimMap, err := ParseToken(accessToken, secret, validate)
if err != nil {
return DataClaims{}, err
}
claimsData, ok := claimMap["data"].(map[string]any)
if ok {
return convertMap(claimsData), nil
}
return DataClaims{}, fmt.Errorf("get data from claim map error")
}
func convertMap(input map[string]interface{}) map[string]string {
output := make(map[string]string)
for key, value := range input {
switch v := value.(type) {
case string:
output[key] = v
case fmt.Stringer:
output[key] = v.String()
default:
output[key] = fmt.Sprintf("%v", value)
}
}
2024-08-27 05:49:17 +00:00
2024-08-27 05:35:08 +00:00
return output
}