@mherod/get-cookie 1.1.2 → 1.1.5

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.
Files changed (3) hide show
  1. package/README.md +31 -0
  2. package/index.js +98 -6
  3. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # get-cookie
2
+
3
+ ## What is it?
4
+
5
+ get-cookie is a command line utility that allows you to get the value of a cookie from your locally installed browser.
6
+ It is useful for testing web pages that require authentication.
7
+
8
+ ## Installation
9
+
10
+ To install get-cookie, run the following command:
11
+
12
+ $ npm install @mherod/get-cookie --global
13
+
14
+ ## How do I use it?
15
+
16
+ To use get-cookie, run the following command:
17
+
18
+ $ get-cookie <cookie-name> <domain>
19
+
20
+ For example, to get the value of the `auth` cookie on the `www.example.com` domain, run the following command:
21
+
22
+ $ get-cookie auth www.example.com
23
+
24
+ The output of the command will be the value of the cookie.
25
+
26
+ The library can also be used as a module.
27
+
28
+ ```javascript
29
+ const {getCookie} = require('@mherod/get-cookie');
30
+ getCookie('auth', 'www.example.com').then(console.log);
31
+ ```
package/index.js CHANGED
@@ -29,7 +29,7 @@ async function getChromePassword() {
29
29
  });
30
30
  }
31
31
 
32
- async function getCookie() {
32
+ async function getCookie(name, domain) {
33
33
  throw new Error('Not implemented');
34
34
  }
35
35
 
@@ -70,19 +70,24 @@ async function getFirefoxCookie({name, domain}) {
70
70
  return await doSqliteQuery1(file, sql);
71
71
  }
72
72
 
73
- async function getEncryptedChromeCookie(name, domain) {
73
+ const defaultChromeRoot = `${process.env.HOME}/Library/Application Support/Google/Chrome`;
74
+ const defaultChromeCookies = `${defaultChromeRoot}/Default/Cookies`;
75
+
76
+ async function getEncryptedChromeCookie({name, domain, file = defaultChromeCookies}) {
74
77
  if (name && typeof name !== 'string') {
75
78
  throw new Error('name must be a string');
76
79
  }
77
80
  if (domain && typeof domain !== 'string') {
78
81
  throw new Error('domain must be a string');
79
82
  }
80
- const file = `${process.env.HOME}/Library/Application Support/Google/Chrome/Default/Cookies`;
83
+ if (file && typeof file !== 'string') {
84
+ throw new Error('file must be a string');
85
+ }
81
86
  if (!fs.existsSync(file)) {
82
87
  throw new Error(`File ${file} does not exist`);
83
88
  }
84
89
  if (process.env.VERBOSE) {
85
- console.log(`Trying Chrome cookie ${name} for domain ${domain}`);
90
+ console.log(`Trying Chrome (at ${file}) cookie ${name} for domain ${domain}`);
86
91
  }
87
92
  let sql;
88
93
  sql = `SELECT encrypted_value FROM cookies`;
@@ -248,9 +253,28 @@ async function getChromeCookie({name, domain}) {
248
253
  throw new Error('domain must be a string');
249
254
  }
250
255
  const chromePasswordPromise = getChromePassword();
251
- const encryptedChromeCookiePromise = getEncryptedChromeCookie(name, domain);
256
+ const [encryptedData] = await findAllFiles({
257
+ path: defaultChromeRoot,
258
+ name: 'Cookies'
259
+ }).then(files => {
260
+ const promise = Promise.all(files.map(file => {
261
+ return getEncryptedChromeCookie({
262
+ name: name,
263
+ domain: domain,
264
+ file: file
265
+ });
266
+ }));
267
+ if (process.env.VERBOSE) {
268
+ console.log('promise', promise);
269
+ }
270
+ return promise;
271
+ }).catch(error => {
272
+ if (process.env.VERBOSE) {
273
+ console.log('error', error);
274
+ }
275
+ return [];
276
+ });
252
277
  const password = await chromePasswordPromise;
253
- const encryptedData = await encryptedChromeCookiePromise;
254
278
  if (process.env.VERBOSE) {
255
279
  console.log("Received encrypted", encryptedData);
256
280
  }
@@ -269,6 +293,74 @@ async function getChromeCookie({name, domain}) {
269
293
  return s;
270
294
  }
271
295
 
296
+ /**
297
+ *
298
+ * @param path
299
+ * @param name
300
+ * @param rootSegments
301
+ * @param maxDepth
302
+ * @returns {Promise<[string]>}
303
+ */
304
+ async function findAllFiles({path, name, rootSegments = path.split('/').length, maxDepth = 2}) {
305
+ if (typeof path !== 'string') {
306
+ throw new Error('path must be a string');
307
+ }
308
+ if (typeof name !== 'string') {
309
+ throw new Error('name must be a string');
310
+ }
311
+ if (process.env.VERBOSE) {
312
+ console.log(`Searching for ${name} in ${path}`);
313
+ }
314
+ const files = [];
315
+ let readdirSync;
316
+ try {
317
+ readdirSync = fs.readdirSync(path);
318
+ } catch (e) {
319
+ if (process.env.VERBOSE) {
320
+ console.log(`Error reading ${path}`, e);
321
+ }
322
+ return files;
323
+ }
324
+ for (const file of readdirSync) {
325
+ const filePath = path + '/' + file;
326
+ let stat;
327
+ try {
328
+ stat = fs.statSync(filePath);
329
+ } catch (e) {
330
+ if (process.env.VERBOSE) {
331
+ console.error(`Error getting stat for ${filePath}`, e);
332
+ }
333
+ continue;
334
+ }
335
+ if (stat.isDirectory()) {
336
+ if (filePath.split('/').length < rootSegments + maxDepth) {
337
+ try {
338
+ const subFiles = await findAllFiles({
339
+ path: filePath,
340
+ name: name,
341
+ rootSegments: rootSegments,
342
+ maxDepth: 2
343
+ });
344
+ files.push(...subFiles);
345
+ } catch (e) {
346
+ if (process.env.VERBOSE) {
347
+ console.error(e);
348
+ }
349
+ }
350
+ }
351
+ } else if (file === name) {
352
+ files.push(filePath);
353
+ }
354
+ }
355
+ if (process.env.VERBOSE) {
356
+ if (files.length > 0) {
357
+ console.log(`Found ${(files.length)} ${name} files`);
358
+ console.log(files);
359
+ }
360
+ }
361
+ return files;
362
+ }
363
+
272
364
  /**
273
365
  *
274
366
  * @param path
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mherod/get-cookie",
3
- "version": "1.1.2",
3
+ "version": "1.1.5",
4
4
  "description": "Node.js module for querying a local user's Chrome cookie",
5
5
  "main": "index.js",
6
6
  "bin": "index.js",