update at 2019-09-19 16:56:49 by ehlxr

This commit is contained in:
2019-09-19 16:56:49 +08:00
parent 9845c33896
commit 89e588000f
9 changed files with 25 additions and 249 deletions

View File

@@ -39,13 +39,13 @@ func ShowToken(cmd *cobra.Command) error {
// get the token
tokData, err := loadData(flagToken)
if err != nil {
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))
_, _ = fmt.Fprintf(os.Stderr, "Token len: %v bytes\n", len(tokData))
}
token, err := jwt.Parse(string(tokData), nil)
@@ -56,12 +56,12 @@ func ShowToken(cmd *cobra.Command) error {
// Print the token details
fmt.Println("Header:")
if err := printJSON(token.Header, flagCompact); err != nil {
return fmt.Errorf("Failed to output header: %v", err)
return fmt.Errorf("failed to output header: %v", err)
}
fmt.Println("Claims:")
if err := printJSON(token.Claims, flagCompact); err != nil {
return fmt.Errorf("Failed to output claims: %v", err)
return fmt.Errorf("failed to output claims: %v", err)
}
return nil

View File

@@ -27,7 +27,7 @@ import (
"github.com/atotto/clipboard"
jwt "github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go"
"github.com/spf13/cobra"
)
@@ -49,16 +49,16 @@ func SignToken(cmd *cobra.Command) error {
// get the token data from command line arguments
tokData, err := loadData(flagData)
if err != nil {
return fmt.Errorf("Couldn't read data: %v", err)
return fmt.Errorf("couldn't read data: %v", err)
} else if flagDebug {
fmt.Fprintf(os.Stderr, "Token len: %v bytes\n", len(tokData))
fmt.Fprintf(os.Stderr, "Token data: %v \n", string(tokData))
_, _ = fmt.Fprintf(os.Stderr, "Token len: %v bytes\n", len(tokData))
_, _ = fmt.Fprintf(os.Stderr, "Token data: %v \n", string(tokData))
}
// parse the JSON of the claims
var claims jwt.MapClaims
if err := json.Unmarshal(tokData, &claims); err != nil {
return fmt.Errorf("Couldn't parse claims JSON: %v", err)
return fmt.Errorf("couldn't parse claims JSON: %v", err)
}
// add command line claims
@@ -72,13 +72,13 @@ func SignToken(cmd *cobra.Command) error {
var key interface{}
key, err = loadData(flagKey)
if err != nil {
return fmt.Errorf("Couldn't read key: %v", err)
return fmt.Errorf("couldn't read key: %v", err)
}
// get the signing alg
alg := jwt.GetSigningMethod(flagAlg)
if alg == nil {
return fmt.Errorf("Couldn't find signing method alg: %v", flagAlg)
return fmt.Errorf("couldn't find signing method alg: %v", flagAlg)
}
// create a new token
@@ -93,7 +93,7 @@ func SignToken(cmd *cobra.Command) error {
if isEs(flagAlg) {
if k, ok := key.([]byte); !ok {
return fmt.Errorf("Couldn't convert key data to key")
return fmt.Errorf("couldn't convert key data to key")
} else {
key, err = jwt.ParseECPrivateKeyFromPEM(k)
if err != nil {
@@ -102,7 +102,7 @@ func SignToken(cmd *cobra.Command) error {
}
} else if isRs(flagAlg) {
if k, ok := key.([]byte); !ok {
return fmt.Errorf("Couldn't convert key data to key")
return fmt.Errorf("couldn't convert key data to key")
} else {
key, err = jwt.ParseRSAPrivateKeyFromPEM(k)
if err != nil {
@@ -113,9 +113,9 @@ func SignToken(cmd *cobra.Command) error {
if out, err := token.SignedString(key); err == nil {
fmt.Println(out)
clipboard.WriteAll(out)
_ = clipboard.WriteAll(out)
} else {
return fmt.Errorf("Error signing token: %v", err)
return fmt.Errorf("error signing token: %v", err)
}
return nil

View File

@@ -37,7 +37,7 @@ import (
// Helper func: Read input from specified file or string
func loadData(p string) ([]byte, error) {
if p == "" {
return nil, fmt.Errorf("No path or arg specified")
return nil, fmt.Errorf("no path or arg specified")
}
var rdr io.Reader

View File

@@ -25,7 +25,7 @@ import (
"os"
"regexp"
jwt "github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go"
"github.com/spf13/cobra"
)
@@ -45,13 +45,13 @@ func VerifyToken(cmd *cobra.Command) error {
// get the token
tokData, err := loadData(flagToken)
if err != nil {
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))
_, _ = fmt.Fprintf(os.Stderr, "Token len: %v bytes\n", len(tokData))
}
// Parse the token. Load the key from command line option
@@ -70,23 +70,23 @@ func VerifyToken(cmd *cobra.Command) error {
// Print some debug data
if flagDebug && token != nil {
fmt.Fprintf(os.Stderr, "Header:\n%v\n", token.Header)
fmt.Fprintf(os.Stderr, "Claims:\n%v\n", token.Claims)
_, _ = fmt.Fprintf(os.Stderr, "Header:\n%v\n", token.Header)
_, _ = fmt.Fprintf(os.Stderr, "Claims:\n%v\n", token.Claims)
}
// Print an error if we can't parse for some reason
if err != nil {
return fmt.Errorf("Couldn't parse token: %v", err)
return fmt.Errorf("couldn't parse token: %v", err)
}
// Is token invalid?
if !token.Valid {
return fmt.Errorf("Token is invalid")
return fmt.Errorf("token is invalid")
}
// Print the token details
if err := printJSON(token.Claims, flagCompact); err != nil {
return fmt.Errorf("Failed to output claims: %v", err)
return fmt.Errorf("failed to output claims: %v", err)
}
return nil