Files
outline/server/routes/api/authenticationProviders/authenticationProviders.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

215 lines
6.3 KiB
TypeScript

import Router from "@koa/router";
import { UserRole } from "@shared/types";
import env from "@server/env";
import { InvalidRequestError } from "@server/errors";
import auth from "@server/middlewares/authentication";
import { transaction } from "@server/middlewares/transaction";
import validate from "@server/middlewares/validate";
import { AuthenticationProvider } from "@server/models";
import AuthenticationHelper from "@server/models/helpers/AuthenticationHelper";
import { authorize } from "@server/policies";
import { PluginManager } from "@server/utils/PluginManager";
import {
presentAuthenticationProvider,
presentPolicies,
} from "@server/presenters";
import type { Context } from "@server/types";
import * as T from "./schema";
const router = new Router();
router.post(
"authenticationProviders.info",
auth({ role: UserRole.Admin }),
validate(T.AuthenticationProvidersInfoSchema),
async (ctx: Context<T.AuthenticationProvidersInfoReq>) => {
const { id } = ctx.input.body;
const { user } = ctx.auth;
const authenticationProvider = await AuthenticationProvider.findByPk(id);
authorize(user, "read", authenticationProvider);
ctx.body = {
data: presentAuthenticationProvider(authenticationProvider),
policies: presentPolicies(user, [authenticationProvider]),
};
}
);
router.post(
"authenticationProviders.update",
auth({ role: UserRole.Admin }),
validate(T.AuthenticationProvidersUpdateSchema),
transaction(),
async (ctx: Context<T.AuthenticationProvidersUpdateReq>) => {
const { transaction } = ctx;
const { id, isEnabled, settings } = ctx.input.body;
const { user } = ctx.auth;
const authenticationProvider = await AuthenticationProvider.findByPk(id, {
transaction,
lock: transaction.LOCK.NO_KEY_UPDATE,
});
authorize(user, "update", authenticationProvider);
if (
settings?.groupSyncEnabled === true &&
!authenticationProvider.settings?.groupSyncEnabled
) {
authorize(user, "externalGroupSync", user.team);
const groupSyncProvider = PluginManager.getGroupSyncProvider(
authenticationProvider.name
);
if (!groupSyncProvider) {
throw InvalidRequestError(
"Group sync is not available for this provider"
);
}
if (groupSyncProvider.startGroupSync) {
throw InvalidRequestError("Set up group sync with admin consent");
}
}
if (isEnabled !== undefined) {
const enabled = !!isEnabled;
if (enabled) {
await authenticationProvider.enable(ctx);
} else {
await authenticationProvider.disable(ctx);
}
}
if (settings !== undefined) {
await authenticationProvider.updateWithCtx(ctx, {
settings: {
...authenticationProvider.settings,
...settings,
},
});
}
ctx.body = {
data: presentAuthenticationProvider(authenticationProvider),
policies: presentPolicies(user, [authenticationProvider]),
};
}
);
router.post(
"authenticationProviders.startGroupSync",
auth({ role: UserRole.Admin }),
validate(T.AuthenticationProvidersStartGroupSyncSchema),
async (ctx: Context<T.AuthenticationProvidersStartGroupSyncReq>) => {
const { user } = ctx.auth;
const authenticationProvider = await AuthenticationProvider.findByPk(
ctx.input.body.id
);
authorize(user, "update", authenticationProvider);
authorize(user, "externalGroupSync", user.team);
const groupSyncProvider = PluginManager.getGroupSyncProvider(
authenticationProvider.name
);
if (!authenticationProvider.enabled || !authenticationProvider.providerId) {
throw InvalidRequestError(
"Authentication provider must be connected first"
);
}
if (!groupSyncProvider?.startGroupSync) {
throw InvalidRequestError(
"Group sync setup is not available for this provider"
);
}
ctx.body = {
data: {
url: await groupSyncProvider.startGroupSync(
authenticationProvider,
user
),
},
};
}
);
router.post(
"authenticationProviders.delete",
auth({ role: UserRole.Admin }),
validate(T.AuthenticationProvidersDeleteSchema),
transaction(),
async (ctx: Context<T.AuthenticationProvidersDeleteReq>) => {
const { transaction } = ctx;
const { id } = ctx.input.body;
const { user } = ctx.auth;
const authenticationProvider = await AuthenticationProvider.findByPk(id, {
transaction,
lock: transaction.LOCK.UPDATE,
});
authorize(user, "delete", authenticationProvider);
if (authenticationProvider.enabled) {
await authenticationProvider.disable(ctx);
}
// On self-hosted, providers are typically registered via env vars and
// would re-appear on the login screen if the row was destroyed, so we
// keep the row with enabled=false. On cloud, destroy the row so the
// admin can reconnect with a different workspace.
if (env.isCloudHosted) {
await authenticationProvider.destroy({ transaction });
}
ctx.body = {
success: true,
};
}
);
router.post(
"authenticationProviders.list",
auth({ role: UserRole.Admin }),
async (ctx: Context) => {
const { user } = ctx.auth;
authorize(user, "read", user.team);
const teamAuthenticationProviders = (await user.team.$get(
"authenticationProviders"
)) as AuthenticationProvider[];
const data = AuthenticationHelper.providers
.filter((p) => p.value.id !== "email" && p.value.id !== "passkeys")
.map((p) => {
const row = teamAuthenticationProviders.find(
(t) => t.name === p.value.id
);
const groupSyncProvider = PluginManager.getGroupSyncProvider(
p.value.id
);
return {
id: p.value.id,
name: p.value.id,
displayName: p.name,
isEnabled: false,
isConnected: false,
groupSyncSupported: !!groupSyncProvider,
groupSyncUsesClaim: groupSyncProvider?.useGroupClaim ?? false,
groupSyncRequiresSetup: !!groupSyncProvider?.startGroupSync,
...(row ? presentAuthenticationProvider(row) : {}),
};
})
.sort((a, b) => (a.isEnabled === b.isEnabled ? 0 : a.isEnabled ? -1 : 1));
ctx.body = {
data,
};
}
);
export default router;