Skip to content

Commit e016f3c

Browse files
authored
Merge pull request #210 from jacoblee93/master
Update hostnames:remove command and adds support for two factor authentication
2 parents 3dca42f + 363bf51 commit e016f3c

3 files changed

Lines changed: 68 additions & 35 deletions

File tree

cli/commands/hostnames/remove.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class HostnamesRemoveCommand extends Command {
4949
return callback(err);
5050
}
5151

52-
let ids = results.map(host => host.id);
52+
let sids = results.map(host => host.sid);
5353
let answers = await inquirer.prompt(
5454
[
5555
{
@@ -65,7 +65,7 @@ class HostnamesRemoveCommand extends Command {
6565
Hostname: hostnameRoute.formatted_hostname,
6666
Target: hostnameRoute.target,
6767
'Created At': hostnameRoute.created_at,
68-
value: ids[index]
68+
value: sids[index]
6969
};
7070
}),
7171
true,
@@ -97,7 +97,9 @@ class HostnamesRemoveCommand extends Command {
9797

9898
let resource = new APIResource(host, port);
9999
resource.authorize(config.get('ACCESS_TOKEN'));
100-
resource.request('/v1/hostname_routes').destroy(answers.route.value, {}, (err, response) => {
100+
resource.request('/v1/hostname_routes').destroy(null, {
101+
sid: answers.route.value
102+
}, (err, response) => {
101103
if (err) {
102104
return callback(err);
103105
}

cli/commands/login.js

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -55,54 +55,85 @@ class LoginCommand extends Command {
5555
message: 'Password',
5656
});
5757

58-
let loopCb = async (err) => {
58+
let resource = new APIResource(host, port);
5959

60-
err && errorLog(err);
60+
let setAccessToken = (accessToken, cb) => {
6161

62-
let promptResult = await inquirer.prompt(questions);
62+
config.set('ACCESS_TOKEN', accessToken);
63+
config.set('ACTIVE_LIBRARY_TOKEN', '');
64+
config.unset('LIBRARY_TOKENS');
65+
config.write();
6366

64-
email = promptResult.email || email;
65-
password = promptResult.password || password;
66-
67-
let resource = new APIResource(host, port);
67+
console.log();
68+
console.log(chalk.bold.green('Logged in successfully!') + ' Retrieving default Identity Token (API Key)...');
6869

69-
resource.request('v1/access_tokens').create({}, {grant_type: 'password', username: email, password: password}, (err, response) => {
70+
resource.authorize(config.get('ACCESS_TOKEN'));
71+
resource.request('/v1/library_tokens').index({default: true}, (err, response) => {
7072

7173
if (err) {
72-
questions.filter(q => q.name === 'email').forEach(q => q.default = email);
73-
password = null;
74-
return loopCb(err);
74+
return cb(err);
7575
}
7676

77-
config.set('ACCESS_TOKEN', response.data[0].access_token);
78-
config.set('ACTIVE_LIBRARY_TOKEN', '');
79-
config.unset('LIBRARY_TOKENS');
80-
config.write();
77+
let tokens = (response && response.data) || [];
78+
79+
if (!tokens.length) {
80+
console.log('Logged in, but could not retrieve default Identity Token.');
81+
} else {
82+
config.save('ACTIVE_LIBRARY_TOKEN', tokens[0].token);
83+
console.log(`Active Identity Token (API Key) set to: ${chalk.bold(tokens[0].label)}`);
84+
}
8185

8286
console.log();
83-
console.log(chalk.bold.green('Logged in successfully!') + ' Retrieving default Identity Token (API Key)...');
87+
return cb();
8488

85-
resource.authorize(config.get('ACCESS_TOKEN'));
86-
resource.request('/v1/library_tokens').index({default: true}, (err, response) => {
89+
});
90+
}
8791

88-
if (err) {
89-
return callback(err);
90-
}
92+
let loopCb = async (err) => {
9193

92-
let tokens = (response && response.data) || [];
94+
err && errorLog(err);
9395

94-
if (!tokens.length) {
95-
console.log('Logged in, but could not retrieve default Identity Token.');
96-
} else {
97-
config.save('ACTIVE_LIBRARY_TOKEN', tokens[0].token);
98-
console.log(`Active Identity Token (API Key) set to: ${chalk.bold(tokens[0].label)}`);
99-
}
96+
let promptResult = await inquirer.prompt(questions);
10097

101-
console.log();
102-
return callback();
98+
email = promptResult.email || email;
99+
password = promptResult.password || password;
103100

101+
resource.request('v1/login').create({}, {grant_type: 'password', username: email, password: password}, (err, response) => {
104102

105-
});
103+
if (err) {
104+
questions.filter(q => q.name === 'email').forEach(q => q.default = email);
105+
password = null;
106+
return loopCb(err);
107+
}
108+
109+
if (!!response.data[0].factor_identifier) {
110+
console.log();
111+
console.log(`Your account has two-factor authentication enabled. Please enter a valid verification code from your device to finish logging in.`);
112+
console.log();
113+
inquirer.prompt({
114+
name: 'verificationCode',
115+
type: 'input',
116+
default: '',
117+
message: 'Verification Code',
118+
}).then((promptResult) => {
119+
resource.request('v1/login').create({}, {
120+
grant_type: 'password',
121+
username: email,
122+
password: password,
123+
factor_verification_check_sid: response.data[0].sid,
124+
factor_verification_code: promptResult.verificationCode,
125+
}, (err, response) => {
126+
if (err) {
127+
questions.filter(q => q.name === 'email').forEach(q => q.default = email);
128+
password = null;
129+
return loopCb(err);
130+
}
131+
setAccessToken(response.data[0].access_token, callback);
132+
});
133+
});
134+
} else {
135+
setAccessToken(response.data[0].access_token, callback);
136+
}
106137

107138
});
108139

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lib.cli",
3-
"version": "5.3.0",
3+
"version": "5.4.0",
44
"description": "Command Line tools for Autocode - autocode.com",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)