2022-09-16 06:25:43 -05:00
|
|
|
import { ConnectorMetadata } from '@logto/connector-kit';
|
2022-06-16 21:36:07 -05:00
|
|
|
import { SignInMode } from '@logto/schemas';
|
2022-06-27 00:22:17 -05:00
|
|
|
import {
|
|
|
|
adminConsoleApplicationId,
|
2022-07-03 02:50:29 -05:00
|
|
|
adminConsoleSignInExperience,
|
2022-06-27 00:22:17 -05:00
|
|
|
demoAppApplicationId,
|
|
|
|
} from '@logto/schemas/lib/seeds';
|
2022-06-01 03:56:23 -05:00
|
|
|
import etag from 'etag';
|
2022-06-27 00:22:17 -05:00
|
|
|
import i18next from 'i18next';
|
2022-06-20 19:56:01 -05:00
|
|
|
import { Provider, errors } from 'oidc-provider';
|
2022-05-18 22:24:42 -05:00
|
|
|
|
2022-08-26 03:25:08 -05:00
|
|
|
import { getLogtoConnectors } from '@/connectors';
|
2022-05-18 22:24:42 -05:00
|
|
|
import { findDefaultSignInExperience } from '@/queries/sign-in-experience';
|
2022-06-16 21:36:07 -05:00
|
|
|
import { hasActiveUsers } from '@/queries/user';
|
2022-05-18 22:24:42 -05:00
|
|
|
|
|
|
|
import { AnonymousRouter } from './types';
|
|
|
|
|
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-07-03 02:50:29 -05:00
|
|
|
const interaction = await provider
|
|
|
|
.interactionDetails(ctx.req, ctx.res)
|
|
|
|
.catch((error: unknown) => {
|
2022-06-20 19:56:01 -05:00
|
|
|
// Should not block if interaction is not found
|
|
|
|
if (error instanceof errors.SessionNotFound) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
2022-07-03 02:50:29 -05:00
|
|
|
});
|
|
|
|
|
2022-10-20 05:40:45 -05:00
|
|
|
const [signInExperience, logtoConnectors] = await Promise.all([
|
|
|
|
findDefaultSignInExperience(),
|
|
|
|
getLogtoConnectors(),
|
|
|
|
]);
|
|
|
|
|
2022-07-03 02:50:29 -05:00
|
|
|
// Hard code AdminConsole sign-in methods settings.
|
|
|
|
if (interaction?.params.client_id === adminConsoleApplicationId) {
|
|
|
|
ctx.body = {
|
|
|
|
...adminConsoleSignInExperience,
|
|
|
|
branding: {
|
|
|
|
...adminConsoleSignInExperience.branding,
|
|
|
|
slogan: i18next.t('admin_console.welcome.title'),
|
|
|
|
},
|
2022-10-20 05:40:45 -05:00
|
|
|
languageInfo: signInExperience.languageInfo,
|
2022-07-03 02:50:29 -05:00
|
|
|
signInMode: (await hasActiveUsers()) ? SignInMode.SignIn : SignInMode.Register,
|
|
|
|
socialConnectors: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom Applications
|
2022-06-16 21:36:07 -05:00
|
|
|
|
2022-06-01 03:56:23 -05:00
|
|
|
const socialConnectors = signInExperience.socialSignInConnectorTargets.reduce<
|
|
|
|
Array<ConnectorMetadata & { id: string }>
|
|
|
|
>((previous, connectorTarget) => {
|
2022-08-26 03:25:08 -05:00
|
|
|
const connectors = logtoConnectors.filter(
|
|
|
|
({ metadata: { target }, dbEntry: { enabled } }) => target === connectorTarget && enabled
|
2022-06-01 03:56:23 -05:00
|
|
|
);
|
2022-05-18 22:24:42 -05:00
|
|
|
|
2022-06-01 03:56:23 -05:00
|
|
|
return [
|
|
|
|
...previous,
|
2022-08-26 03:25:08 -05:00
|
|
|
...connectors.map(({ metadata, dbEntry: { id } }) => ({ ...metadata, id })),
|
2022-06-01 03:56:23 -05:00
|
|
|
];
|
|
|
|
}, []);
|
2022-06-16 21:36:07 -05:00
|
|
|
|
2022-07-14 08:36:32 -05:00
|
|
|
// Insert Demo App Notification
|
|
|
|
if (interaction?.params.client_id === demoAppApplicationId) {
|
|
|
|
const {
|
2022-10-10 21:30:17 -05:00
|
|
|
languageInfo: { autoDetect, fallbackLanguage },
|
2022-07-14 08:36:32 -05:00
|
|
|
} = signInExperience;
|
2022-06-27 00:22:17 -05:00
|
|
|
|
2022-07-14 08:36:32 -05:00
|
|
|
ctx.body = {
|
|
|
|
...signInExperience,
|
|
|
|
socialConnectors,
|
|
|
|
notification: i18next.t(
|
|
|
|
'demo_app.notification',
|
2022-10-10 21:30:17 -05:00
|
|
|
autoDetect ? undefined : { lng: fallbackLanguage }
|
2022-07-14 08:36:32 -05:00
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = { ...signInExperience, socialConnectors };
|
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
|
|
|
}
|