mirror of
https://github.com/ZSCGR/CloudFlare-ImgBed.git
synced 2026-08-16 13:43:43 +08:00
- 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
41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
/**
|
|
* 用户端认证工具
|
|
* 基于统一认证核心,保持原有导出签名不变
|
|
*/
|
|
|
|
import { authenticate, AUTH_SCOPE } from './authCore.js';
|
|
|
|
/**
|
|
* 客户端用户认证
|
|
* @param {Object} env - 环境变量
|
|
* @param {URL} url - 请求的URL
|
|
* @param {Request} request - 请求对象
|
|
* @param {string|null} requiredPermission - 如果提供,则进行Token权限验证
|
|
* @return {Promise<boolean>} 返回是否认证通过
|
|
*/
|
|
export async function userAuthCheck(env, url, request, requiredPermission = null) {
|
|
const result = await authenticate({
|
|
env,
|
|
request,
|
|
url,
|
|
requiredPermission,
|
|
authScope: AUTH_SCOPE.USER,
|
|
});
|
|
return result.authorized;
|
|
}
|
|
|
|
export function UnauthorizedResponse(reason) {
|
|
return new Response(reason, {
|
|
status: 401,
|
|
statusText: "Unauthorized",
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'POST, GET',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization, authCode',
|
|
"Content-Type": "text/plain;charset=UTF-8",
|
|
"Cache-Control": "no-store",
|
|
"Content-Length": reason.length,
|
|
},
|
|
});
|
|
}
|