49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { app } from '../app.ts';
|
||
import './cnb-env/check.ts'
|
||
import './repo/index.ts'
|
||
import './workspace/index.ts'
|
||
import './call/index.ts'
|
||
import './cnb-env/index.ts'
|
||
import './knowledge/index.ts'
|
||
import './issues/index.ts'
|
||
|
||
/**
|
||
* 验证上下文中的 App ID 是否与指定的 App ID 匹配
|
||
* @param {any} ctx - 上下文对象,可能包含 appId 属性
|
||
* @param {string} appId - 需要验证的目标 App ID
|
||
* @returns {boolean} 如果 ctx 中包含 appId 且匹配则返回 true,否则返回 false
|
||
* @throws {Error} 如果 ctx 中包含 appId 但不匹配,则抛出 403 错误
|
||
*/
|
||
const checkAppId = (ctx: any, appId: string) => {
|
||
const _appId = ctx?.app?.appId;
|
||
if (_appId) {
|
||
if (_appId !== appId) {
|
||
ctx.throw(403, 'Invalid App ID');
|
||
}
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
if (!app.hasRoute('auth')) {
|
||
app.route({
|
||
id: 'auth',
|
||
path: 'auth',
|
||
}).define(async (ctx) => {
|
||
// ctx.body = 'Auth Route';
|
||
if (checkAppId(ctx, app.appId)) {
|
||
return;
|
||
}
|
||
}).addTo(app);
|
||
|
||
app.route({
|
||
id: 'admin-auth',
|
||
path: 'admin-auth',
|
||
middleware: ['auth'],
|
||
}).define(async (ctx) => {
|
||
// ctx.body = 'Admin Auth Route';
|
||
if (checkAppId(ctx, app.appId)) {
|
||
return;
|
||
}
|
||
}).addTo(app);
|
||
} |