Skip to content

Commit

Permalink
cmd: list supported algorithms
Browse files Browse the repository at this point in the history
The help message looks like:
```
  -alg string
        signing algorithm identifier, one of
        ES256, ES384, ES512, EdDSA, HS256, HS384, HS512,
        PS256, PS384, PS512, RS256, RS384, RS512, none
```

Also fixes #63

Signed-off-by: Alexander Yastrebov <yastrebov.alex@gmail.com>
  • Loading branch information
AlexanderYastrebov committed Nov 15, 2021
1 parent 823c014 commit bac3766
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 19 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", "", "signing algorithm identifier, one of\n"+algs())
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,23 @@ func isNone() bool {
return *flagAlg == "none"
}

func algs() string {
algs := jwt.GetAlgorithms()
sort.Strings(algs)
var b strings.Builder
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
}

0 comments on commit bac3766

Please sign in to comment.