fixed whitespace issue when reading tokens from stdin

This commit is contained in:
Dave Grijalva 2014-05-19 22:38:15 -07:00
parent 8724cca5ea
commit 6b310a4cc2
1 changed files with 12 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"regexp"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
) )
@ -22,7 +23,7 @@ var (
flagAlg = flag.String("alg", "", "signing algorithm identifier") flagAlg = flag.String("alg", "", "signing algorithm identifier")
flagKey = flag.String("key", "", "path to key file or '-' to read from stdin") flagKey = flag.String("key", "", "path to key file or '-' to read from stdin")
flagPretty = flag.Bool("pretty", true, "output pretty JSON") flagPretty = flag.Bool("pretty", true, "output pretty JSON")
flagDebug = flag.Bool("debug", false, "print out all kinds of debug data") flagDebug = flag.Bool("debug", false, "print out all kinds of debug data")
// Modes - exactly one of these is required // Modes - exactly one of these is required
flagSign = flag.String("sign", "", "path to claims object to sign or '-' to read from stdin") flagSign = flag.String("sign", "", "path to claims object to sign or '-' to read from stdin")
@ -90,6 +91,12 @@ func verifyToken() error {
return fmt.Errorf("Couldn't read token: %v", err) return fmt.Errorf("Couldn't read token: %v", err)
} }
// trim possible whitespace from token
tokData = regexp.MustCompile(`\s*$`).ReplaceAll(tokData, []byte{})
if *flagDebug {
fmt.Fprintf(os.Stderr, "Token len: %v bytes\n", len(tokData))
}
// Parse the token. Load the key from command line option // Parse the token. Load the key from command line option
token, err := jwt.Parse(string(tokData), func(t *jwt.Token) ([]byte, error) { token, err := jwt.Parse(string(tokData), func(t *jwt.Token) ([]byte, error) {
return loadData(*flagKey) return loadData(*flagKey)
@ -97,8 +104,8 @@ func verifyToken() error {
// Print some debug data // Print some debug data
if *flagDebug && token != nil { if *flagDebug && token != nil {
fmt.Printf("Header:\n%v\n", token.Header) fmt.Fprintf(os.Stderr, "Header:\n%v\n", token.Header)
fmt.Printf("Claims:\n%v\n", token.Claims) fmt.Fprintf(os.Stderr, "Claims:\n%v\n", token.Claims)
} }
// Print an error if we can't parse for some reason // Print an error if we can't parse for some reason
@ -129,6 +136,8 @@ func signToken() error {
tokData, err := loadData(*flagSign) tokData, err := loadData(*flagSign)
if err != nil { if err != nil {
return fmt.Errorf("Couldn't read token: %v", err) return fmt.Errorf("Couldn't read token: %v", err)
} else if *flagDebug {
fmt.Println("Token: %v bytes", len(tokData))
} }
// parse the JSON of the claims // parse the JSON of the claims