forked from drudge/node-keychain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeychain.js
197 lines (164 loc) · 4.22 KB
/
keychain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*!
* node-keychain
* Copyright(c) 2012 Nicholas Penree <[email protected]>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var spawn = require('child_process').spawn;
var noop = function () {};
/**
* Basic Keychain Access on Mac computers running Node.js
*
* @class KeychainAccess
* @api public
*/
function KeychainAccess() {
this.executablePath = '/usr/bin/security';
}
/**
* Retreive a password from the keychain.
*
* @param {Object} opts Object containing `account` and `service`
* @param {Function} fn Callback
* @api public
*/
KeychainAccess.prototype.getPassword = function(opts, fn) {
opts = opts || {};
opts.type = (opts.type || 'generic').toLowerCase();
fn = fn || noop;
var err;
if (!opts.account) {
err = new Error('An account is required');
fn(err, null);
return;
}
if (!opts.service) {
err = new Error('A service is required');
fn(err, null);
return;
}
var security = spawn(this.executablePath, [ 'find-'+opts.type+'-password', '-a', opts.account, '-s', opts.service, '-g' ]);
var keychain = '';
var password = '';
security.stdout.on('data', function(d) {
keychain += d.toString();
});
// For better or worse, the last line (containing the actual password) is actually written to stderr instead of stdout.
// Reference: http://blog.macromates.com/2006/keychain-access-from-shell/
security.stderr.on('data', function(d) {
password += d.toString();
});
security.on('exit', function(code, signal) {
if (code !== 0) {
err = new Error('Could not find password');
fn(err, null);
return;
}
if (/password/.test(password)) {
password = password.match(/"(.*)\"/, '')[1];
fn(null, password);
} else {
err = new Error('Could not find password');
fn(err, null);
}
});
};
/**
* Set/update a password in the keychain.
*
* @param {Object} opts Object containing `account`, `service`, and `password`
* @param {Function} fn Callback
* @api public
*/
KeychainAccess.prototype.setPassword = function(opts, fn) {
opts = opts || {};
opts.type = (opts.type || 'generic').toLowerCase();
fn = fn || noop;
var err;
if (!opts.account) {
err = new Error('An account is required');
fn(err, null);
return;
}
if (!opts.service) {
err = new Error('A service is required');
fn(err, null);
return;
}
if (!opts.password) {
err = new Error('A password is required');
fn(err, null);
return;
}
var spawnArgs = [ 'add-'+opts.type+'-password', '-a', opts.account, '-s', opts.service, '-w', opts.password ];
if (opts.comments) {
spawnArgs.push('-j');
spawnArgs.push(opts.comments);
}
if (opts.protocol) {
spawnArgs.push('-r');
spawnArgs.push(opts.protocol);
}
var security = spawn(this.executablePath, spawnArgs);
var self = this;
security.on('exit', function(code, signal) {
if (code !== 0) {
var msg = 'Security returned a non-successful error code: ' + code;
if (code == 45) {
self.deletePassword(opts, function(err) {
if (err) {
console.log(err);
fn(err);
return;
}
self.setPassword(opts, fn);
return;
});
} else {
err = new Error(msg);
fn(err);
return;
}
} else {
fn(null);
}
});
};
/**
* Delete a password from the keychain.
*
* @param {Object} opts Object containing `account`, `service`, and `password`
* @param {Function} fn Callback
* @api public
*/
KeychainAccess.prototype.deletePassword = function(opts, fn) {
opts = opts || {};
opts.type = (opts.type || 'generic').toLowerCase();
fn = fn || noop;
var err;
if (!opts.account) {
err = new Error('An account is required');
fn(err, null);
return;
}
if (!opts.service) {
err = new Error('A service is required');
fn(err, null);
return;
}
var security = spawn(this.executablePath, [ 'delete-'+opts.type+'-password', '-a', opts.account, '-s', opts.service ]);
security.on('exit', function(code, signal) {
if (code !== 0) {
err = new Error('Could not find password');
fn(err);
return;
}
fn(null);
});
};
/**
* Expose new Keychain Access
*/
module.exports = new KeychainAccess();