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

Fix: do not try to parse a rsa key if algo is hmac #32

Merged
merged 1 commit into from
Sep 29, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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