forked from mirror/jwt
cmd: list supported algorithms (-alg flag) (#123)
This commit is contained in:
parent
823c014036
commit
a725c1f60c
|
@ -14,6 +14,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v4"
|
"github.com/golang-jwt/jwt/v4"
|
||||||
|
@ -21,7 +22,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Options
|
// Options
|
||||||
flagAlg = flag.String("alg", "", "signing algorithm identifier")
|
flagAlg = flag.String("alg", "", algHelp())
|
||||||
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")
|
||||||
|
@ -307,6 +308,25 @@ func isNone() bool {
|
||||||
return *flagAlg == "none"
|
return *flagAlg == "none"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func algHelp() string {
|
||||||
|
algs := jwt.GetAlgorithms()
|
||||||
|
sort.Strings(algs)
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString("signing algorithm identifier, one of\n")
|
||||||
|
for i, alg := range algs {
|
||||||
|
if i > 0 {
|
||||||
|
if i%7 == 0 {
|
||||||
|
b.WriteString(",\n")
|
||||||
|
} else {
|
||||||
|
b.WriteString(", ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.WriteString(alg)
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
type ArgList map[string]string
|
type ArgList map[string]string
|
||||||
|
|
||||||
func (l ArgList) String() string {
|
func (l ArgList) String() string {
|
||||||
|
|
|
@ -33,3 +33,14 @@ func GetSigningMethod(alg string) (method SigningMethod) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAlgorithms returns a list of registered "alg" names
|
||||||
|
func GetAlgorithms() (algs []string) {
|
||||||
|
signingMethodLock.RLock()
|
||||||
|
defer signingMethodLock.RUnlock()
|
||||||
|
|
||||||
|
for alg := range signingMethods {
|
||||||
|
algs = append(algs, alg)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue