mirror of
https://github.com/outline/outline.git
synced 2026-11-25 07:04:37 +03:00
* 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>
146 lines
4.4 KiB
TypeScript
146 lines
4.4 KiB
TypeScript
import Router from "@koa/router";
|
|
import { find } from "es-toolkit/compat";
|
|
import { IntegrationService, IntegrationType } from "@shared/types";
|
|
import { createContext } from "@server/context";
|
|
import { ValidationError } from "@server/errors";
|
|
import apexAuthRedirect from "@server/middlewares/apexAuthRedirect";
|
|
import auth from "@server/middlewares/authentication";
|
|
import { transaction } from "@server/middlewares/transaction";
|
|
import validate from "@server/middlewares/validate";
|
|
import validateWebhook from "@server/middlewares/validateWebhook";
|
|
import { IntegrationAuthentication, Integration } from "@server/models";
|
|
import { authorize } from "@server/policies";
|
|
import type { Context } from "@server/types";
|
|
import { verifyOAuthStateNonce } from "@server/utils/oauth";
|
|
import { RequestHelper } from "@server/utils/RequestHelper";
|
|
import { GitHubOAuthNonceCookie, GitHubUtils } from "../../shared/GitHubUtils";
|
|
import env from "../env";
|
|
import { GitHub, InstallationAccess } from "../github";
|
|
import GitHubWebhookTask from "../tasks/GitHubWebhookTask";
|
|
import * as T from "./schema";
|
|
|
|
const router = new Router();
|
|
|
|
router.get(
|
|
"github.callback",
|
|
auth({ optional: true }),
|
|
validate(T.GitHubCallbackSchema),
|
|
apexAuthRedirect<T.GitHubCallbackReq>({
|
|
getTeamId: (ctx) => GitHubUtils.parseState(ctx.input.query.state)?.teamId,
|
|
getRedirectPath: (ctx, team) =>
|
|
GitHubUtils.callbackUrl({
|
|
baseUrl: team.url,
|
|
params: ctx.request.querystring,
|
|
}),
|
|
getErrorPath: () => GitHubUtils.errorUrl("unauthenticated"),
|
|
}),
|
|
transaction(),
|
|
async (ctx: Context<T.GitHubCallbackReq>) => {
|
|
const {
|
|
code,
|
|
state,
|
|
error,
|
|
installation_id: installationId,
|
|
setup_action: setupAction,
|
|
} = ctx.input.query;
|
|
const { user } = ctx.auth;
|
|
const { transaction } = ctx;
|
|
|
|
if (error) {
|
|
ctx.redirect(GitHubUtils.errorUrl(error));
|
|
return;
|
|
}
|
|
|
|
if (setupAction === T.SetupAction.request) {
|
|
ctx.redirect(GitHubUtils.installRequestUrl());
|
|
return;
|
|
}
|
|
|
|
const parsedState = GitHubUtils.parseState(state);
|
|
if (!parsedState) {
|
|
throw ValidationError("Invalid state");
|
|
}
|
|
|
|
verifyOAuthStateNonce(ctx, GitHubOAuthNonceCookie, parsedState.nonce);
|
|
authorize(user, "createIntegration", user.team);
|
|
|
|
const client = await GitHub.authenticateAsUser(code!, state);
|
|
const installationsByUser = await client.requestAppInstallations();
|
|
const installation = find(
|
|
installationsByUser,
|
|
(i) => i.id === installationId
|
|
);
|
|
|
|
if (!installation) {
|
|
return ctx.redirect(GitHubUtils.errorUrl("unauthenticated"));
|
|
}
|
|
|
|
const access = await GitHub.canManageInstallation(client, installation);
|
|
if (access !== InstallationAccess.Allowed) {
|
|
return ctx.redirect(GitHubUtils.errorUrl(access));
|
|
}
|
|
|
|
const scopes = Object.entries(installation.permissions).map(
|
|
([name, permission]) => `${name}:${String(permission)}`
|
|
);
|
|
|
|
const authentication = await IntegrationAuthentication.create(
|
|
{
|
|
service: IntegrationService.GitHub,
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
scopes,
|
|
},
|
|
{ transaction }
|
|
);
|
|
await Integration.createWithCtx(createContext({ user, transaction }), {
|
|
service: IntegrationService.GitHub,
|
|
type: IntegrationType.Embed,
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
authenticationId: authentication.id,
|
|
settings: {
|
|
github: {
|
|
installation: {
|
|
id: installationId!,
|
|
account: {
|
|
id: installation.account?.id,
|
|
name: installation.account?.login,
|
|
avatarUrl: installation.account?.avatar_url,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
ctx.redirect(GitHubUtils.url);
|
|
}
|
|
);
|
|
|
|
router.post(
|
|
"github.webhooks",
|
|
validateWebhook({
|
|
secretKey: env.GITHUB_WEBHOOK_SECRET!,
|
|
getSignatureFromHeader: (ctx) => {
|
|
const { headers } = ctx.request;
|
|
const signatureHeader = headers["x-hub-signature-256"];
|
|
const signature = Array.isArray(signatureHeader)
|
|
? signatureHeader[0]
|
|
: signatureHeader;
|
|
return signature?.split("=")[1];
|
|
},
|
|
}),
|
|
async (ctx: Context) => {
|
|
const { headers } = ctx.request;
|
|
const body = RequestHelper.getBodyObject(ctx.request);
|
|
|
|
await new GitHubWebhookTask().schedule({
|
|
payload: body,
|
|
headers,
|
|
});
|
|
|
|
ctx.status = 202;
|
|
}
|
|
);
|
|
|
|
export default router;
|