Skip to content
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

validate expiry time #583

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions src/envVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,54 @@ module.exports = function () {
success: success,
}
}

//validate if the expiry token is greater than idle time or not.
function validateTokenExpiry() {
const expiry = process.env.ACCESS_TOKEN_EXPIRY // "30000m"
const allowedIdleTime = parseInt(process.env.ALLOWED_IDLE_TIME, 10) // 1000000

// Extract numeric part and unit
const expiryMatch = expiry.match(/^(\d+)([smhd])$/) // Match digits followed by 's', 'm', 'h', or 'd'
if (!expiryMatch) {
throw new Error("Invalid format for ACCESS_TOKEN_EXPIRY. Use format like '30000m', '5h', etc.")
}

const expiryValue = parseInt(expiryMatch[1], 10) // Numeric part
const expiryUnit = expiryMatch[2] // Time unit (s, m, h, d)

// Convert expiry to milliseconds
let expiryInMilliseconds
switch (expiryUnit) {
case 's':
expiryInMilliseconds = expiryValue * 1000
break
case 'm':
expiryInMilliseconds = expiryValue * 60 * 1000
break
case 'h':
expiryInMilliseconds = expiryValue * 60 * 60 * 1000
break
case 'd':
expiryInMilliseconds = expiryValue * 24 * 60 * 60 * 1000
break
default:
throw new Error('Unsupported time unit in ACCESS_TOKEN_EXPIRY.')
}

// Validate
if (expiryInMilliseconds <= allowedIdleTime) {
throw new Error(
`ACCESS_TOKEN_EXPIRY (${expiryInMilliseconds}ms) must be greater than ALLOWED_IDLE_TIME (${allowedIdleTime}ms).`
)
}

console.log('Token expiry and idle time validation passed.')
}

// Call this function during service initialization
try {
validateTokenExpiry()
} catch (error) {
console.error(error.message)
process.exit(1) // Exit the application if validation fails
}