Skip to content

Commit

Permalink
Update dependency packages and fix mkdirp promise
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanho committed Jan 19, 2023
1 parent 759b2ab commit 5891096
Show file tree
Hide file tree
Showing 6 changed files with 2,359 additions and 2,275 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020,
"ecmaVersion": 13,
"ecmaFeatures": {
"globalReturn": true
}
Expand All @@ -35,6 +35,7 @@
"no-prototype-builtins": "off",
"no-unused-vars": "off",
"no-useless-return": "error",
"no-var": "error"
"no-var": "error",
"jsdoc/require-returns": "off"
}
}
178 changes: 92 additions & 86 deletions GPhotos.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const opn = require("open");
const readline = require("readline");
const fs = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");
const { mkdirp } = require("mkdirp");
const { OAuth2Client } = require("google-auth-library");
const Axios = require("axios");
const moment = require("moment");
Expand All @@ -20,99 +20,105 @@ function sleep(ms = 1000) {
setTimeout(resolve, ms);
});
}
/**
*
* @param config
* @param debug
*/
function Auth(config, debug = false) {
const log = debug
? (...args) => {
console.log("[GPHOTOS:AUTH]", ...args);
}
: () => {};
if (config === undefined) config = {};
if (config.keyFilePath === undefined) {
throw new Error('Missing "keyFilePath" from config (This should be where your Credential file is)');
}
if (config.savedTokensPath === undefined) {
throw new Error('Missing "savedTokensPath" from config (this should be where your OAuth2 access tokens will be saved)');
}
let creds = path.resolve(__dirname, config.keyFilePath);
if (!fs.existsSync(creds)) {
throw new Error("Missing Credentials.");

class Auth extends EventEmitter {
#config;
#debug = {};

constructor(config, debug = false) {
super();
this.#config = config;
this.#debug = debug;
this.init();
}
const key = require(config.keyFilePath).installed;
const oauthClient = new OAuth2Client(key.client_id, key.client_secret, key.redirect_uris[0]);
let tokens;
const saveTokens = (first = false) => {
oauthClient.setCredentials(tokens);
let expired = false;
let now = Date.now();
if (tokens.expiry_date < Date.now()) {
expired = true;
log("Token is expired.");

async init() {
const log = this.#debug
? (...args) => {
console.log("[GPHOTOS:AUTH]", ...args);
}
: () => {};
if (this.#config === undefined) config = {};
if (this.#config.keyFilePath === undefined) {
throw new Error('Missing "keyFilePath" from config (This should be where your Credential file is)');
}
if (expired || first) {
oauthClient.refreshAccessToken().then((tk) => {
tokens = tk.credentials;
let tp = path.resolve(__dirname, config.savedTokensPath);
mkdirp(path.dirname(tp), () => {
fs.writeFileSync(tp, JSON.stringify(tokens));
log("Token is refreshed.");
this.emit("ready", oauthClient);
});
});
} else {
log("Token is alive.");
this.emit("ready", oauthClient);
if (this.#config.savedTokensPath === undefined) {
throw new Error('Missing "savedTokensPath" from config (this should be where your OAuth2 access tokens will be saved)');
}
};

const getTokens = () => {
const url = oauthClient.generateAuthUrl({
access_type: "offline",
scope: [config.scope],
});
log("Opening OAuth URL.\n\n" + url + "\n\nReturn here with your code.");
opn(url).catch(() => {
log("Failed to automatically open the URL. Copy/paste this in your browser:\n", url);
});
if (typeof config.tokenInput === "function") {
config.tokenInput(processTokens);
return;
let creds = path.resolve(__dirname, this.#config.keyFilePath);
if (!fs.existsSync(creds)) {
throw new Error("Missing Credentials.");
}
const reader = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
reader.question("> Paste your code: ", processTokens);
};
const processTokens = (oauthCode) => {
if (!oauthCode) process.exit(-1);
oauthClient.getToken(oauthCode, (error, tkns) => {
if (error) throw new Error("Error getting tokens:", error);
tokens = tkns;
saveTokens(true);
});
};
process.nextTick(() => {
if (config.savedTokensPath) {
const key = require(this.#config.keyFilePath).installed;
const oauthClient = new OAuth2Client(key.client_id, key.client_secret, key.redirect_uris[0]);
let tokensCred;
const saveTokens = async (first = false) => {
oauthClient.setCredentials(tokensCred);
let expired = false;
let now = Date.now();
if (tokensCred.expiry_date < Date.now()) {
expired = true;
log("Token is expired.");
}
if (expired || first) {
const tk = await oauthClient.refreshAccessToken();
tokensCred = tk.credentials;
let tp = path.resolve(__dirname, this.#config.savedTokensPath);
await mkdirp(path.dirname(tp));
fs.writeFileSync(tp, JSON.stringify(tokensCred));
log("Token is refreshed.");
this.emit("ready", oauthClient);
} else {
log("Token is alive.");
this.emit("ready", oauthClient);
}
};

const getTokens = () => {
const url = oauthClient.generateAuthUrl({
access_type: "offline",
scope: [this.#config.scope],
});
log("Opening OAuth URL.\n\n" + url + "\n\nReturn here with your code.");
opn(url).catch(() => {
log("Failed to automatically open the URL. Copy/paste this in your browser:\n", url);
});
if (typeof this.#config.tokenInput === "function") {
this.#config.tokenInput(processTokens);
return;
}
const reader = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
reader.question("> Paste your code: ", processTokens);
};
const processTokens = async (oauthCode) => {
if (!oauthCode) process.exit(-1);
try {
let file = path.resolve(__dirname, config.savedTokensPath);
const tokensFile = fs.readFileSync(file);
tokens = JSON.parse(tokensFile);
const tkns = await oauthClient.getToken(oauthCode);
tokensCred = tkns;
await saveTokens(true);
} catch (error) {
getTokens();
} finally {
if (tokens !== undefined) saveTokens();
throw new Error("Error getting tokens:", error);
}
}
});
return this;
};
process.nextTick(() => {
if (this.#config.savedTokensPath) {
try {
let file = path.resolve(__dirname, this.#config.savedTokensPath);
const tokensFile = fs.readFileSync(file);
tokensCred = JSON.parse(tokensFile);
} catch (error) {
getTokens();
} finally {
if (tokensCred !== undefined) saveTokens();
}
}
});
}
}
util.inherits(Auth, EventEmitter);

class GPhotos {
constructor(options) {
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Display your photos from album of Google Photos on MagicMirror
![](https://raw.githubusercontent.com/eouia/MMM-GooglePhotos/master/sc2.png)

## New Updates
**`[2.0.4] - 2023/01/19`**
- Changed: Update dependency packages to fix vulnerability.

**`[2.0.3] - 2022/10/18`**
- Changed: Update dependency packages to fix vulnerability.

Expand Down
9 changes: 4 additions & 5 deletions generate_token_v2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
const fs = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");
const { mkdirp } = require("mkdirp");
const { authenticate } = require("@google-cloud/local-auth");
const config = require("./google_auth.json");

Expand All @@ -18,10 +18,9 @@ async function generate() {
if (client.credentials && config.savedTokensPath) {
if (config.savedTokensPath) {
const tp = path.resolve(__dirname, config.savedTokensPath);
mkdirp(path.dirname(tp), () => {
fs.writeFileSync(tp, JSON.stringify(client.credentials));
console.log("Token is generated. check it. (ls -al)");
});
await mkdirp(path.dirname(tp));
fs.writeFileSync(tp, JSON.stringify(client.credentials));
console.log("Token is generated. check it. (ls -al)");
}
}
}
Expand Down
Loading

0 comments on commit 5891096

Please sign in to comment.