1 | "use strict";
|
2 |
|
3 |
|
4 | const utils = require('./utils');
|
5 |
|
6 |
|
7 | const fs = require('fs');
|
8 |
|
9 |
|
10 | const prompt = require('prompt');
|
11 |
|
12 |
|
13 | const processor = {};
|
14 |
|
15 |
|
16 | processor.syncFile = (program) => {
|
17 |
|
18 | const passwordFile = program.args[0];
|
19 | const realm = program.args[1];
|
20 | const username = program.args[2];
|
21 |
|
22 |
|
23 | const writeData = utils.encode(program);
|
24 |
|
25 |
|
26 | let found = false;
|
27 | let newLines = [];
|
28 |
|
29 |
|
30 | if (!program.create) {
|
31 |
|
32 | if (!fs.existsSync(passwordFile)) {
|
33 | console.error(`Cannot modify file ${passwordFile}; use '-c' to create it.`);
|
34 | return
|
35 | }
|
36 |
|
37 |
|
38 | const lines = fs.readFileSync(passwordFile, 'UTF-8').split("\n");
|
39 |
|
40 |
|
41 | lines.forEach(line => {
|
42 | if (line.indexOf(`${username}:${realm}:`) === 0) {
|
43 | found = true;
|
44 | newLines.push(writeData);
|
45 | console.log(`Changing password for user ${username} in realm ${realm}.`);
|
46 | } else {
|
47 | newLines.push(line);
|
48 | }
|
49 | });
|
50 | }
|
51 |
|
52 |
|
53 | if (!found) {
|
54 | newLines.push(writeData);
|
55 | console.log(`Adding password for user ${username} in realm ${realm}.`);
|
56 | }
|
57 |
|
58 |
|
59 | fs.writeFileSync(passwordFile, newLines.join("\n") + "\n", 'UTF-8');
|
60 | };
|
61 |
|
62 |
|
63 | function readPassword(program) {
|
64 | prompt.message = "";
|
65 | prompt.delimiter = "";
|
66 |
|
67 | const passportOption = [{name: 'password', description: 'New password:', hidden: true}];
|
68 | const rePassportOption = [{name: 'rePassword', description: 'Re-type new password:', hidden: true}];
|
69 |
|
70 |
|
71 | prompt.get(passportOption, (err, result) => {
|
72 | if (!err) {
|
73 | const password = result.password;
|
74 | setTimeout(function () {
|
75 | prompt.get(rePassportOption, (err, result) => {
|
76 | if (!err && password == result.rePassword) {
|
77 | program.args.push(password);
|
78 |
|
79 | try {
|
80 | processor.syncFile(program);
|
81 | } catch (err) {
|
82 | console.error(err.message);
|
83 | }
|
84 | } else {
|
85 | console.error("\nPassword verification error.");
|
86 | }
|
87 | });
|
88 | }, 50);
|
89 | } else {
|
90 | console.error("\nPassword verification error.");
|
91 | }
|
92 | });
|
93 | }
|
94 |
|
95 |
|
96 | processor.exec = (program) => {
|
97 | if (program.args.length === 3) {
|
98 | readPassword(program);
|
99 | } else {
|
100 | program.help();
|
101 | }
|
102 | };
|
103 |
|
104 |
|
105 | module.exports = processor; |
\ | No newline at end of file |