-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16eb340
commit ff6adbc
Showing
8 changed files
with
83 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{} | ||
{} |
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 |
---|---|---|
@@ -1,27 +1,31 @@ | ||
const currentUser = ({ event }) => { | ||
const { requestContext: { authorizer } } = event; | ||
const { claims } = authorizer.jwt || authorizer; // Dealing with difference between production and sls-offline | ||
const { | ||
requestContext: { authorizer }, | ||
} = event; | ||
const { claims } = authorizer.jwt || authorizer; // Dealing with difference between production and sls-offline | ||
|
||
if (!claims || !claims.sub || !claims.sub.length) { | ||
throw new Error('Unauthorized'); | ||
} | ||
if (!claims || !claims.sub || !claims.sub.length) { | ||
throw new Error("Unauthorized"); | ||
} | ||
|
||
const claimGroups = claims['cognito:groups'] ? claims['cognito:groups'] : '[]'; | ||
const groups = claimGroups | ||
.substring(1, claimGroups.length - 1) | ||
.split(' ') | ||
.filter(Boolean); | ||
const claimGroups = claims["cognito:groups"] | ||
? claims["cognito:groups"] | ||
: "[]"; | ||
const groups = claimGroups | ||
.substring(1, claimGroups.length - 1) | ||
.split(" ") | ||
.filter(Boolean); | ||
|
||
function hasGroup(group) { | ||
return groups.includes(group); | ||
} | ||
function hasGroup(group) { | ||
return groups.includes(group); | ||
} | ||
|
||
return { | ||
...claims, | ||
id: claims.sub, | ||
groups, | ||
hasGroup | ||
}; | ||
return { | ||
...claims, | ||
id: claims.sub, | ||
groups, | ||
hasGroup, | ||
}; | ||
}; | ||
|
||
module.exports = currentUser; |
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 |
---|---|---|
@@ -1,31 +1,31 @@ | ||
const middleware = require('../src'); | ||
const middleware = require("../src"); | ||
|
||
it('eventbridge events are passed through', async() => { | ||
const payload = { | ||
'version': '0', | ||
'id': '03f6b260-9ee8-af5f-4a02-95ebf5fc0925', | ||
'detail-type': 'ArtworkUpdate', | ||
'source': 'some-other-system', | ||
'account': '493638157050', | ||
'time': '2020-11-10T10:36:28Z', | ||
'region': 'eu-west-1', | ||
'resources': [], | ||
'detail': { | ||
'id': 31329, | ||
'action': 'create', | ||
} | ||
}; | ||
it("eventbridge events are passed through", async () => { | ||
const payload = { | ||
version: "0", | ||
id: "03f6b260-9ee8-af5f-4a02-95ebf5fc0925", | ||
"detail-type": "ArtworkUpdate", | ||
source: "some-other-system", | ||
account: "493638157050", | ||
time: "2020-11-10T10:36:28Z", | ||
region: "eu-west-1", | ||
resources: [], | ||
detail: { | ||
id: 31329, | ||
action: "create", | ||
}, | ||
}; | ||
|
||
const exampleApp = middleware(async(event) => { | ||
return event; | ||
}).register(() => ({})); | ||
const exampleApp = middleware(async (event) => { | ||
return event; | ||
}).register(() => ({})); | ||
|
||
expect(JSON.parse(JSON.stringify(await exampleApp(payload)))).toEqual({ | ||
body: JSON.stringify(payload), | ||
headers: { | ||
'Content-Type': 'application/json; charset=utf-8', | ||
}, | ||
isBase64Encoded: false, | ||
statusCode: 200, | ||
}); | ||
expect(JSON.parse(JSON.stringify(await exampleApp(payload)))).toEqual({ | ||
body: JSON.stringify(payload), | ||
headers: { | ||
"Content-Type": "application/json; charset=utf-8", | ||
}, | ||
isBase64Encoded: false, | ||
statusCode: 200, | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
const middleware = require('../src'); | ||
const middleware = require("../src"); | ||
|
||
it('responds correctly to serverless-plugin-warmup', async() => { | ||
const exampleApp = middleware(async() => { | ||
return 'Hello, world!'; | ||
}).register(() => ({})); | ||
it("responds correctly to serverless-plugin-warmup", async () => { | ||
const exampleApp = middleware(async () => { | ||
return "Hello, world!"; | ||
}).register(() => ({})); | ||
|
||
expect(await exampleApp({ | ||
source: 'serverless-plugin-warmup' | ||
})).toEqual('Lambda is warm'); | ||
expect( | ||
await exampleApp({ | ||
source: "serverless-plugin-warmup", | ||
}), | ||
).toEqual("Lambda is warm"); | ||
}); |