Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created January 12, 2022 09:28
Show Gist options
  • Save velotiotech/99f8bd389b804bc82d883ffed9a09e77 to your computer and use it in GitHub Desktop.
Save velotiotech/99f8bd389b804bc82d883ffed9a09e77 to your computer and use it in GitHub Desktop.
queryHandler: async (event,context) => {
const payload = JSON.parse(event.body);
const documentClient = new DynamoDB.DocumentClient({
region : process.env.REGION
});
try {
switch(payload.action){
case 'expectauth':
const expires_at = parseInt(new Date().getTime() / 1000) + 300;
await documentClient.put({
TableName : process.env.DYNAMODB_TABLE,
Item: {
authkey : payload.authkey,
connectionId : event.requestContext.connectionId,
username : payload.username,
expires_at : expires_at,
authVerified: false
}
}).promise();
return {
statusCode: 200,
body : "OK"
};
case 'getconid':
return {
statusCode: 200,
body: `connectionid:${event.requestContext.connectionId}`
};
case 'verifyauth':
const data = await documentClient.get({
TableName : process.env.DYNAMODB_TABLE,
Key : {
authkey : payload.authkey
}
}).promise();
if(!("Item" in data)){
throw "Failed to query data";
}
if(data.Item.authVerified === true){
return {
statusCode: 200,
body: `authverified:${payload.challengeText}`
}
}
throw "auth verification failed";
}
} catch (error) {
console.log(error);
}
return {
statusCode: 200,
body : "ok"
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment