Created
March 25, 2023 23:28
-
-
Save mikemackintosh/6d2ee0a1684f32584d5f7ef97b979a75 to your computer and use it in GitHub Desktop.
jwt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"encoding/base64" | |
"strings" | |
) | |
func decodeJWT(jwt string) ([]byte, error) { | |
// Split the JWT into its three parts | |
parts := strings.Split(jwt, ".") | |
if len(parts) != 3 { | |
return nil, errors.New("invalid JWT format") | |
} | |
// Decode the payload, with proper padding and URL decoding | |
payload, err := base64.URLEncoding.DecodeString(addPadding(parts[1])) | |
if err != nil { | |
return nil, err | |
} | |
return payload, nil | |
} | |
func addPadding(s string) string { | |
switch len(s) % 4 { | |
case 1: | |
return s + "===" | |
case 2: | |
return s + "==" | |
case 3: | |
return s + "=" | |
default: | |
return s | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment