Skip to content

Commit

Permalink
fix(auth): standardize CloudFormation trigger templates, prevent erro…
Browse files Browse the repository at this point in the history
…rs at runtime (#7219)

* fix: add-to-group PostConfirmation Lambda should not throw

Co-authored-by: Colin Ihrig <[email protected]>
  • Loading branch information
ctjlewis and cjihrig-aws authored Jul 12, 2021
1 parent 753a737 commit f9796bd
Show file tree
Hide file tree
Showing 24 changed files with 152 additions and 201 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* tslint:disable */
/* eslint-disable */

exports.handler = (event, context) => {
exports.handler = async event => {
if (event.request.session.length === 2 && event.request.challengeName === 'CUSTOM_CHALLENGE') {
event.response.publicChallengeParameters = { trigger: 'true' };

event.response.privateChallengeParameters = {};
event.response.privateChallengeParameters.answer = process.env.CHALLENGEANSWER;
}
context.done(null, event);
return event;
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* tslint:disable */
/* eslint-disable */

exports.handler = (event, context) => {
exports.handler = async event => {
if (event.request.session.length === 2 && event.request.challengeName === 'CUSTOM_CHALLENGE') {
event.response.publicChallengeParameters = { trigger: 'true' };
event.response.privateChallengeParameters = { answer: '' };
event.response.challengeMetadata = 'CAPTCHA_CHALLENGE';
}
context.done(null, event);
return event;
};

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.handler = (event, context, callback) => {
exports.handler = async event => {
// Define the URL that you want the user to be directed to after verification is complete
if (event.triggerSource === 'CustomMessage_SignUp') {
const { codeParameter } = event.request;
Expand Down Expand Up @@ -26,7 +26,7 @@ exports.handler = (event, context, callback) => {
redirectUrl,
region,
clientId,
})
}),
).toString('base64');
const bucketUrl = `http://${resourcePrefix}verificationbucket-${process.env.ENV}.s3-website${seperator}${region}.amazonaws.com`;
const url = `${bucketUrl}/?data=${payload}&code=${codeParameter}`;
Expand All @@ -35,8 +35,7 @@ exports.handler = (event, context, callback) => {
event.response.emailSubject = process.env.EMAILSUBJECT;
event.response.emailMessage = message;
console.log('event.response', event.response);
callback(null, event);
} else {
callback(null, event);
}

return event;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.handler = (event, context) => {
exports.handler = async event => {
if (event.request.session.length === 1 && event.request.session[0].challengeName === 'SRP_A') {
event.response.issueTokens = false;
event.response.failAuthentication = false;
Expand All @@ -22,5 +22,6 @@ exports.handler = (event, context) => {
event.response.issueTokens = false;
event.response.failAuthentication = true;
}
context.done(null, event);

return event;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.handler = (event, context) => {
exports.handler = async event => {
if (event.request.session.length === 1 && event.request.session[0].challengeName === 'SRP_A') {
event.response.issueTokens = false;
event.response.failAuthentication = false;
Expand All @@ -22,5 +22,6 @@ exports.handler = (event, context) => {
event.response.issueTokens = false;
event.response.failAuthentication = true;
}
context.done(null, event);

return event;
};

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
/* eslint-disable-line */ const aws = require('aws-sdk');

exports.handler = async (event, context) => {
const cognitoidentityserviceprovider = new aws.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' });
const cognitoidentityserviceprovider = new aws.CognitoIdentityServiceProvider({
apiVersion: '2016-04-18',
});

exports.handler = async event => {
const groupParams = {
GroupName: process.env.GROUP,
UserPoolId: event.userPoolId,
};

const addUserParams = {
GroupName: process.env.GROUP,
UserPoolId: event.userPoolId,
Username: event.userName,
};

/**
* Check if the group exists; if it doesn't, create it.
*/
try {
await cognitoidentityserviceprovider.getGroup(groupParams).promise();
} catch (e) {
await cognitoidentityserviceprovider.createGroup(groupParams).promise();
}
/**
* Then, add the user to the group.
*/
await cognitoidentityserviceprovider.adminAddUserToGroup(addUserParams).promise();

try {
await cognitoidentityserviceprovider.adminAddUserToGroup(addUserParams).promise();
return {
statusCode: 200,
body: JSON.stringify(event)
}
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error)
}
}
return event;
};

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
exports.handler = (event, context, callback) => {
exports.handler = async event => {
// allowed domains
const ald = process.env.DOMAINWHITELIST.split(',').map(d => d.trim());

const { email } = event.request.userAttributes;
const domain = email.substring(email.indexOf('@') + 1);

if (!ald.includes(domain)) {
callback(new Error(`Invalid email domain: ${domain}`), event);
} else {
callback(null, event);
throw new Error(`Invalid email domain: ${domain}`);
}

return event;
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
exports.handler = (event, context, callback) => {
exports.handler = async event => {
// allowed domains
const ald = process.env.DOMAINALLOWLIST.split(',').map(d => d.trim());

const { email } = event.request.userAttributes;
const domain = email.substring(email.indexOf('@') + 1);

if (!ald.includes(domain)) {
callback(new Error(`Invalid email domain: ${domain}`), event);
} else {
callback(null, event);
throw new Error(`Invalid email domain: ${domain}`);
}

return event;
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
exports.handler = (event, context, callback) => {
exports.handler = async event => {
// disallowed domains
const dld = process.env.DOMAINBLACKLIST.split(',').map(d => d.trim());

const { email } = event.request.userAttributes;
const domain = email.substring(email.indexOf('@') + 1);

if (dld.includes(domain)) {
callback(new Error(`Invalid email domain: ${domain}`), event);
} else {
callback(null, event);
throw new Error(`Invalid email domain: ${domain}`);
}

return event;
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
exports.handler = (event, context, callback) => {
exports.handler = async event => {
// disallowed domains
const dld = process.env.DOMAINDENYLIST.split(',').map(d => d.trim());

const { email } = event.request.userAttributes;
const domain = email.substring(email.indexOf('@') + 1);

if (dld.includes(domain)) {
callback(new Error(`Invalid email domain: ${domain}`), event);
} else {
callback(null, event);
throw new Error(`Invalid email domain: ${domain}`);
}

return event;
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.handler = async (event, context, callback) => {
exports.handler = async event => {
event.response = {
claimsOverrideDetails: {
claimsToAddOrOverride: {
Expand All @@ -9,5 +9,5 @@ exports.handler = async (event, context, callback) => {
},
};
// Return to Amazon Cognito
callback(null, event);
return event;
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
exports.handler = (event, context) => {
exports.handler = async event => {
if (event.request.privateChallengeParameters.answer === event.request.challengeAnswer) {
event.response.answerCorrect = true;
} else {
event.response.answerCorrect = false;
}
context.done(null, event);

return event;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
const axios = require('axios');
/* eslint-enable */

exports.handler = (event, context, callback) => {
axios
.post(
`https://www.google.com/recaptcha/api/siteverify?secret=${process.env.RECAPTCHASECRET}&response=${event.request.challengeAnswer}`,
{}
)
.then(response => {
if (response && response.data && response.data.success) {
event.response.answerCorrect = true;
callback(null, event);
} else {
event.response.answerCorrect = false;
callback(new Error('captcha verification error'), event);
}
})
.catch(() => {
event.response.answerCorrect = false;
callback(new Error('captcha verification error'), event);
});
exports.handler = async event => {
const response = await axios.post(
`https://www.google.com/recaptcha/api/siteverify?secret=${process.env.RECAPTCHASECRET}&response=${event.request.challengeAnswer}`,
{},
);
/**
* Verify that the CAPTCHA challenge succeeded, and if it did, indicate so in
* the event response.
*
* If the challenge fails, throw an error.
*/
const challengeSucceeded = response && response.data && response.data.success;
event.response.answerCorrect = !!challengeSucceeded;

if (!challengeSucceeded) {
throw new Error('CAPTCHA verification error');
}

return event;
};
Loading

0 comments on commit f9796bd

Please sign in to comment.