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