Skip to content

Instantly share code, notes, and snippets.

@sagar-gavhane
Created March 20, 2019 08:16
Show Gist options
  • Save sagar-gavhane/767b203cf78527066ff6710e02474fe2 to your computer and use it in GitHub Desktop.
Save sagar-gavhane/767b203cf78527066ff6710e02474fe2 to your computer and use it in GitHub Desktop.
AWS Cognito Backend Multi Factor Authentication (MFA)
// mfaSignIn.js
const AWS = require("aws-sdk");
try {
const credentials = {
email: "[email protected]",
password: "xxxxxxxxxxxxxx"
};
const params = {
AuthFlow: "ADMIN_NO_SRP_AUTH",
ClientId: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
UserPoolId: "us-east-1_xxxxxxxx",
AuthParameters: {
email: credentials.email,
password: credentials.password,
USERNAME: "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx", // cognito id
PASSWORD: "xxxxxxxxx"
}
};
const x = new AWS.CognitoIdentityServiceProvider({
apiVersion: "2016-04-18",
region: "us-east-2"
});
x.adminInitiateAuth(params, (err, data) => {
if (err) {
throw err;
}
console.log("data", data);
});
} catch (err) {
console.log("err", err);
}
// verifyMfaOtp.js
const AWS = require("aws-sdk");
try {
const params = {
ChallengeName: "SMS_MFA",
ChallengeResponses: {
SMS_MFA_CODE: "555335", // otp
USERNAME: "xxxxxx-xxx-xxxx-xxxx-xxxxxxxxxxxx"
},
ClientId: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
UserPoolId: "us-east-1_xxxxxxxx",
Session: `fRbuW7ngUJsY66oJDfCbEMZa_PZlal5SB81lGXNU6Gr_8h0JCAA6l7Cz9InTuWAG7YHznaZrQyvFT9SNI41BhrPmUJZsUI
nMWO4a2IikFpYW79FpvMoYt3-SMGVNtP8ly1C9qBpSaKfwrtJMHQSRPcI6sHOxXvBiFhJ2ZHhk9DVlLKcXZB6gwx2B7zexN1L5190Pmw4EGztyvvZsbYCk1YVssdW6OizFNbjp7XuGeH
sFz1mxZZI4Jo6PJx_cHfREkWI7AQH_A5pHtWbwuWXrP9WslSULFEjK1yvK3X0e0lWcrouQr7RYu0bbmvZtwm7idjfkJBO-IvCCufllG7dC_sGuhoqNpzgeRa_7Kgm1XoXtnjJ1UyjIXC
5lRDYEG8KRYJwS4UXSyNw9qPmcTeUHdAzwkBfGcWESxahCi4yNEz_59S-v6evjCYQz8n867RPPnH7HoxrjVhTPcGm_5bhvzm3Dp0nVXIV4VLob7QuXmGaLE1TqI9eQNzL0zMCHjzuuTz
a0x0prrUv_GHWNkZxIgHvRHpCefM205aJk_wt2tcH4N4oL4DyssRRIN_WkJtS7uRjFLgWxVwnd7ZVyvU8LfXkMrqOWlR7zpss5NDkAd-SF1aGbUurKIWutRtO24MAoBUuIZs2rLk`
};
const x = new AWS.CognitoIdentityServiceProvider({
apiVersion: "2016-04-18",
region: "us-east-1"
});
x.adminRespondToAuthChallenge(params, (err, data) => {
if (err) {
throw err;
}
console.log("data", data);
});
} catch (err) {
console.log("err", err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment