Enable alternative ways for settings passwords (#101)

* Enable alternative ways for settings passwords:
* the environment variable BW_PASSWORD
* prefix the command line argument with "file:" and the password will read from the first line of that file
* prefix the command line argument with "env:" and the password will be read from that environment variable

* Appveyor fixes

* Switch to using command options for password file and password env

* Lowercase options
This commit is contained in:
Pasi Niemi 2020-05-08 17:38:28 +03:00 committed by GitHub
parent 0092aac275
commit fb7335b927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 7 deletions

View File

@ -14,6 +14,8 @@ import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse'; import { MessageResponse } from '../models/response/messageResponse';
import { NodeUtils } from '../../misc/nodeUtils';
export class LoginCommand { export class LoginCommand {
protected validatedParams: () => Promise<any>; protected validatedParams: () => Promise<any>;
protected success: () => Promise<MessageResponse>; protected success: () => Promise<MessageResponse>;
@ -38,14 +40,21 @@ export class LoginCommand {
return Response.badRequest('Email address is invalid.'); return Response.badRequest('Email address is invalid.');
} }
if ((password == null || password === '') && canInteract) { if (password == null || password === '') {
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({ if (cmd.passwordfile) {
type: 'password', password = await NodeUtils.readFirstLine(cmd.passwordfile);
name: 'password', } else if (cmd.passwordenv && process.env[cmd.passwordenv]) {
message: 'Master password:', password = process.env[cmd.passwordenv];
}); } else if (canInteract) {
password = answer.password; const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
type: 'password',
name: 'password',
message: 'Master password:',
});
password = answer.password;
}
} }
if (password == null || password === '') { if (password == null || password === '') {
return Response.badRequest('Master password is required.'); return Response.badRequest('Master password is required.');
} }

View File

@ -1,5 +1,6 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as readline from 'readline';
export class NodeUtils { export class NodeUtils {
static mkdirpSync(targetDir: string, mode = '700', relative = false, relativeDir: string = null) { static mkdirpSync(targetDir: string, mode = '700', relative = false, relativeDir: string = null) {
@ -13,4 +14,16 @@ export class NodeUtils {
return dir; return dir;
}, initialDir); }, initialDir);
} }
static readFirstLine(fileName: string) {
return new Promise<string>((resolve, reject) => {
const readStream = fs.createReadStream(fileName, {encoding: 'utf8'});
const readInterface = readline.createInterface(readStream);
readInterface
.on('line', (line) => {
readStream.close();
resolve(line);
})
.on('error', (err) => reject(err));
});
}
} }