Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion run-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

# variable declarations
build_dir=dist
lambdas=( PXEnforcer PXFirstParty PXActivities )
lambdas=( PXEnforcer PXFirstParty PXActivities PXCombined_Enforcer_FirstParty)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this?


# clear previous zips
rm -f *.zip
Expand Down
45 changes: 45 additions & 0 deletions src/PXCombined_Enforcer_FirstParty.ts
Copy link
Contributor

Choose a reason for hiding this comment

The 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 directory and then have another sub-folder for additional examples (combined enforcer + first party, async config, etc) that won't be compiled.

src/

  • HumanFirstPartyLambda
  • HumanEnforceLambda
  • HumanActivitiesLambda
  • code_samples/
    • EnforceAndFirstPartyOriginRequestLambda
    • AsyncConfigLambdaExample
    • ...

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}