-
Notifications
You must be signed in to change notification settings - Fork 0
Added new template for combined firstParty and Enforcer lambdas #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest reorganizing the files and directories for clarity. What I might do is have the most basic usage of our library in the src/
Or something like this. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { CloudFrontRequest, CloudFrontRequestEvent, CloudFrontResponseResult, Context } from 'aws-lambda'; | ||
| import { HumanSecurityEnforcer, HumanSecurityFirstParty } from './px/humansecurity'; | ||
| import { getConfigAsync } from './custom/config'; | ||
|
|
||
|
|
||
| // define and export a handler | ||
| export async function handler( | ||
| event: CloudFrontRequestEvent, | ||
| context: Context | ||
| ): Promise<CloudFrontRequest | CloudFrontResponseResult> { | ||
| // extract request from event | ||
| const request = event.Records[0].cf.request; | ||
|
|
||
| // retrieve and await the configuration | ||
| const config = await getConfigAsync(); | ||
|
|
||
| // initialize enforcer and first party | ||
| const enforcer = HumanSecurityEnforcer.initialize(config); | ||
| const firstParty = HumanSecurityFirstParty.initialize(config); | ||
|
Comment on lines
+18
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be initialized once instead of on every invocation of the handler. Store them in global variables and reuse them. |
||
|
|
||
|
|
||
| // handle first party before calling enforce or other custom logic | ||
| const firstPartyResult = await firstParty.handleFirstParty(request, context); | ||
|
|
||
| // if the result exists, the incoming request is a HUMAN first party request | ||
| // the result should be returned from the handler | ||
| if (firstPartyResult) { | ||
| return firstPartyResult; | ||
| } | ||
|
|
||
| // if the request is not first party, we should enforce the incoming request | ||
| const blockResponse = await enforcer.enforce(request); | ||
|
|
||
| // if we received a response, we should return it from the handler | ||
| // this will return the block response to the end user and prevent the | ||
| // request from reaching the origin server | ||
| if (blockResponse) { | ||
| return blockResponse; | ||
| } | ||
|
|
||
| // if we did not receive a block response, the request can be processed | ||
| // using custom logic as desired and eventually returned from the handler | ||
| // to pass it along to the origin server | ||
| return request; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this?