Skip to content

Commit 14e3dc6

Browse files
committed
Change validator.New signature to stop depending on jose pkg directly
1 parent 1d13876 commit 14e3dc6

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

middleware_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/google/go-cmp/cmp"
1313
"github.com/pkg/errors"
14-
"gopkg.in/square/go-jose.v2"
1514
"gopkg.in/square/go-jose.v2/jwt"
1615

1716
"github.com/auth0/go-jwt-middleware/validate/josev2"
@@ -32,9 +31,9 @@ func Test_CheckJWT(t *testing.T) {
3231
func(_ context.Context) (interface{}, error) {
3332
return []byte("secret"), nil
3433
},
35-
jose.HS256,
34+
"HS256",
3635
"testing",
37-
jwt.Audience{},
36+
[]string{},
3837
)
3938
if err != nil {
4039
t.Fatal(err)

validate/josev2/examples/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"time"
99

1010
"github.com/pkg/errors"
11-
"gopkg.in/square/go-jose.v2"
12-
"gopkg.in/square/go-jose.v2/jwt"
1311

1412
"github.com/auth0/go-jwt-middleware"
1513
"github.com/auth0/go-jwt-middleware/validate/josev2"
@@ -56,9 +54,9 @@ func main() {
5654
// Set up the josev2 validator.
5755
validator, err := josev2.New(
5856
keyFunc,
59-
jose.HS256,
57+
"HS256",
6058
"josev2-example",
61-
jwt.Audience{},
59+
[]string{},
6260
josev2.WithCustomClaims(customClaims),
6361
josev2.WithAllowedClockSkew(30*time.Second),
6462
)

validate/josev2/validator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ type UserContext struct {
3131
// and signatureAlgorithm as well as custom options.
3232
func New(
3333
keyFunc func(context.Context) (interface{}, error),
34-
signatureAlgorithm jose.SignatureAlgorithm,
34+
signatureAlgorithm string,
3535
issuerURL string,
36-
audience jwt.Audience,
36+
audience []string,
3737
opts ...Option,
3838
) (*Validator, error) {
3939
if keyFunc == nil {
@@ -51,7 +51,7 @@ func New(
5151

5252
v := &Validator{
5353
keyFunc: keyFunc,
54-
signatureAlgorithm: signatureAlgorithm,
54+
signatureAlgorithm: jose.SignatureAlgorithm(signatureAlgorithm),
5555
expectedClaims: jwt.Expected{
5656
Issuer: issuerURL,
5757
Audience: audience,

validate/josev2/validator_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"testing"
77

88
"github.com/google/go-cmp/cmp"
9-
"gopkg.in/square/go-jose.v2"
109
"gopkg.in/square/go-jose.v2/jwt"
1110
)
1211

@@ -30,7 +29,7 @@ func TestValidator_ValidateToken(t *testing.T) {
3029
name string
3130
token string
3231
keyFunc func(context.Context) (interface{}, error)
33-
algorithm jose.SignatureAlgorithm
32+
algorithm string
3433
customClaims CustomClaims
3534
expectedError error
3635
expectedContext *UserContext
@@ -73,7 +72,7 @@ func TestValidator_ValidateToken(t *testing.T) {
7372
keyFunc: func(context.Context) (interface{}, error) {
7473
return []byte("secret"), nil
7574
},
76-
algorithm: jose.RS256,
75+
algorithm: "RS256",
7776
expectedError: errors.New(`expected "RS256" signing algorithm but token specified "HS256"`),
7877
},
7978
{
@@ -93,7 +92,7 @@ func TestValidator_ValidateToken(t *testing.T) {
9392
expectedError: errors.New("error getting the keys from the key func: key func error message"),
9493
},
9594
{
96-
name: "it throws an error when it fails to deserialize the claims",
95+
name: "it throws an error when it fails to deserialize the claims because the signature is invalid",
9796
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2dvLWp3dC1taWRkbGV3YXJlLmV1LmF1dGgwLmNvbS8iLCJzdWIiOiIxMjM0NTY3ODkwIiwiYXVkIjpbImh0dHBzOi8vZ28tand0LW1pZGRsZXdhcmUtYXBpLyJdfQ.vR2K2tZHDrgsEh9zNWcyk4aljtR6gZK0s2anNGlfwz0",
9897
keyFunc: func(context.Context) (interface{}, error) {
9998
return []byte("secret"), nil
@@ -124,14 +123,14 @@ func TestValidator_ValidateToken(t *testing.T) {
124123
for _, testCase := range testCases {
125124
t.Run(testCase.name, func(t *testing.T) {
126125
if testCase.algorithm == "" {
127-
testCase.algorithm = jose.HS256
126+
testCase.algorithm = "HS256"
128127
}
129128

130129
validator, err := New(
131130
testCase.keyFunc,
132131
testCase.algorithm,
133132
issuer,
134-
jwt.Audience{audience},
133+
[]string{audience},
135134
WithCustomClaims(testCase.customClaims),
136135
)
137136
if err != nil {

0 commit comments

Comments
 (0)