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.
Open-IM-Server/test/jwt/main.go

35 lines
862 B

package main
import (
"fmt"
"github.com/golang-jwt/jwt/v4"
)
func main() {
rawJWT := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJpYW0uYXV0aHoubWFybW90ZWR1LmNvbSIsImV4cCI6MTYwNDEyODQwMywiaWF0IjoxNjA0MTI4NDAyLCJpc3MiOiJpYW1jdGwiLCJraWQiOiJpZDEifQ.Itr5u4C-nTeA01qbjjl7RzuPD-aSQazsJZY_Z25aGnI`
// Verify the token
claims := &jwt.MapClaims{}
parsedT, err := jwt.ParseWithClaims(rawJWT, claims, func(token *jwt.Token) (interface{}, error) {
// Validate the alg is HMAC signature
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
if kid, ok := token.Header["kid"].(string); ok {
fmt.Println("kid", kid)
}
return []byte("key1"), nil
})
if err != nil || !parsedT.Valid {
fmt.Println("token valid failed", err)
return
}
fmt.Println("ok")
}