diff --git a/package-lock.json b/package-lock.json index 7200c67..1d78550 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "debug": "~2.6.9", "dotenv": "^10.0.0", "dotenv-expand": "^8.0.1", + "envfile": "^7.1.0", "express": "^4.18.2", "got": "^11.8.3", "http-errors": "~1.6.3", @@ -2130,6 +2131,20 @@ "once": "^1.4.0" } }, + "node_modules/envfile": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/envfile/-/envfile-7.1.0.tgz", + "integrity": "sha512-dyH4QnnZsArCLhPASr29eqBWDvKpq0GggQFTmysTT/S9TTmt1JrEKNvTBc09Cd7ujVZQful2HBGRMe2agu7Krg==", + "bin": { + "envfile": "bin.cjs" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://bevry.me/fund" + } + }, "node_modules/error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -6852,6 +6867,11 @@ "once": "^1.4.0" } }, + "envfile": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/envfile/-/envfile-7.1.0.tgz", + "integrity": "sha512-dyH4QnnZsArCLhPASr29eqBWDvKpq0GggQFTmysTT/S9TTmt1JrEKNvTBc09Cd7ujVZQful2HBGRMe2agu7Krg==" + }, "error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", diff --git a/package.json b/package.json index fcdb04b..a621ba5 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "debug": "~2.6.9", "dotenv": "^10.0.0", "dotenv-expand": "^8.0.1", + "envfile": "^7.1.0", "express": "^4.18.2", "got": "^11.8.3", "http-errors": "~1.6.3", diff --git a/tokens.js b/tokens.js index bfbf389..846be41 100644 --- a/tokens.js +++ b/tokens.js @@ -1,5 +1,9 @@ require('dotenv').config() const got = require('got') +const fs = require('node:fs/promises') +const { parse, stringify } = require('envfile') +const sourcePath = '.env' +let expired = true // https://stackoverflow.com/a/69058154/1413302 const isTokenExpired = (token) => (Date.now() >= JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()).exp * 1000) @@ -11,16 +15,27 @@ const isTokenExpired = (token) => (Date.now() >= JSON.parse(Buffer.from(token.sp */ async function generateNewAccessToken() { try { - const { tokenObject } = await got.post(process.env.RERUM_ACCESS_TOKEN_URL, { + const tokenObject = await got.post(process.env.RERUM_ACCESS_TOKEN_URL, { timeout: 10000, - json: { REFRESH_TOKEN: process.env.REFRESH_TOKEN } + json: { "refresh_token": process.env.REFRESH_TOKEN } }).json() - process.env.ACCESS_TOKEN = tokenObject.access_token + try{ + const data = await fs.readFile('./.env', { encoding: 'utf8' }) + // Please note that this parse() will remove all #comments in the .env file. + let env_file_obj = parse(data) + env_file_obj.ACCESS_TOKEN = tokenObject.access_token + await fs.writeFile('./.env', stringify(env_file_obj)) + console.log("TinyNode now has an updated access token.") + } + catch(env_error){ + console.error("Could not write new token property to the file. The access token has not been updated.") + console.error(env_error) + } + } + catch (err) { + console.error("Access token not updated: ", err) } - catch (err) { console.error("Token not updated: ", err) } - - console.warn("Access Token expired. Consider updating your .env files") } /** @@ -28,4 +43,7 @@ async function generateNewAccessToken() { * This does not validate your access token, so you may still be rejected by * your RERUM instance as unauthorized. */ -//if (isTokenExpired(process.env.ACCESS_TOKEN)) { generateNewAccessToken() } +if (isTokenExpired(process.env.ACCESS_TOKEN)) { + console.log("Tiny Node detected an expired access token. Updating the token now.") + generateNewAccessToken() +}