Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: list supported algorithms #123

Merged
merged 1 commit into from Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion cmd/jwt/main.go
Expand Up @@ -14,14 +14,15 @@ import (
"io/ioutil"
"os"
"regexp"
"sort"
"strings"

"github.com/golang-jwt/jwt/v4"
)

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")
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions signing_method.go
Expand Up @@ -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
}