mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +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>
125 lines
4.0 KiB
TypeScript
125 lines
4.0 KiB
TypeScript
import Router from "@koa/router";
|
|
import { toError } from "@shared/utils/error";
|
|
import { IntegrationService, IntegrationType } from "@shared/types";
|
|
import { ValidationError } from "@server/errors";
|
|
import Logger from "@server/logging/Logger";
|
|
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 { IntegrationAuthentication, Integration } from "@server/models";
|
|
import { authorize } from "@server/policies";
|
|
import { afterCommit } from "@server/storage/transaction";
|
|
import type { Context } from "@server/types";
|
|
import { verifyOAuthStateNonce } from "@server/utils/oauth";
|
|
import { Linear } from "../linear";
|
|
import UploadIntegrationLogoTask from "@server/queues/tasks/UploadIntegrationLogoTask";
|
|
import * as T from "./schema";
|
|
import {
|
|
LinearOAuthNonceCookie,
|
|
LinearUtils,
|
|
} from "plugins/linear/shared/LinearUtils";
|
|
import { addSeconds } from "date-fns";
|
|
|
|
const router = new Router();
|
|
|
|
router.get(
|
|
"linear.callback",
|
|
auth({
|
|
optional: true,
|
|
}),
|
|
validate(T.LinearCallbackSchema),
|
|
apexAuthRedirect<T.LinearCallbackReq>({
|
|
getTeamId: (ctx) => LinearUtils.parseState(ctx.input.query.state)?.teamId,
|
|
getRedirectPath: (ctx, team) =>
|
|
LinearUtils.callbackUrl({
|
|
baseUrl: team.url,
|
|
params: ctx.request.querystring,
|
|
}),
|
|
getErrorPath: () => LinearUtils.errorUrl("unauthenticated"),
|
|
}),
|
|
transaction(),
|
|
async (ctx: Context<T.LinearCallbackReq>) => {
|
|
const { code, error, state } = ctx.input.query;
|
|
const { user } = ctx.auth;
|
|
const { transaction } = ctx;
|
|
|
|
// Check error after any sub-domain redirection. Otherwise, the user will be redirected to the root domain.
|
|
if (error) {
|
|
ctx.redirect(LinearUtils.errorUrl(error));
|
|
return;
|
|
}
|
|
|
|
const parsedState = LinearUtils.parseState(state);
|
|
if (!parsedState) {
|
|
throw ValidationError("Invalid state");
|
|
}
|
|
|
|
verifyOAuthStateNonce(ctx, LinearOAuthNonceCookie, parsedState.nonce);
|
|
authorize(user, "createIntegration", user.team);
|
|
|
|
try {
|
|
// validation middleware ensures that code is non-null at this point.
|
|
const oauth = await Linear.oauthAccess(code!);
|
|
const workspace = await Linear.getInstalledWorkspace(oauth.access_token);
|
|
|
|
const authentication = await IntegrationAuthentication.create(
|
|
{
|
|
service: IntegrationService.Linear,
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
token: oauth.access_token,
|
|
refreshToken: oauth.refresh_token,
|
|
expiresAt: oauth.expires_in
|
|
? addSeconds(Date.now(), oauth.expires_in)
|
|
: undefined,
|
|
scopes: oauth.scope.split(" "),
|
|
},
|
|
{ transaction }
|
|
);
|
|
const integration = await Integration.create<
|
|
Integration<IntegrationType.Embed>
|
|
>(
|
|
{
|
|
service: IntegrationService.Linear,
|
|
type: IntegrationType.Embed,
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
authenticationId: authentication.id,
|
|
settings: {
|
|
linear: {
|
|
workspace: {
|
|
id: workspace.id,
|
|
name: workspace.name,
|
|
key: workspace.urlKey,
|
|
logoUrl: workspace.logoUrl ?? undefined,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{ transaction }
|
|
);
|
|
|
|
await afterCommit(transaction, async () => {
|
|
if (workspace.logoUrl) {
|
|
await new UploadIntegrationLogoTask().schedule({
|
|
integrationId: integration.id,
|
|
logoUrl: workspace.logoUrl,
|
|
settingsPath: ["linear", "workspace", "logoUrl"],
|
|
});
|
|
}
|
|
});
|
|
|
|
ctx.redirect(LinearUtils.successUrl());
|
|
} catch (err) {
|
|
Logger.error(
|
|
"Encountered error during Linear OAuth callback",
|
|
toError(err)
|
|
);
|
|
ctx.redirect(LinearUtils.errorUrl("unknown"));
|
|
}
|
|
}
|
|
);
|
|
|
|
export default router;
|