From a725c1f60cdc2fc8d2ab144c243457ce073185d2 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Tue, 16 Nov 2021 15:00:45 +0100 Subject: [PATCH] cmd: list supported algorithms (-alg flag) (#123) --- cmd/jwt/main.go | 22 +++++++++++++++++++++- signing_method.go | 11 +++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/jwt/main.go b/cmd/jwt/main.go index a13790c..8706ab0 100644 --- a/cmd/jwt/main.go +++ b/cmd/jwt/main.go @@ -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 { diff --git a/signing_method.go b/signing_method.go index 3269170..241ae9c 100644 --- a/signing_method.go +++ b/signing_method.go @@ -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 +}