mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
Merge pull request #3361 from logto-io/gao-fix-api-issues
refactor(core): fix api issues
This commit is contained in:
commit
e191beb29f
6 changed files with 63 additions and 55 deletions
|
@ -19,6 +19,12 @@ const logListening = (type: 'core' | 'admin' = 'core') => {
|
|||
|
||||
export default async function initApp(app: Koa): Promise<void> {
|
||||
app.use(async (ctx, next) => {
|
||||
if (EnvSet.values.isDomainBasedMultiTenancy && ctx.URL.pathname === '/status') {
|
||||
ctx.status = 204;
|
||||
|
||||
return next();
|
||||
}
|
||||
|
||||
const tenantId = getTenantId(ctx.URL);
|
||||
|
||||
if (!tenantId) {
|
||||
|
|
|
@ -17,7 +17,7 @@ export default function koaBodyEtag<StateT, ContextT, ResponseBodyT>(
|
|||
await next();
|
||||
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
if (methods.includes(ctx.method as Uppercase<Method>)) {
|
||||
if (methods.includes(ctx.method as Uppercase<Method>) && ctx.body) {
|
||||
ctx.response.etag = etag(JSON.stringify(ctx.body));
|
||||
|
||||
if (ctx.fresh) {
|
||||
|
|
|
@ -3,7 +3,9 @@ import type { MiddlewareType } from 'koa';
|
|||
import type { IRouterParamContext } from 'koa-router';
|
||||
import type Provider from 'oidc-provider';
|
||||
|
||||
import { EnvSet } from '#src/env-set/index.js';
|
||||
import { EnvSet, getTenantEndpoint } from '#src/env-set/index.js';
|
||||
import RequestError from '#src/errors/RequestError/index.js';
|
||||
import { getTenantId } from '#src/utils/tenant.js';
|
||||
|
||||
// Need To Align With UI
|
||||
export const sessionNotFoundPath = '/unknown-session';
|
||||
|
@ -20,18 +22,24 @@ export default function koaSpaSessionGuard<
|
|||
ContextT extends IRouterParamContext,
|
||||
ResponseBodyT
|
||||
>(provider: Provider): MiddlewareType<StateT, ContextT, ResponseBodyT> {
|
||||
const { endpoint } = EnvSet.values;
|
||||
|
||||
return async (ctx, next) => {
|
||||
const requestPath = ctx.request.path;
|
||||
const isPreview = ctx.request.URL.searchParams.get('preview');
|
||||
const isPreview = ctx.URL.searchParams.get('preview');
|
||||
const isSessionRequiredPath = guardedPath.some((path) => requestPath.startsWith(path));
|
||||
|
||||
if (isSessionRequiredPath && !isPreview) {
|
||||
try {
|
||||
await provider.interactionDetails(ctx.req, ctx.res);
|
||||
} catch {
|
||||
ctx.redirect(appendPath(endpoint, sessionNotFoundPath).href);
|
||||
const tenantId = getTenantId(ctx.URL);
|
||||
|
||||
if (!tenantId) {
|
||||
throw new RequestError({ code: 'session.not_found', status: 404 });
|
||||
}
|
||||
|
||||
ctx.redirect(
|
||||
appendPath(getTenantEndpoint(tenantId, EnvSet.values), sessionNotFoundPath).href
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import Koa from 'koa';
|
|||
import Router from 'koa-router';
|
||||
|
||||
import { EnvSet } from '#src/env-set/index.js';
|
||||
import koaBodyEtag from '#src/middleware/koa-body-etag.js';
|
||||
import koaCors from '#src/middleware/koa-cors.js';
|
||||
import type TenantContext from '#src/tenants/TenantContext.js';
|
||||
|
||||
|
@ -68,6 +69,7 @@ export default function initApis(tenant: TenantContext): Koa {
|
|||
|
||||
const { adminUrlSet, cloudUrlSet } = EnvSet.values;
|
||||
apisApp.use(koaCors(adminUrlSet, cloudUrlSet));
|
||||
apisApp.use(koaBodyEtag());
|
||||
|
||||
for (const router of createRouters(tenant)) {
|
||||
apisApp.use(router.routes()).use(router.allowedMethods());
|
||||
|
|
|
@ -4,7 +4,6 @@ import { adminTenantId } from '@logto/schemas';
|
|||
|
||||
import { EnvSet, getTenantEndpoint } from '#src/env-set/index.js';
|
||||
import RequestError from '#src/errors/RequestError/index.js';
|
||||
import koaBodyEtag from '#src/middleware/koa-body-etag.js';
|
||||
|
||||
import type { AnonymousRouter, RouterInitArgs } from './types.js';
|
||||
|
||||
|
@ -17,57 +16,49 @@ export default function wellKnownRoutes<T extends AnonymousRouter>(
|
|||
} = libraries;
|
||||
|
||||
if (id === adminTenantId) {
|
||||
router.get(
|
||||
'/.well-known/endpoints/:tenantId',
|
||||
async (ctx, next) => {
|
||||
if (!ctx.params.tenantId) {
|
||||
throw new RequestError('request.invalid_input');
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
user: getTenantEndpoint(ctx.params.tenantId, EnvSet.values),
|
||||
};
|
||||
|
||||
return next();
|
||||
},
|
||||
koaBodyEtag()
|
||||
);
|
||||
}
|
||||
|
||||
router.get(
|
||||
'/.well-known/sign-in-exp',
|
||||
async (ctx, next) => {
|
||||
const [signInExperience, logtoConnectors] = await Promise.all([
|
||||
getSignInExperience(),
|
||||
getLogtoConnectors(),
|
||||
]);
|
||||
|
||||
const forgotPassword = {
|
||||
phone: logtoConnectors.some(({ type }) => type === ConnectorType.Sms),
|
||||
email: logtoConnectors.some(({ type }) => type === ConnectorType.Email),
|
||||
};
|
||||
|
||||
const socialConnectors = signInExperience.socialSignInConnectorTargets.reduce<
|
||||
Array<ConnectorMetadata & { id: string }>
|
||||
>((previous, connectorTarget) => {
|
||||
const connectors = logtoConnectors.filter(
|
||||
({ metadata: { target } }) => target === connectorTarget
|
||||
);
|
||||
|
||||
return [
|
||||
...previous,
|
||||
...connectors.map(({ metadata, dbEntry: { id } }) => ({ ...metadata, id })),
|
||||
];
|
||||
}, []);
|
||||
router.get('/.well-known/endpoints/:tenantId', async (ctx, next) => {
|
||||
if (!ctx.params.tenantId) {
|
||||
throw new RequestError('request.invalid_input');
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
...signInExperience,
|
||||
socialConnectors,
|
||||
forgotPassword,
|
||||
user: getTenantEndpoint(ctx.params.tenantId, EnvSet.values),
|
||||
};
|
||||
|
||||
return next();
|
||||
},
|
||||
koaBodyEtag()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
router.get('/.well-known/sign-in-exp', async (ctx, next) => {
|
||||
const [signInExperience, logtoConnectors] = await Promise.all([
|
||||
getSignInExperience(),
|
||||
getLogtoConnectors(),
|
||||
]);
|
||||
|
||||
const forgotPassword = {
|
||||
phone: logtoConnectors.some(({ type }) => type === ConnectorType.Sms),
|
||||
email: logtoConnectors.some(({ type }) => type === ConnectorType.Email),
|
||||
};
|
||||
|
||||
const socialConnectors = signInExperience.socialSignInConnectorTargets.reduce<
|
||||
Array<ConnectorMetadata & { id: string }>
|
||||
>((previous, connectorTarget) => {
|
||||
const connectors = logtoConnectors.filter(
|
||||
({ metadata: { target } }) => target === connectorTarget
|
||||
);
|
||||
|
||||
return [
|
||||
...previous,
|
||||
...connectors.map(({ metadata, dbEntry: { id } }) => ({ ...metadata, id })),
|
||||
];
|
||||
}, []);
|
||||
|
||||
ctx.body = {
|
||||
...signInExperience,
|
||||
socialConnectors,
|
||||
forgotPassword,
|
||||
};
|
||||
|
||||
return next();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -94,6 +94,7 @@ export const createContextWithRouteParameters = (
|
|||
|
||||
return {
|
||||
...ctx,
|
||||
URL: ctx.URL,
|
||||
params: {},
|
||||
router: new Router(),
|
||||
_matchedRoute: undefined,
|
||||
|
|
Loading…
Add table
Reference in a new issue