@mherod/get-cookie 1.1.7 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/cli.js +14 -61
- package/index.js +87 -7
- package/package.json +2 -1
- package/utils.js +15 -0
package/README.md
CHANGED
|
@@ -11,6 +11,8 @@ To install get-cookie, run the following command:
|
|
|
11
11
|
|
|
12
12
|
$ npm install @mherod/get-cookie --global
|
|
13
13
|
|
|
14
|
+
**Note: Currently only macOS is supported. Windows support is planned for a future release.**
|
|
15
|
+
|
|
14
16
|
## How do I use it?
|
|
15
17
|
|
|
16
18
|
To use get-cookie, run the following command:
|
package/cli.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
const {printStringValue} = require("./utils");
|
|
3
|
+
const {getCookie} = require("./index");
|
|
5
4
|
|
|
6
5
|
if (process.argv) {
|
|
7
6
|
if (process.argv.length > 2) {
|
|
@@ -14,6 +13,9 @@ if (process.argv) {
|
|
|
14
13
|
domain = '%';
|
|
15
14
|
}
|
|
16
15
|
|
|
16
|
+
if (process.argv.includes("--require-jwt")) {
|
|
17
|
+
process.env.REQUIRE_JWT = "true";
|
|
18
|
+
}
|
|
17
19
|
if (process.argv.includes('--verbose')) {
|
|
18
20
|
process.env.VERBOSE = "true";
|
|
19
21
|
}
|
|
@@ -31,65 +33,16 @@ if (process.argv) {
|
|
|
31
33
|
console.log("Verbose mode", process.argv);
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
printStringValue(cookie);
|
|
42
|
-
} else {
|
|
43
|
-
console.log('No cookie found');
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
.catch(err => {
|
|
47
|
-
if (process.env.VERBOSE) {
|
|
48
|
-
console.error(err);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
} else if (process.env.FIREFOX_ONLY) {
|
|
52
|
-
if (process.env.VERBOSE) {
|
|
53
|
-
console.log('firefox only');
|
|
36
|
+
getCookie({
|
|
37
|
+
name: name,
|
|
38
|
+
domain: domain,
|
|
39
|
+
requireJwt: (process.env.REQUIRE_JWT === "true"),
|
|
40
|
+
}).then(cookie => {
|
|
41
|
+
if (typeof cookie === "string") {
|
|
42
|
+
console.log(cookie);
|
|
54
43
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
printStringValue(cookie);
|
|
59
|
-
} else {
|
|
60
|
-
console.log('No cookie found');
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
.catch(err => {
|
|
64
|
-
if (process.env.VERBOSE) {
|
|
65
|
-
console.error("Error getting Firefox cookie", err);
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
} else {
|
|
69
|
-
getChromeCookie({name, domain})
|
|
70
|
-
.catch(err => {
|
|
71
|
-
if (process.env.VERBOSE) {
|
|
72
|
-
console.error("Error getting Chrome cookie", err);
|
|
73
|
-
}
|
|
74
|
-
})
|
|
75
|
-
.then(r => {
|
|
76
|
-
if (typeof r === 'string' && r.trim().length > 0) {
|
|
77
|
-
return r;
|
|
78
|
-
} else {
|
|
79
|
-
return getFirefoxCookie({name, domain})
|
|
80
|
-
.catch(err => {
|
|
81
|
-
if (process.env.VERBOSE) {
|
|
82
|
-
console.error("Error getting Firefox cookie", err);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
.catch((e) => {
|
|
88
|
-
if (process.env.VERBOSE) {
|
|
89
|
-
console.error("Error getting Chrome or Firefox cookie", e);
|
|
90
|
-
}
|
|
91
|
-
})
|
|
92
|
-
.then(printStringValue);
|
|
93
|
-
}
|
|
44
|
+
}).catch(err => {
|
|
45
|
+
console.error(err);
|
|
46
|
+
});
|
|
94
47
|
}
|
|
95
48
|
}
|
package/index.js
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
if (process.platform !== 'darwin') {
|
|
4
|
+
throw new Error('This script only works on macOS');
|
|
5
|
+
}
|
|
6
|
+
|
|
3
7
|
const crypto = require('crypto');
|
|
4
8
|
const fs = require("fs");
|
|
5
|
-
const {execSimple, toStringOrNull, doSqliteQuery1} = require("./utils");
|
|
9
|
+
const {execSimple, toStringOrNull, doSqliteQuery1, printStringValue, toStringValue} = require("./utils");
|
|
10
|
+
const jsonwebtoken = require('jsonwebtoken');
|
|
6
11
|
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
function isValidJwt(token) {
|
|
13
|
+
if (typeof token !== 'string') {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
const result = jsonwebtoken.decode(token, {complete: true});
|
|
18
|
+
if (process.env.VERBOSE) {
|
|
19
|
+
console.log(result);
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
} catch (err) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
9
25
|
}
|
|
10
26
|
|
|
11
27
|
/**
|
|
@@ -16,8 +32,71 @@ async function getChromePassword() {
|
|
|
16
32
|
return await execSimple("security find-generic-password -w -s \"Chrome Safe Storage\"");
|
|
17
33
|
}
|
|
18
34
|
|
|
19
|
-
|
|
20
|
-
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @param params
|
|
38
|
+
* @returns {Promise<string>}
|
|
39
|
+
*/
|
|
40
|
+
async function getCookie(params) {
|
|
41
|
+
if (process.env.CHROME_ONLY) {
|
|
42
|
+
if (process.env.VERBOSE) {
|
|
43
|
+
console.log('chrome only');
|
|
44
|
+
}
|
|
45
|
+
return await getChromeCookie(params)
|
|
46
|
+
.then(cookie => {
|
|
47
|
+
if (cookie && cookie.length > 0) {
|
|
48
|
+
return toStringValue(cookie);
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
.catch(err => {
|
|
52
|
+
if (process.env.VERBOSE) {
|
|
53
|
+
console.error(err);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
} else if (process.env.FIREFOX_ONLY) {
|
|
57
|
+
if (process.env.VERBOSE) {
|
|
58
|
+
console.log('firefox only');
|
|
59
|
+
}
|
|
60
|
+
return await getFirefoxCookie(params)
|
|
61
|
+
.then(b => b.toString('utf8'))
|
|
62
|
+
.then(cookie => {
|
|
63
|
+
if (cookie && cookie.length > 0) {
|
|
64
|
+
return toStringValue(cookie);
|
|
65
|
+
} else {
|
|
66
|
+
console.log('No cookie found');
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
.catch(err => {
|
|
70
|
+
if (process.env.VERBOSE) {
|
|
71
|
+
console.error("Error getting Firefox cookie", err);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
} else {
|
|
75
|
+
return await getChromeCookie(params)
|
|
76
|
+
.catch(err => {
|
|
77
|
+
if (process.env.VERBOSE) {
|
|
78
|
+
console.error("Error getting Chrome cookie", err);
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
.then(r => {
|
|
82
|
+
if (typeof r === 'string' && r.trim().length > 0) {
|
|
83
|
+
return r;
|
|
84
|
+
} else {
|
|
85
|
+
return getFirefoxCookie(params)
|
|
86
|
+
.catch(err => {
|
|
87
|
+
if (process.env.VERBOSE) {
|
|
88
|
+
console.error("Error getting Firefox cookie", err);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
.catch((e) => {
|
|
94
|
+
if (process.env.VERBOSE) {
|
|
95
|
+
console.error("Error getting Chrome or Firefox cookie", e);
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
.then(toStringValue);
|
|
99
|
+
}
|
|
21
100
|
}
|
|
22
101
|
|
|
23
102
|
/**
|
|
@@ -191,9 +270,10 @@ async function decrypt(password, encryptedData) {
|
|
|
191
270
|
*
|
|
192
271
|
* @param {string|undefined} name
|
|
193
272
|
* @param {string} domain
|
|
273
|
+
* @param {boolean} requireJwt
|
|
194
274
|
* @returns {Promise<string>}
|
|
195
275
|
*/
|
|
196
|
-
async function getChromeCookie({name, domain = '%'}) {
|
|
276
|
+
async function getChromeCookie({name, domain = '%', requireJwt = false}) {
|
|
197
277
|
if (name && typeof name !== 'string') {
|
|
198
278
|
throw new Error('name must be a string');
|
|
199
279
|
}
|
|
@@ -262,7 +342,7 @@ async function getChromeCookie({name, domain = '%'}) {
|
|
|
262
342
|
console.log('results', results);
|
|
263
343
|
}
|
|
264
344
|
return results.map(toStringOrNull).find(result => {
|
|
265
|
-
return typeof result === 'string' && result.length > 0;
|
|
345
|
+
return typeof result === 'string' && result.length > 0 && (requireJwt === false || isValidJwt(result));
|
|
266
346
|
});
|
|
267
347
|
}
|
|
268
348
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mherod/get-cookie",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Node.js module for querying a local user's Chrome cookie",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": "cli.js",
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"author": "Matthew Herod",
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
+
"jsonwebtoken": "^8.5.1",
|
|
18
19
|
"sqlite3": "^5.0.8"
|
|
19
20
|
},
|
|
20
21
|
"devDependencies": {
|
package/utils.js
CHANGED
|
@@ -56,6 +56,20 @@ async function execAsBuffer(command) {
|
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
function toStringValue(r) {
|
|
60
|
+
if (process.env.VERBOSE) {
|
|
61
|
+
console.log("Printing value", r);
|
|
62
|
+
}
|
|
63
|
+
if (r) {
|
|
64
|
+
if (typeof r === 'string') {
|
|
65
|
+
return r;
|
|
66
|
+
} else if (r.toString) {
|
|
67
|
+
// noinspection JSCheckFunctionSignatures
|
|
68
|
+
return r.toString('utf8');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
59
73
|
function printStringValue(r) {
|
|
60
74
|
if (process.env.VERBOSE) {
|
|
61
75
|
console.log("Printing value", r);
|
|
@@ -155,6 +169,7 @@ module.exports = {
|
|
|
155
169
|
execSimple: execSimple,
|
|
156
170
|
execAsBuffer: execAsBuffer,
|
|
157
171
|
printStringValue: printStringValue,
|
|
172
|
+
toStringValue,
|
|
158
173
|
toStringOrNull: toStringOrNull,
|
|
159
174
|
doSqliteQuery1
|
|
160
175
|
};
|