mirror of https://github.com/golang-jwt/jwt.git
allow claims and headers to be specified at command line
This commit is contained in:
parent
a601269ab7
commit
53194fccb3
|
@ -25,14 +25,20 @@ var (
|
||||||
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")
|
||||||
flagCompact = flag.Bool("compact", false, "output compact JSON")
|
flagCompact = flag.Bool("compact", false, "output compact 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")
|
||||||
|
flagClaims = make(ArgList)
|
||||||
|
flagHead = make(ArgList)
|
||||||
|
|
||||||
// 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, '-' to read from stdin, or '+' to use only -claim args")
|
||||||
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")
|
flagShow = flag.String("show", "", "path to JWT file or '-' to read from stdin")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Plug in Var flags
|
||||||
|
flag.Var(flagClaims, "claim", "add additional claims. may be used more than once")
|
||||||
|
flag.Var(flagHead, "head", "add additional header params. may be used more than once")
|
||||||
|
|
||||||
// Usage message if you ask for -help or if you mess up inputs.
|
// Usage message if you ask for -help or if you mess up inputs.
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||||
|
@ -74,6 +80,8 @@ func loadData(p string) ([]byte, error) {
|
||||||
var rdr io.Reader
|
var rdr io.Reader
|
||||||
if p == "-" {
|
if p == "-" {
|
||||||
rdr = os.Stdin
|
rdr = os.Stdin
|
||||||
|
} else if p == "+" {
|
||||||
|
return []byte("{}"), nil
|
||||||
} else {
|
} else {
|
||||||
if f, err := os.Open(p); err == nil {
|
if f, err := os.Open(p); err == nil {
|
||||||
rdr = f
|
rdr = f
|
||||||
|
@ -171,6 +179,13 @@ func signToken() error {
|
||||||
return fmt.Errorf("Couldn't parse claims JSON: %v", err)
|
return fmt.Errorf("Couldn't parse claims JSON: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add command line claims
|
||||||
|
if len(flagClaims) > 0 {
|
||||||
|
for k, v := range flagClaims {
|
||||||
|
claims[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// get the key
|
// get the key
|
||||||
var key interface{}
|
var key interface{}
|
||||||
key, err = loadData(*flagKey)
|
key, err = loadData(*flagKey)
|
||||||
|
@ -187,6 +202,13 @@ func signToken() error {
|
||||||
// create a new token
|
// create a new token
|
||||||
token := jwt.NewWithClaims(alg, claims)
|
token := jwt.NewWithClaims(alg, claims)
|
||||||
|
|
||||||
|
// add command line headers
|
||||||
|
if len(flagHead) > 0 {
|
||||||
|
for k, v := range flagHead {
|
||||||
|
token.Header[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if isEs() {
|
if isEs() {
|
||||||
if k, ok := key.([]byte); !ok {
|
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")
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ArgList map[string]string
|
||||||
|
|
||||||
|
func (l ArgList) String() string {
|
||||||
|
data, _ := json.Marshal(l)
|
||||||
|
return string(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l ArgList) Set(arg string) error {
|
||||||
|
parts := strings.SplitN(arg, "=", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return fmt.Errorf("Invalid argument '%v'. Must use format 'key=value'. %v", arg, parts)
|
||||||
|
}
|
||||||
|
l[parts[0]] = parts[1]
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue