forked from CenterForDigitalHumanities/rerum_server_nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Created backend token manager for automatic access token refresh #50
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
Open
Mehulantony
wants to merge
2
commits into
main
Choose a base branch
from
token
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /** | ||
| * Token Manager for RERUM Auth0 integration. | ||
| * | ||
| * This module handles automatic access-token refresh using the existing | ||
| * RERUM/Auth0 refresh-token flow. It does NOT create or manage tokens | ||
| * independently; instead it proxies token refresh requests through the | ||
| * configured Auth0/RERUM token endpoint | ||
| */ | ||
|
|
||
| import config from '../config/index.js' | ||
| import fs from 'node:fs/promises' | ||
|
|
||
| const sourcePath = process.env.ENV_FILE_PATH ?? '.env' | ||
| let refreshInFlight = null | ||
|
|
||
| // Checks if a JWT token is expired based on its 'exp' claim. | ||
| const isTokenExpired = (token) => { | ||
| if (!token) return true | ||
|
|
||
| try { | ||
| const parts = token.split('.') | ||
| if (parts.length !== 3) return true | ||
|
|
||
| const payload = JSON.parse( | ||
| Buffer.from(token.split('.')[1], 'base64').toString() | ||
| ) | ||
|
|
||
| const SKEW_MS = 30 * 1000 // 30 seconds | ||
|
|
||
| return !payload.exp || Date.now() >= (payload.exp * 1000 - SKEW_MS) | ||
| } catch (err) { | ||
| console.error('Failed to parse token:', err) | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| /** Generates a new access token using the stored refresh token. | ||
| * The refresh token must come from the Auth0 UX registration/login flow. | ||
| * If no refresh token is available, the server cannot request a new | ||
| * access token automatically. | ||
|
|
||
| */ | ||
| async function generateNewAccessToken() { | ||
| const refreshToken = config.REFRESH_TOKEN || process.env.REFRESH_TOKEN | ||
| const tokenUrl = config.RERUM_ACCESS_TOKEN_URL || process.env.RERUM_ACCESS_TOKEN_URL | ||
|
|
||
| if (!refreshToken) { | ||
| throw new Error( | ||
| 'No refresh token available. Please register through the Auth0 UX flow first.' | ||
| ) | ||
| } | ||
|
|
||
| if (!tokenUrl) { | ||
| throw new Error('No token refresh URL configured.') | ||
| } | ||
|
|
||
| // Request a new access token from the Auth0/RERUM token endpoint | ||
| const response = await fetch(tokenUrl, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| }, | ||
| body: JSON.stringify({ refresh_token: refreshToken }) | ||
| }) | ||
|
|
||
| let tokenObject | ||
|
|
||
| try { | ||
| tokenObject = await response.json() | ||
| } catch (err) { | ||
| throw new Error(`Failed to parse token response (status ${response.status})`) | ||
| } | ||
|
|
||
| // Handle HTTP or API errors | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| tokenObject.error_description || | ||
| tokenObject.error || | ||
| 'Token refresh failed' | ||
| ) | ||
| } | ||
|
|
||
| //NOTE: We intentionally update process.env at runtime so the latest access token | ||
| // is available across the application. Callers should prefer using | ||
| // getValidAccessToken() instead of reading process.env directly. | ||
| process.env.ACCESS_TOKEN = tokenObject.access_token | ||
|
Mehulantony marked this conversation as resolved.
|
||
|
|
||
| // Auth0 may return a new refresh token depending on configuration | ||
| if (tokenObject.refresh_token) { | ||
| process.env.REFRESH_TOKEN = tokenObject.refresh_token | ||
| } | ||
|
|
||
| try { | ||
| const data = await fs.readFile(sourcePath, { encoding: 'utf8' }) | ||
|
|
||
| let envContent = data | ||
|
|
||
| const accessTokenLine = `ACCESS_TOKEN=${tokenObject.access_token}` | ||
|
|
||
| if (envContent.includes('ACCESS_TOKEN=')) { | ||
| envContent = envContent.replace(/ACCESS_TOKEN=.*/g, accessTokenLine) | ||
| } else { | ||
| envContent += `\n${accessTokenLine}` | ||
| } | ||
|
|
||
| await fs.writeFile(sourcePath, envContent) | ||
|
|
||
| console.log('Access token updated successfully.') | ||
| } catch (err) { | ||
| console.warn('Could not update .env file. Token updated in memory only.') | ||
| } | ||
|
|
||
| return tokenObject.access_token | ||
| } | ||
|
|
||
| /** | ||
| * This function checks whether the existing access token is expired. | ||
| * If it is expired, it automatically generates a new one | ||
| * using the stored refresh token | ||
| */ | ||
|
|
||
| async function checkAndRefreshAccessToken() { | ||
|
Mehulantony marked this conversation as resolved.
|
||
| const accessToken = config.ACCESS_TOKEN || process.env.ACCESS_TOKEN | ||
| const refreshToken = config.REFRESH_TOKEN || process.env.REFRESH_TOKEN | ||
|
|
||
| if (!accessToken && refreshToken) { | ||
| if (!refreshInFlight) { | ||
| refreshInFlight = generateNewAccessToken().finally(() => { | ||
| refreshInFlight = null | ||
| }) | ||
| } | ||
|
|
||
| await refreshInFlight | ||
| return | ||
| } | ||
|
|
||
| if (accessToken && isTokenExpired(accessToken)) { | ||
| console.log('Access token expired. Refreshing...') | ||
|
|
||
| if (!refreshInFlight) { | ||
| refreshInFlight = generateNewAccessToken().finally(() => { | ||
| refreshInFlight = null | ||
| }) | ||
| } | ||
|
|
||
| await refreshInFlight | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve a valid access token for use in API requests. | ||
| */ | ||
| async function getValidAccessToken() { | ||
| await checkAndRefreshAccessToken() | ||
| return process.env.ACCESS_TOKEN || config.ACCESS_TOKEN | ||
| } | ||
|
|
||
| export default { | ||
| isTokenExpired, | ||
| generateNewAccessToken, | ||
| checkAndRefreshAccessToken, | ||
| getValidAccessToken | ||
| } | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.