| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| function maskToken(token, visiblePercent = 70) { |
| if (!token || typeof token !== 'string') { |
| return '[EMPTY]' |
| } |
|
|
| const { length } = token |
|
|
| |
| if (length <= 2) { |
| return '*'.repeat(length) |
| } |
|
|
| if (length <= 5) { |
| return token.slice(0, 1) + '*'.repeat(length - 1) |
| } |
|
|
| if (length <= 10) { |
| const visibleLength = Math.min(5, length - 2) |
| const front = token.slice(0, visibleLength) |
| return front + '*'.repeat(length - visibleLength) |
| } |
|
|
| |
| const visibleLength = Math.floor(length * (visiblePercent / 100)) |
|
|
| |
| const frontLength = Math.ceil(visibleLength * 0.6) |
| const backLength = visibleLength - frontLength |
|
|
| |
| const front = token.slice(0, frontLength) |
| const back = token.slice(-backLength) |
| const middle = '*'.repeat(length - visibleLength) |
|
|
| return `${front}${middle}${back}` |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function maskTokensInObject( |
| obj, |
| tokenFields = ['accessToken', 'refreshToken', 'access_token', 'refresh_token'] |
| ) { |
| if (!obj || typeof obj !== 'object') { |
| return obj |
| } |
|
|
| const masked = { ...obj } |
|
|
| tokenFields.forEach((field) => { |
| if (masked[field]) { |
| masked[field] = maskToken(masked[field]) |
| } |
| }) |
|
|
| return masked |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function formatTokenRefreshLog(accountId, accountName, tokens, status, message = '') { |
| const log = { |
| timestamp: new Date().toISOString(), |
| event: 'token_refresh', |
| accountId, |
| accountName, |
| status, |
| message |
| } |
|
|
| if (tokens) { |
| log.tokens = { |
| accessToken: tokens.accessToken ? maskToken(tokens.accessToken) : '[NOT_PROVIDED]', |
| refreshToken: tokens.refreshToken ? maskToken(tokens.refreshToken) : '[NOT_PROVIDED]', |
| expiresAt: tokens.expiresAt || '[NOT_PROVIDED]' |
| } |
| } |
|
|
| return log |
| } |
|
|
| module.exports = { |
| maskToken, |
| maskTokensInObject, |
| formatTokenRefreshLog |
| } |
|
|