Skip to content

Instantly share code, notes, and snippets.

@yfuruyama
Created October 3, 2018 11:54
Show Gist options
  • Select an option

  • Save yfuruyama/21b3f6c37d6996c4bf0ca738b2ab0574 to your computer and use it in GitHub Desktop.

Select an option

Save yfuruyama/21b3f6c37d6996c4bf0ca738b2ab0574 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strings"
)
func main() {
if len(os.Args) != 2 {
reportError(fmt.Sprintf("Usage: %s TOKEN", os.Args[0]))
}
jwt := os.Args[1]
parts := strings.Split(jwt, ".")
if len(parts) != 3 {
reportError("not JWT string")
}
rawHeader := parts[0]
rawBody := parts[1]
headerJson, err := base64.RawURLEncoding.DecodeString(rawHeader)
if err != nil {
reportError(err.Error())
}
bodyJson, err := base64.RawURLEncoding.DecodeString(rawBody)
if err != nil {
reportError(err.Error())
}
var header interface{}
err = json.Unmarshal(headerJson, &header)
if err != nil {
reportError(err.Error())
}
headerStr, err := json.MarshalIndent(header, "", " ")
if err != nil {
reportError(err.Error())
}
var body interface{}
err = json.Unmarshal(bodyJson, &body)
if err != nil {
reportError(err.Error())
}
bodyStr, err := json.MarshalIndent(body, "", " ")
if err != nil {
reportError(err.Error())
}
fmt.Println(string(headerStr))
fmt.Println(string(bodyStr))
}
func reportError(msg string) {
fmt.Printf("Invalid JWT: %s\n", msg)
os.Exit(-1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment