-
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce header to override authorizer response (#1328)
- Loading branch information
Showing
7 changed files
with
189 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
exports.echo_authorizer = async function get(context) { | ||
return { | ||
body: JSON.stringify(context.requestContext.authorizer), | ||
statusCode: 200, | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
tests/integration/override-authorizer/override-authorizer.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { resolve } from 'path' | ||
import fetch from 'node-fetch' | ||
import { joinUrl, setup, teardown } from '../_testHelpers/index.js' | ||
|
||
jest.setTimeout(30000) | ||
|
||
const envAuthorizer = { | ||
iam: { | ||
cognitoUser: { | ||
amr: ['unauthenticated'], | ||
identityId: 'env_identity_id', | ||
}, | ||
}, | ||
} | ||
|
||
const headerAuthorizer = { | ||
iam: { | ||
cognitoUser: { | ||
amr: ['unauthenticated'], | ||
identityId: 'header_identity_id', | ||
}, | ||
}, | ||
} | ||
|
||
describe('override authorizer tests', () => { | ||
// init | ||
beforeAll(async () => { | ||
process.env.AUTHORIZER = JSON.stringify(envAuthorizer) | ||
await setup({ | ||
servicePath: resolve(__dirname), | ||
}) | ||
}) | ||
|
||
// cleanup | ||
afterAll(async () => { | ||
process.env.AUTHORIZER = undefined | ||
await teardown() | ||
}) | ||
|
||
// | ||
;[ | ||
{ | ||
description: 'HTTP API Falls back on env variable', | ||
req: { | ||
path: '/gateway_v2_http_api', | ||
headers: {}, | ||
}, | ||
res: { | ||
status: 200, | ||
body: envAuthorizer, | ||
}, | ||
}, | ||
{ | ||
description: 'REST API Falls back on env variable', | ||
req: { | ||
path: '/dev/gateway_v1_rest_api', | ||
headers: {}, | ||
}, | ||
res: { | ||
status: 200, | ||
body: envAuthorizer, | ||
}, | ||
}, | ||
{ | ||
description: 'HTTP API uses override header', | ||
req: { | ||
path: '/gateway_v2_http_api', | ||
headers: { | ||
'sls-offline-authorizer-override': JSON.stringify(headerAuthorizer), | ||
}, | ||
}, | ||
res: { | ||
status: 200, | ||
body: headerAuthorizer, | ||
}, | ||
}, | ||
{ | ||
description: 'HTTP API uses override header', | ||
req: { | ||
path: '/dev/gateway_v1_rest_api', | ||
headers: { | ||
'sls-offline-authorizer-override': JSON.stringify(headerAuthorizer), | ||
}, | ||
}, | ||
res: { | ||
status: 200, | ||
body: headerAuthorizer, | ||
}, | ||
}, | ||
].forEach(({ description, req, res }) => { | ||
test(description, async () => { | ||
const url = joinUrl(TEST_BASE_URL, req.path) | ||
const options = { | ||
headers: req.headers, | ||
} | ||
|
||
const response = await fetch(url, options) | ||
expect(response.status).toEqual(res.status) | ||
|
||
const json = await response.json() | ||
expect(json).toEqual(res.body) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
service: jwt-authorizer | ||
|
||
plugins: | ||
- ../../../ | ||
|
||
custom: | ||
serverless-offline: | ||
noAuth: true | ||
|
||
provider: | ||
memorySize: 128 | ||
name: aws | ||
region: us-east-1 # default | ||
runtime: nodejs12.x | ||
stage: dev | ||
versionFunctions: false | ||
httpApi: | ||
payload: '2.0' | ||
|
||
functions: | ||
user: | ||
events: | ||
- http: | ||
authorizer: | ||
type: 'AWS_IAM' | ||
method: get | ||
path: gateway_v1_rest_api | ||
- httpApi: | ||
authorizer: | ||
type: AWS_IAM | ||
method: get | ||
path: gateway_v2_http_api | ||
handler: handler.echo_authorizer |