CONTEXT
Should you need to extract claims from a JWT token issued by an identity provider, the following code can be used for that purpose.
package main import ( "fmt" "github.com/golang-jwt/jwt" ) func main() { tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" // typically obtained from and 'Authorization' http header token, _, err := new(jwt.Parser).ParseUnverified(tokenString, jwt.MapClaims{}) if err != nil { fmt.Printf("Error %s", err) } if claims, ok := token.Claims.(jwt.MapClaims); ok { // obtains claims sub := fmt.Sprint(claims["sub"]) name := fmt.Sprint(claims["name"]) // and so on and on // ... fmt.Printf("sub = %s\r\n", sub) fmt.Printf("name = %s", name) } }
Hope it helps!!