Skip to content

Commit

Permalink
fix: skip adding authorizer to event if no authorizer is configured (#…
Browse files Browse the repository at this point in the history
…1786)

* dont add authorizor to event if no authorizer is configured

* update test descriptions

* Update src/events/http/HttpServer.js

Co-authored-by: Dorian <[email protected]>

---------

Co-authored-by: Dorian <[email protected]>
  • Loading branch information
cnuss and DorianMazur committed May 19, 2024
1 parent ee20b48 commit 68f8f53
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/events/http/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,15 @@ export default class HttpServer {
)

event = lambdaProxyIntegrationEvent.create()

const customizations = this.#serverless.service.custom
const hasCustomAuthProvider =
customizations?.offline?.customAuthenticationProvider

if (!endpoint.authorizer && !hasCustomAuthProvider) {
log.debug("no authorizer configured, deleting authorizer payload")
delete event.requestContext.authorizer
}
}

log.debug("event:", event)
Expand Down
41 changes: 41 additions & 0 deletions tests/integration/request-authorizer/request-authorizer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("request authorizer tests", () => {
{
description: "should respond with Allow policy",
expected: {
hasAuthorizer: true,
status: "Authorized",
},
options: {
Expand Down Expand Up @@ -97,6 +98,7 @@ describe("request authorizer tests", () => {
{
description: "should respond with Allow policy",
expected: {
hasAuthorizer: true,
status: "Authorized",
},
options: {},
Expand Down Expand Up @@ -149,6 +151,7 @@ describe("request authorizer tests", () => {
{
description: "should respond with Allow policy",
expected: {
hasAuthorizer: true,
status: "Authorized",
},
options: {
Expand Down Expand Up @@ -213,6 +216,7 @@ describe("request authorizer tests", () => {
{
description: "should respond with Allow policy",
expected: {
hasAuthorizer: true,
status: "Authorized",
},
options: {},
Expand Down Expand Up @@ -265,6 +269,7 @@ describe("request authorizer tests", () => {
{
description: "should respond with isAuthorized true",
expected: {
hasAuthorizer: true,
status: "Authorized",
},
options: {
Expand Down Expand Up @@ -329,6 +334,7 @@ describe("request authorizer tests", () => {
{
description: "should respond with Allow policy",
expected: {
hasAuthorizer: true,
status: "Authorized",
},
options: {},
Expand Down Expand Up @@ -375,4 +381,39 @@ describe("request authorizer tests", () => {
},
].forEach(doTest)
})

describe("no authorizer configured", () => {
;[
{
description:
"should respond authorized with no authorizer with payload 1.0",
expected: {
hasAuthorizer: false,
status: "Authorized",
},
options: {
headers: {
Authorization: "Bearer fc3e55ea-e6ec-4bf2-94d2-06ae6efe6e5a",
},
},
path: "/user1-no-authorizer",
status: 200,
},
{
description:
"should respond authorized with no authorizer with payload 2.0",
expected: {
hasAuthorizer: false,
status: "Authorized",
},
options: {
headers: {
Authorization: "Bearer fc3e55ea-e6ec-4bf2-94d2-06ae6efe6e5a",
},
},
path: "/user2-no-authorizer",
status: 200,
},
].forEach(doTest)
})
})
18 changes: 18 additions & 0 deletions tests/integration/request-authorizer/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ functions:
path: /user1-header
handler: src/handler.user

user1NoAuthorizer:
httpApi:
payload: "1.0"
events:
- httpApi:
method: get
path: /user1-no-authorizer
handler: src/handler.user

user2:
events:
- httpApi:
Expand Down Expand Up @@ -110,6 +119,15 @@ functions:
path: /user2simple-querystring
handler: src/handler.user

user2NoAuthorizer:
httpApi:
payload: "2.0"
events:
- httpApi:
method: get
path: /user2-no-authorizer
handler: src/handler.user

requestAuthorizer1FormatHeader:
handler: src/authorizer.requestAuthorizer1Format

Expand Down
8 changes: 6 additions & 2 deletions tests/integration/request-authorizer/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const { stringify } = JSON

export async function user() {
export async function user(event) {
const { authorizer } = event.requestContext
return {
body: stringify({ status: "Authorized" }),
body: stringify({
hasAuthorizer: !!authorizer,
status: "Authorized",
}),
statusCode: 200,
}
}

0 comments on commit 68f8f53

Please sign in to comment.