Commit 39157129 authored by Felix Heilmeyer's avatar Felix Heilmeyer Committed by Bradley Hilton
Browse files

Enable deployment for users with active 2FA (#46)

* added token&userId authentication option for deploying (instead of username & password)

* added support for 2FA for deployment

* Improve some of the code styling
parent 3cbf0242
......@@ -19,6 +19,16 @@ export default class Deploy extends Command {
url: flags.string({ description: 'where the App should be deployed to' }),
username: flags.string({ char: 'u', dependsOn: ['url'], description: 'username to authenticate with' }),
password: flags.string({ char: 'p', dependsOn: ['username'], description: 'password of the user' }),
code: flags.string({ char: 'c', dependsOn: ['username'], description: '2FA code of the user' }),
i2fa: flags.boolean({ description: 'interactively ask for 2FA code' }),
token: flags.string({
char: 't', dependsOn: ['userid'],
description: 'API token to use with UserID (instead of username & password)',
}),
userid: flags.string({
char: 'i', dependsOn: ['token'],
description: 'UserID to use with API token (instead of username & password)',
}),
};
public async run() {
......@@ -53,14 +63,18 @@ export default class Deploy extends Command {
flags.url = await cli.prompt('What is the server\'s url (include https)?');
}
if (!flags.username) {
if (!flags.username && !flags.token) {
flags.username = await cli.prompt('What is the username?');
}
if (!flags.password) {
if (!flags.password && !flags.token) {
flags.password = await cli.prompt('And, what is the password?', { type: 'hide' });
}
if (flags.i2fa) {
flags.code = await cli.prompt('2FA code', { type: 'hide' });
}
cli.action.start(`${ chalk.green('deploying') } your app`);
const data = new FormData();
......@@ -73,16 +87,41 @@ export default class Deploy extends Command {
// tslint:disable:promise-function-async
private async asyncSubmitData(data: FormData, flags: { [key: string]: any }, fd: FolderDetails): Promise<void> {
const authResult = await fetch(this.normalizeUrl(flags.url, '/api/v1/login'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username: flags.username, password: flags.password }),
}).then((res: Response) => res.json());
if (authResult.status === 'error' || !authResult.data) {
throw new Error('Invalid username and password');
let authResult;
if (!flags.token) {
let credentials: { username: string, password: string, code?: string };
credentials = { username: flags.username, password: flags.password };
if (flags.code) {
credentials.code = flags.code;
}
authResult = await fetch(this.normalizeUrl(flags.url, '/api/v1/login'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(credentials),
}).then((res: Response) => res.json());
if (authResult.status === 'error' || !authResult.data) {
throw new Error('Invalid username and password or missing 2FA code (if active)');
}
} else {
const verificationResult = await fetch(this.normalizeUrl(flags.url, '/api/v1/me'), {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': flags.token,
'X-User-Id': flags.userid,
},
}).then((res: Response) => res.json());
if (!verificationResult.success) {
throw new Error('Invalid API token');
}
authResult = { data: { authToken: flags.token, userId: flags.userid } };
}
let endpoint = '/api/apps';
......@@ -100,7 +139,7 @@ export default class Deploy extends Command {
}).then((res: Response) => res.json());
if (deployResult.status === 'error') {
throw new Error('Unknown error occurred while deploying');
throw new Error(`Unknown error occurred while deploying ${JSON.stringify(deployResult)}`);
} else if (!deployResult.success) {
throw new Error(`Deployment error: ${ deployResult.error }`);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment