-
Notifications
You must be signed in to change notification settings - Fork 3
Debug Access Token Refresh #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
78a8e18
This does it
thehabes 5347cc6
Test deploy
thehabes d26f91b
token is always expired, for testing purposes
thehabes cddb88d
token is expired once until refreshed, for testing purposes
thehabes ab74ddb
token is expired once until refreshed, for testing purposes
thehabes 1adaa8f
successful auto refresh on dev
thehabes dc4d720
Added an expired token to mock the process one more time.
thehabes c0b6d20
log wording
thehabes 4979fe4
Ready for production
thehabes 42dff19
dumb extra space
thehabes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
|
@@ -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.") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.