Created
July 12, 2016 16:02
-
-
Save rjeczalik/6ef09e694a23035fdd2c18484f808713 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"bytes" | |
"encoding/base64" | |
"fmt" | |
"io/ioutil" | |
"os" | |
) | |
func die(v interface{}) { | |
fmt.Fprintln(os.Stderr, v) | |
os.Exit(1) | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
die("usage: jwt-dbg -|file") | |
} | |
var ( | |
p []byte | |
err error | |
) | |
switch os.Args[1] { | |
case "": | |
die("no token file") | |
case "-": | |
p, err = ioutil.ReadAll(os.Stdin) | |
default: | |
p, err = ioutil.ReadFile(os.Args[1]) | |
} | |
if err != nil { | |
die(err) | |
} | |
pp := bytes.Split(p, []byte(".")) | |
if len(pp) != 3 { | |
die("invalid token segments") | |
} | |
for _, p := range pp { | |
for i := len(p) % 4; i > 0; i-- { | |
p = append(p, '=') | |
} | |
p, err := base64.URLEncoding.DecodeString(string(p)) | |
if err != nil { | |
die(err) | |
} | |
fmt.Println(string(p)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment