Files
CloudFlare-ImgBed/functions/api/fetchRes.js
MarSeventh 1e96f2e27b 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

34 lines
954 B
JavaScript

import { dualAuthCheck } from '../utils/auth/dualAuth.js';
export async function onRequest(context) {
// 获取请求体中URL的内容
const {
request,
env,
params,
waitUntil,
next,
data
} = context;
// 双重鉴权检查
const url = new URL(request.url);
const { authorized } = await dualAuthCheck(env, url, request);
if (!authorized) {
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
status: 401,
headers: { 'Content-Type': 'application/json' }
});
}
const jsonRequest = await request.json();
const targetUrl = jsonRequest.url;
if (targetUrl === undefined) {
return new Response('URL is required', { status: 400 })
}
const response = await fetch(targetUrl);
const headers = new Headers(response.headers);
return new Response(response.body, {
headers: headers
})
}