Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mikemackintosh/6d2ee0a1684f32584d5f7ef97b979a75 to your computer and use it in GitHub Desktop.
Save mikemackintosh/6d2ee0a1684f32584d5f7ef97b979a75 to your computer and use it in GitHub Desktop.
jwt
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