cmd: list supported algorithms (-alg flag) (#123)

This commit is contained in:
Alexander Yastrebov 2021-11-16 15:00:45 +01:00 committed by GitHub
parent 823c014036
commit a725c1f60c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -14,6 +14,7 @@ import (
"io/ioutil"
"os"
"regexp"
"sort"
"strings"
"github.com/golang-jwt/jwt/v4"
@ -21,7 +22,7 @@ import (
var (
// 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")
flagCompact = flag.Bool("compact", false, "output compact JSON")
flagDebug = flag.Bool("debug", false, "print out all kinds of debug data")
@ -307,6 +308,25 @@ func isNone() bool {
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
func (l ArgList) String() string {

View File

@ -33,3 +33,14 @@ func GetSigningMethod(alg string) (method SigningMethod) {
}
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
}