-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyring.js
46 lines (38 loc) · 1.33 KB
/
keyring.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
var spawn = require("child_process").spawn;
function python(command, cb) {
var stdout = [];
var stderr = [];
var process = spawn("python", ["-c", command]);
process.stdout.setEncoding("utf8");
process.stdout.on("data", on_process_stdout);
process.stderr.setEncoding("utf8");
process.stderr.on("data", on_process_stderr);
process.on("close", on_process_closed);
function on_process_stdout(data) {
stdout.push(data.toString());
}
function on_process_stderr(data) {
stderr.push(data.toString());
}
function on_process_closed(code) {
if(code != 0) return cb({
error: "python exited with code " + code,
stdout: stdout.join(""),
stderr: stderr.join("")
}, null);
cb(null, stdout.join(""));
}
}
exports.set_password = function(system, username, password, cb) {
python("import keyring; keyring.set_password('" + system + "', '" + username + "', '" + password + "')", cb);
};
exports.get_password = function(system, username, cb) {
python("import keyring; print keyring.get_password('" + system + "', '" + username + "')", function(err, res) {
if(err) return cb(err);
var firstLine = /^\w*/.exec(res)[0];
cb(null, firstLine === "None" ? null : firstLine);
});
};
exports.delete_password = function(system, username, cb) {
python("import keyring; keyring.delete_password('" + system + "', '" + username + "')", cb);
};