Skip to content

Commit

Permalink
Merge pull request #1070 from rloomans/jwt-token-with-multiple-aud-va…
Browse files Browse the repository at this point in the history
…lues

JWT token contains multiple audience values (eg, Auth0)
  • Loading branch information
dherault committed Aug 26, 2020
2 parents b31f89d + d50bf81 commit c0bcba6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/events/http/createJWTAuthScheme.js
Expand Up @@ -54,11 +54,15 @@ export default function createAuthScheme(jwtOptions) {
}

if (
!jwtOptions.audience.includes(aud) &&
jwtOptions.audience.filter((x) =>
Array.isArray(aud) ? aud.includes(x) : aud === x,
).length === 0 &&
!jwtOptions.audience.includes(clientId)
) {
serverlessLog(`JWT Token not from correct audience`)
return Boom.unauthorized('JWT Token not from correct audience')
serverlessLog(`JWT Token does not contain correct audience`)
return Boom.unauthorized(
'JWT Token does not contain correct audience',
)
}

let scopes = null
Expand Down
24 changes: 22 additions & 2 deletions tests/integration/jwt-authorizer/jwt-authorizer.test.js
Expand Up @@ -58,6 +58,11 @@ const correctAudience = {
}
delete correctAudience.client_id

const multipleCorrectAudience = {
...correctAudience,
aud: [baseJWT.client_id, 'https://api.example.com/'],
}

const noScopes = {
...baseJWT,
}
Expand Down Expand Up @@ -104,6 +109,21 @@ describe('jwt authorizer tests', () => {
status: 200,
},

{
description:
'Valid JWT with multiple audience values (one matching single configured audience)',
expected: {
status: 'authorized',
requestContext: {
claims: multipleCorrectAudience,
scopes: ['profile', 'email'],
},
},
jwt: multipleCorrectAudience,
path: '/dev/user1',
status: 200,
},

{
description: 'Valid JWT with scopes',
expected: {
Expand Down Expand Up @@ -144,7 +164,7 @@ describe('jwt authorizer tests', () => {
expected: {
statusCode: 401,
error: 'Unauthorized',
message: 'JWT Token not from correct audience',
message: 'JWT Token does not contain correct audience',
},
jwt: wrongClientId,
path: '/dev/user1',
Expand All @@ -155,7 +175,7 @@ describe('jwt authorizer tests', () => {
expected: {
statusCode: 401,
error: 'Unauthorized',
message: 'JWT Token not from correct audience',
message: 'JWT Token does not contain correct audience',
},
jwt: wrongAudience,
path: '/dev/user1',
Expand Down

0 comments on commit c0bcba6

Please sign in to comment.