ParseJWTToken parses a JWT token string and extracts its claims without performing cryptographic signature verification. This is useful for introspecting the token's contents to retrieve user information from an ID token after it has been validated by the authentication server.
(token string)
| 56 | // contents to retrieve user information from an ID token after it has been validated |
| 57 | // by the authentication server. |
| 58 | func ParseJWTToken(token string) (*JWTClaims, error) { |
| 59 | parts := strings.Split(token, ".") |
| 60 | if len(parts) != 3 { |
| 61 | return nil, fmt.Errorf("invalid JWT token format: expected 3 parts, got %d", len(parts)) |
| 62 | } |
| 63 | |
| 64 | // Decode the claims (payload) part |
| 65 | claimsData, err := base64URLDecode(parts[1]) |
| 66 | if err != nil { |
| 67 | return nil, fmt.Errorf("failed to decode JWT claims: %w", err) |
| 68 | } |
| 69 | |
| 70 | var claims JWTClaims |
| 71 | if err = json.Unmarshal(claimsData, &claims); err != nil { |
| 72 | return nil, fmt.Errorf("failed to unmarshal JWT claims: %w", err) |
| 73 | } |
| 74 | |
| 75 | return &claims, nil |
| 76 | } |
| 77 | |
| 78 | // base64URLDecode decodes a Base64 URL-encoded string, adding padding if necessary. |
| 79 | // JWTs use a URL-safe Base64 alphabet and omit padding, so this function ensures |
no test coverage detected