Files
CloudFlare-ImgBed/functions/utils/auth/tokenExpiration.js
T
MarSeventh 4d2796f54a refactor: unify auth logic into authCore with authScope parameter
- Extract shared authentication logic into utils/auth/authCore.js
- Replace enableBasicAuth/enableAuthCode flags with clear authScope enum (ADMIN/USER/EITHER)
- Move all auth-related files into utils/auth/ subdirectory
- Eliminate duplicated admin auth logic between _middleware.js and dualAuth.js
- Fix: user session no longer grants access to admin-only endpoints
- Fix: dualAuth no longer bypasses authCode when admin is not configured
- Update all 17 import references across the codebase
- Preserve original function signatures (userAuthCheck, dualAuthCheck) for zero caller changes
2026-04-18 13:24:53 +08:00

41 lines
1.2 KiB
JavaScript

/**
* Token 过期时间工具模块(后端)
* 提供 Token 过期判定和自动删除过滤功能
*/
/**
* 判定 Token 是否已过期
* @param {string|null} expiresAt - 过期时间 ISO 8601 字符串,null 表示永不过期
* @param {Date} [now=new Date()] - 当前时间
* @returns {boolean} 是否已过期
*/
export function isExpired(expiresAt, now = new Date()) {
if (expiresAt === null || expiresAt === undefined) {
return false;
}
const expiresDate = new Date(expiresAt);
const currentTime = now instanceof Date ? now : new Date(now);
return currentTime.getTime() > expiresDate.getTime();
}
/**
* 过滤出需要自动删除的 Token 并返回保留的 Token 列表
* @param {Array<Object>} tokens - Token 数组
* @param {Date} [now=new Date()] - 当前时间
* @returns {{ toDelete: Array<Object>, toKeep: Array<Object> }}
*/
export function filterAutoDeleteTokens(tokens, now = new Date()) {
const toDelete = [];
const toKeep = [];
for (const token of tokens) {
if (isExpired(token.expiresAt, now) && token.autoDelete === true) {
toDelete.push(token);
} else {
toKeep.push(token);
}
}
return { toDelete, toKeep };
}