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 [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 10, 2021
1 parent 1275a5b commit 0a7fdad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 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", "", fmt.Sprintf("signing algorithm identifier %v", 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,12 @@ func isNone() bool {
return *flagAlg == "none"
}

func algs() []string {
algs := jwt.GetAlgorithms()
sort.Strings(algs)
return algs
}

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 0a7fdad

Please sign in to comment.