Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

respect 2fa settings #396

Merged
merged 2 commits into from
Feb 6, 2019
Merged
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
15 changes: 14 additions & 1 deletion src/commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const utils = require('../utils');
const QUESTION_USERNAME =
'What is your Zapier login email address? (Ctrl-C to cancel)';
const QUESTION_PASSWORD = 'What is your Zapier login password?';
const QUESTION_TOTP = 'What is your current 6-digit 2FA code?';

const login = async (context, firstTime = true) => {
const checks = [
Expand Down Expand Up @@ -46,7 +47,19 @@ const login = async (context, firstTime = true) => {
const username = await utils.getInput(QUESTION_USERNAME);
const password = await utils.getInput(QUESTION_PASSWORD, { secret: true });

const deployKey = (await utils.createCredentials(username, password)).key;
let goodResponse;
try {
goodResponse = await utils.createCredentials(username, password);
} catch ({ errText, json: { errors } }) {
if (errors[0].startsWith('missing totp_code')) {
const code = await utils.getInput(QUESTION_TOTP);
goodResponse = await utils.createCredentials(username, password, code);
} else {
throw new Error(errText);
}
}

const deployKey = goodResponse.key;

await utils.writeFile(
constants.AUTH_LOCATION,
Expand Down
25 changes: 15 additions & 10 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,21 @@ const callAPI = (route, options, rawError = false) => {
};

// Given a valid username and password - create a new deploy key.
const createCredentials = (username, password) => {
// TODO: 2fa in the future?
return callAPI('/keys', {
skipDeployKey: true,
method: 'POST',
body: {
username,
password
}
});
const createCredentials = (username, password, totpCode) => {
return callAPI(
'/keys',
{
skipDeployKey: true,
method: 'POST',
body: {
username,
password,
totp_code: totpCode
}
},
// if totp is empty, we want a raw request so we can supress an error. If it's here, we want it to be "non-raw"
!totpCode
);
};

// Reads the JSON file in the app directory.
Expand Down