Skip to content

Commit

Permalink
Add support for 2fa login using --2fa
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Oct 1, 2018
1 parent 65fde12 commit 1baeec9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ async function getCredentials(opts) {
password: await read({ prompt: 'Password:', silent: true }),
project:
opts.project || (await read({ prompt: 'Project:', default: configStore.get('project') })),
token: opts['2fa'] && await read({ prompt: '2fa token:' })
};
}

exports.run = async function({ opts }) {
let { email, password, project } = opts;
let { email, password, project, token } = opts;

// We only want to prompt for input outside of the test environment
/* istanbul ignore next */
if (!testing) {
({ email, password, project } = await getCredentials(opts));
({ email, password, project, token } = await getCredentials(opts));
}

if (!project) {
Expand All @@ -49,7 +50,7 @@ exports.run = async function({ opts }) {

return request
.post(`${config.host}/api/v1/login`, {
json: { email, password, project },
json: { email, password, project, token },
})
.then(res => {
configStore.set('apiKey', res.apiKey);
Expand Down
35 changes: 35 additions & 0 deletions test/login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,40 @@ describe('login command', () => {
});
});

it('should error if missing two factor token', done => {
const email = '[email protected]';
const password = '123456';
const project = 'subdomain';

const mock = nock(config.host)
.post('/api/v1/login', { email, password, project })
.reply(400, {
description: 'You must provide a Two Factor Code',
error: 'Bad Request',
});

return login([], { email, password, project }).catch(err => {
mock.done();
assert.equal(err.error, 'Bad Request');
assert.equal(err.description, 'You must provide a Two Factor Code');
return done();
});
});

it('should send 2fa token if provided', () => {
const email = '[email protected]';
const password = '123456';
const project = 'subdomain';
const token = '123456';

const mock = nock(config.host)
.post('/api/v1/login', { email, password, project, token })
.reply(200, { apiKey: 123 });

return login([], { email, password, project, token }).then(() => {
mock.done();
});
});

it('should error if trying to access a project that is not yours', () => {});
});

0 comments on commit 1baeec9

Please sign in to comment.