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

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

View File

@ -1,56 +0,0 @@
// Copyright © 2018 ehlxr <ehlxr.me@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// binaryCmd represents the binary command
var binaryCmd = &cobra.Command{
Use: "b",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("binary called")
},
}
func init() {
rootCmd.AddCommand(binaryCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// binaryCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// binaryCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View File

@ -1,56 +0,0 @@
// Copyright © 2018 ehlxr <ehlxr.me@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// decimalCmd represents the decimal command
var decimalCmd = &cobra.Command{
Use: "d",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("decimal called")
},
}
func init() {
rootCmd.AddCommand(decimalCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// decimalCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// decimalCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View File

@ -1,56 +0,0 @@
// Copyright © 2018 ehlxr <ehlxr.me@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// hexadecimalCmd represents the hexadecimal command
var hexadecimalCmd = &cobra.Command{
Use: "h",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("hexadecimal called")
},
}
func init() {
rootCmd.AddCommand(hexadecimalCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// hexadecimalCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// hexadecimalCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View File

@ -1,56 +0,0 @@
// Copyright © 2018 ehlxr <ehlxr.me@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// octalCmd represents the octal command
var octalCmd = &cobra.Command{
Use: "o",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("octal called")
},
}
func init() {
rootCmd.AddCommand(octalCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// octalCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// octalCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/ehlxr/jwt
go 1.12
go 1.13
require (
github.com/BurntSushi/toml v0.3.1 // indirect

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