mirror of https://github.com/golang-jwt/jwt.git
Merge pull request #132 from abourget/master
Implement a "-show" command too.. for debugging purposes..
This commit is contained in:
commit
0ef1a002d3
|
@ -0,0 +1,13 @@
|
||||||
|
`jwt` command-line tool
|
||||||
|
=======================
|
||||||
|
|
||||||
|
This is a simple tool to sign, verify and show JSON Web Tokens from
|
||||||
|
the command line.
|
||||||
|
|
||||||
|
The following will create and sign a token, then verify it and output the original claims:
|
||||||
|
|
||||||
|
echo {\"foo\":\"bar\"} | bin/jwt -key test/sample_key -alg RS256 -sign - | bin/jwt -key test/sample_key.pub -verify -
|
||||||
|
|
||||||
|
To simply display a token, use:
|
||||||
|
|
||||||
|
echo $JWT | jwt -show -
|
|
@ -16,7 +16,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/dgrijalva/jwt-go"
|
jwt "github.com/dgrijalva/jwt-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -29,6 +29,7 @@ var (
|
||||||
// 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")
|
||||||
flagVerify = flag.String("verify", "", "path to JWT token to verify or '-' to read from stdin")
|
flagVerify = flag.String("verify", "", "path to JWT token to verify or '-' to read from stdin")
|
||||||
|
flagShow = flag.String("show", "", "path to JWT file or '-' to read from stdin")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -56,6 +57,8 @@ func start() error {
|
||||||
return signToken()
|
return signToken()
|
||||||
} else if *flagVerify != "" {
|
} else if *flagVerify != "" {
|
||||||
return verifyToken()
|
return verifyToken()
|
||||||
|
} else if *flagShow != "" {
|
||||||
|
return showToken()
|
||||||
} else {
|
} else {
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
return fmt.Errorf("None of the required flags are present. What do you want me to do?")
|
return fmt.Errorf("None of the required flags are present. What do you want me to do?")
|
||||||
|
@ -205,6 +208,39 @@ func signToken() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// showToken pretty-prints the token on the command line.
|
||||||
|
func showToken() error {
|
||||||
|
// get the token
|
||||||
|
tokData, err := loadData(*flagShow)
|
||||||
|
if err != nil {
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
token, err := jwt.Parse(string(tokData), nil)
|
||||||
|
if token == nil {
|
||||||
|
return fmt.Errorf("malformed token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print the token details
|
||||||
|
fmt.Println("Header:")
|
||||||
|
if err := printJSON(token.Header); err != nil {
|
||||||
|
return fmt.Errorf("Failed to output header: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Claims:")
|
||||||
|
if err := printJSON(token.Claims); err != nil {
|
||||||
|
return fmt.Errorf("Failed to output claims: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func isEs() bool {
|
func isEs() bool {
|
||||||
return strings.HasPrefix(*flagAlg, "ES")
|
return strings.HasPrefix(*flagAlg, "ES")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue