Skip to content

Instantly share code, notes, and snippets.

@JWally
Last active January 28, 2024 23:44
Show Gist options
  • Save JWally/3ff5ca5a1d229fec266f9a5b39ddb62c to your computer and use it in GitHub Desktop.
Save JWally/3ff5ca5a1d229fec266f9a5b39ddb62c to your computer and use it in GitHub Desktop.
// Set default response headers as `const`
let DEFAULT_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
};
export const handler = async (event) => {
// N.B. YOU SHOULD MAKE A _REAL_ APPLICATION WITH ERROR CHECKING
// AND SOLID DEV PRACTICES. THIS IS A PROOF-OF-CONCEPT.
//
// IN OTHER WORDS...DON'T TRY THIS AT HOME!
//
const body = JSON.parse(event.body);
const url = "https://fp.keyri.com/v1/client";
const ipAddress = event.requestContext.identity.sourceIp;
const Service_Encryption_Key = process.env.Service_Encryption_Key;
const Service_Decryption_Key = process.env.Service_Decryption_Key;
const API_Key = process.env.API_Key;
//
// BUILD THE PAYLOAD TO SEND TO THE KEYRI API
//
const sendBody = {
...body,
"userId": "undefined",
"eventType": "visits",
"metadata": {},
ipAddress,
headers: event.headers,
API_Key,
Service_Encryption_Key,
Service_Decryption_Key
};
//
// SEND THE PAYLOAD...
//
let returnData = await fetch(url, {
method: 'POST', // Method itself
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(sendBody)
});
let returnDataJson = await returnData.json();
//
// DONE. NOW DECIDE WHAT TO DO NEXT BASED ON
// THE RESPONSE FROM THE KEYRI API
//
// Probably want to get fancier here - this'll do for demo purposes:
let statusCode;
if(returnDataJson?.riskSummary === "allow"){
statusCode = 200;
} else if(returnDataJson?.riskSummary === "warn"){
statusCode = 302;
DEFAULT_HEADERS = {...DEFAULT_HEADERS, "Location": "./bicycle_identification_adventure.html"}
} else {
statusCode = 400;
DEFAULT_HEADERS = {...DEFAULT_HEADERS, "Location": "./blocked.html"}
}
return {
statusCode,
body: JSON.stringify(returnDataJson),
headers: DEFAULT_HEADERS
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment