Skip to content

Commit

Permalink
Fix: do not try to parse a rsa key if algo is hmac #32
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Hassert authored Sep 29, 2020
2 parents 42724f9 + 614f5c4 commit f2e979c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions accesscontrol/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func NewJWT(algorithm, name string, claims Claims, reqClaims []string, src Sourc
sourceKey: srcKey,
}

if algo.IsHMAC() {
return jwtObj, nil
}

pubKey, err := parsePublicPEMKey(key)
if err != nil && (err != jwt.ErrKeyMustBePEMEncoded || err != jwt.ErrNotRSAPublicKey) {
cert, err := x509.ParseCertificate(key)
Expand Down
9 changes: 9 additions & 0 deletions accesscontrol/jwt_algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ func NewAlgorithm(a string) Algorithm {
}
}

func (a Algorithm) IsHMAC() bool {
switch a {
case AlgorithmHMAC256, AlgorithmHMAC384, AlgorithmHMAC512:
return true
default:
return false
}
}

func (a Algorithm) String() string {
switch a {
case AlgorithmRSA256:
Expand Down
9 changes: 5 additions & 4 deletions accesscontrol/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/dgrijalva/jwt-go/v4"
Expand Down Expand Up @@ -40,12 +39,14 @@ func TestJWT_Validate(t *testing.T) {
var token string
var tokenErr error

if strings.HasPrefix(signingMethod.Alg(), "HS") {
algo := ac.NewAlgorithm(signingMethod.Alg())

if algo.IsHMAC() {
pubKeyBytes = []byte("mySecretK3y")
token, tokenErr = tok.SignedString(pubKeyBytes)
} else if strings.HasPrefix(signingMethod.Alg(), "RS") {
} else {
token, tokenErr = tok.SignedString(privKey)
}
algo := ac.NewAlgorithm(signingMethod.Alg())

if tokenErr != nil {
t.Error(tokenErr)
Expand Down

0 comments on commit f2e979c

Please sign in to comment.