2022-10-21 00:14:17 -05:00
|
|
|
import type { ConnectorMetadata } from '@logto/connector-kit';
|
2022-11-02 21:31:57 -05:00
|
|
|
import { ConnectorType } from '@logto/connector-kit';
|
2022-12-19 11:01:40 -05:00
|
|
|
import { adminConsoleApplicationId } from '@logto/schemas';
|
2022-06-01 03:56:23 -05:00
|
|
|
import etag from 'etag';
|
2022-10-21 00:14:17 -05:00
|
|
|
import type { Provider } from 'oidc-provider';
|
2022-05-18 22:24:42 -05:00
|
|
|
|
2022-11-21 03:38:24 -05:00
|
|
|
import { getLogtoConnectors } from '#src/connectors/index.js';
|
2022-12-14 00:23:59 -05:00
|
|
|
import { getApplicationIdFromInteraction } from '#src/libraries/session.js';
|
|
|
|
import { getSignInExperienceForApplication } from '#src/libraries/sign-in-experience/index.js';
|
2022-05-18 22:24:42 -05:00
|
|
|
|
2022-11-21 03:38:24 -05:00
|
|
|
import type { AnonymousRouter } from './types.js';
|
2022-05-18 22:24:42 -05:00
|
|
|
|
2022-07-01 04:14:38 -05:00
|
|
|
export default function wellKnownRoutes<T extends AnonymousRouter>(router: T, provider: Provider) {
|
2022-06-01 03:56:23 -05:00
|
|
|
router.get(
|
2022-07-01 04:14:38 -05:00
|
|
|
'/.well-known/sign-in-exp',
|
2022-06-01 03:56:23 -05:00
|
|
|
async (ctx, next) => {
|
2022-11-14 04:57:47 -05:00
|
|
|
const applicationId = await getApplicationIdFromInteraction(ctx, provider);
|
2022-07-03 02:50:29 -05:00
|
|
|
|
2022-10-20 05:40:45 -05:00
|
|
|
const [signInExperience, logtoConnectors] = await Promise.all([
|
2022-11-14 04:57:47 -05:00
|
|
|
getSignInExperienceForApplication(applicationId),
|
2022-10-20 05:40:45 -05:00
|
|
|
getLogtoConnectors(),
|
|
|
|
]);
|
|
|
|
|
2022-11-04 04:31:12 -05:00
|
|
|
const forgotPassword = {
|
2022-11-28 00:51:46 -05:00
|
|
|
sms: logtoConnectors.some(({ type }) => type === ConnectorType.Sms),
|
|
|
|
email: logtoConnectors.some(({ type }) => type === ConnectorType.Email),
|
2022-11-04 04:31:12 -05:00
|
|
|
};
|
|
|
|
|
2022-11-14 04:57:47 -05:00
|
|
|
const socialConnectors =
|
|
|
|
applicationId === adminConsoleApplicationId
|
|
|
|
? []
|
|
|
|
: signInExperience.socialSignInConnectorTargets.reduce<
|
|
|
|
Array<ConnectorMetadata & { id: string }>
|
|
|
|
>((previous, connectorTarget) => {
|
|
|
|
const connectors = logtoConnectors.filter(
|
2022-11-28 00:51:46 -05:00
|
|
|
({ metadata: { target } }) => target === connectorTarget
|
2022-11-14 04:57:47 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
return [
|
|
|
|
...previous,
|
|
|
|
...connectors.map(({ metadata, dbEntry: { id } }) => ({ ...metadata, id })),
|
|
|
|
];
|
|
|
|
}, []);
|
2022-07-14 08:36:32 -05:00
|
|
|
|
2022-11-02 21:31:57 -05:00
|
|
|
ctx.body = {
|
|
|
|
...signInExperience,
|
|
|
|
socialConnectors,
|
2022-11-04 04:31:12 -05:00
|
|
|
forgotPassword,
|
2022-11-02 21:31:57 -05:00
|
|
|
};
|
2022-05-18 22:24:42 -05:00
|
|
|
|
2022-06-01 03:56:23 -05:00
|
|
|
return next();
|
|
|
|
},
|
|
|
|
async (ctx, next) => {
|
|
|
|
await next();
|
|
|
|
|
|
|
|
ctx.response.etag = etag(JSON.stringify(ctx.body));
|
|
|
|
|
|
|
|
if (ctx.fresh) {
|
|
|
|
ctx.status = 304;
|
|
|
|
ctx.body = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-05-18 22:24:42 -05:00
|
|
|
}
|