Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#18 should have a corollary to get out of got() as well.

"http-errors": "~1.6.3",
Expand Down
32 changes: 25 additions & 7 deletions tokens.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -11,21 +15,35 @@ 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.")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works locally here, I hope the runners can do the same. We don't have a test for this either, which should just test the file access, methinks.

}
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")
}

/**
* This will conduct a simple check against the expiry date in your token.
* 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()
}