Skip to content

Commit e3e7b46

Browse files
committed
Add example cognito token alongside public key
1 parent f7e4a4f commit e3e7b46

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

parser_test.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,32 @@ import (
1616
var errKeyFuncError error = fmt.Errorf("error loading key")
1717

1818
var (
19-
jwtTestDefaultKey *rsa.PublicKey
20-
jwtTestRSAPrivateKey *rsa.PrivateKey
21-
jwtTestEC256PublicKey crypto.PublicKey
22-
jwtTestEC256PrivateKey crypto.PrivateKey
23-
defaultKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return jwtTestDefaultKey, nil }
24-
ecdsaKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return jwtTestEC256PublicKey, nil }
25-
emptyKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, nil }
26-
errorKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, errKeyFuncError }
27-
nilKeyFunc jwt.Keyfunc = nil
19+
jwtTestDefaultKey *rsa.PublicKey
20+
jwtTestRSAPrivateKey *rsa.PrivateKey
21+
jwtTestEC256PublicKey crypto.PublicKey
22+
jwtTestEC256PrivateKey crypto.PrivateKey
23+
exampleCognitoPublicKey crypto.PublicKey
24+
defaultKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return jwtTestDefaultKey, nil }
25+
ecdsaKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return jwtTestEC256PublicKey, nil }
26+
exampleCognitoKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return exampleCognitoPublicKey, nil }
27+
emptyKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, nil }
28+
errorKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return nil, errKeyFuncError }
29+
nilKeyFunc jwt.Keyfunc = nil
2830
)
2931

3032
func init() {
3133
// Load public keys
3234
jwtTestDefaultKey = test.LoadRSAPublicKeyFromDisk("test/sample_key.pub")
3335
jwtTestEC256PublicKey = test.LoadECPublicKeyFromDisk("test/ec256-public.pem")
3436

37+
// Load cognito public key - note there is only a public key for this key pair and should only be used for the
38+
// two test cases below.
39+
exampleCognitoPublicKey = test.LoadECPublicKeyFromDisk("test/exampleCognito-public.pem")
40+
3541
// Load private keys
3642
jwtTestRSAPrivateKey = test.LoadRSAPrivateKeyFromDisk("test/sample_key")
3743
jwtTestEC256PrivateKey = test.LoadECPrivateKeyFromDisk("test/ec256-private.pem")
44+
3845
}
3946

4047
var jwtTestData = []struct {
@@ -480,6 +487,24 @@ var setPaddingTestData = []struct {
480487
keyfunc: defaultKeyFunc,
481488
valid: true,
482489
},
490+
{
491+
name: "Error for padded cognito token with padding disabled",
492+
tokenString: "eyJ0eXAiOiJKV1QiLCJraWQiOiIxMjM0NTY3OC1hYmNkLTEyMzQtYWJjZC0xMjM0NTY3OGFiY2QiLCJhbGciOiJFUzI1NiIsImlzcyI6Imh0dHBzOi8vY29nbml0by1pZHAuZXUtd2VzdC0yLmFtYXpvbmF3cy5jb20vIiwiY2xpZW50IjoiN0xUY29QWnJWNDR6ZVg2WUs5VktBcHZPM3EiLCJzaWduZXIiOiJhcm46YXdzOmVsYXN0aWNsb2FkYmFsYW5jaW5nIiwiZXhwIjoxNjI5NDcwMTAxfQ==.eyJzdWIiOiIxMjM0NTY3OC1hYmNkLTEyMzQtYWJjZC0xMjM0NTY3OGFiY2QiLCJlbWFpbF92ZXJpZmllZCI6InRydWUiLCJlbWFpbCI6InVzZXJAZXhhbXBsZS5jb20iLCJ1c2VybmFtZSI6IjEyMzQ1Njc4LWFiY2QtMTIzNC1hYmNkLTEyMzQ1Njc4YWJjZCIsImV4cCI6MTYyOTQ3MDEwMSwiaXNzIjoiaHR0cHM6Ly9jb2duaXRvLWlkcC5ldS13ZXN0LTIuYW1hem9uYXdzLmNvbS8ifQ==.sx0muJ754glJvwWgkHaPrOI3L1gaPjRLLUvOQRk0WitnqC5Dtt1knorcbOzlEcH9zwPM2jYYIAYQz_qEyM3grw==",
493+
claims: nil,
494+
paddedDecode: false,
495+
signingMethod: jwt.SigningMethodES256,
496+
keyfunc: exampleCognitoKeyFunc,
497+
valid: false,
498+
},
499+
{
500+
name: "Validated padded cognito token with padding enabled",
501+
tokenString: "eyJ0eXAiOiJKV1QiLCJraWQiOiIxMjM0NTY3OC1hYmNkLTEyMzQtYWJjZC0xMjM0NTY3OGFiY2QiLCJhbGciOiJFUzI1NiIsImlzcyI6Imh0dHBzOi8vY29nbml0by1pZHAuZXUtd2VzdC0yLmFtYXpvbmF3cy5jb20vIiwiY2xpZW50IjoiN0xUY29QWnJWNDR6ZVg2WUs5VktBcHZPM3EiLCJzaWduZXIiOiJhcm46YXdzOmVsYXN0aWNsb2FkYmFsYW5jaW5nIiwiZXhwIjoxNjI5NDcwMTAxfQ==.eyJzdWIiOiIxMjM0NTY3OC1hYmNkLTEyMzQtYWJjZC0xMjM0NTY3OGFiY2QiLCJlbWFpbF92ZXJpZmllZCI6InRydWUiLCJlbWFpbCI6InVzZXJAZXhhbXBsZS5jb20iLCJ1c2VybmFtZSI6IjEyMzQ1Njc4LWFiY2QtMTIzNC1hYmNkLTEyMzQ1Njc4YWJjZCIsImV4cCI6MTYyOTQ3MDEwMSwiaXNzIjoiaHR0cHM6Ly9jb2duaXRvLWlkcC5ldS13ZXN0LTIuYW1hem9uYXdzLmNvbS8ifQ==.sx0muJ754glJvwWgkHaPrOI3L1gaPjRLLUvOQRk0WitnqC5Dtt1knorcbOzlEcH9zwPM2jYYIAYQz_qEyM3grw==",
502+
claims: nil,
503+
paddedDecode: true,
504+
signingMethod: jwt.SigningMethodES256,
505+
keyfunc: exampleCognitoKeyFunc,
506+
valid: true,
507+
},
483508
}
484509

485510
// Extension of Parsing, this is to test out functionality specific to switching codecs with padding.
@@ -499,6 +524,7 @@ func TestSetPadding(t *testing.T) {
499524
var token *jwt.Token
500525
var err error
501526
parser := new(jwt.Parser)
527+
parser.SkipClaimsValidation = true
502528

503529
// Figure out correct claims type
504530
token, err = parser.ParseWithClaims(data.tokenString, jwt.MapClaims{}, data.keyfunc)

test/exampleCognito-public.pem

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEIcaUjXhC7Mn2OonyfHF+zjblKkns
3+
4GLbILnHrZr+aQwddiff5urCDAZ177t81Mn39CDs3uhlNDxfRIRheGnK/Q==
4+
-----END PUBLIC KEY-----

0 commit comments

Comments
 (0)