mirror of
https://github.com/outline/outline.git
synced 2026-11-26 07:33:19 +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>
118 lines
3.8 KiB
TypeScript
118 lines
3.8 KiB
TypeScript
import { toError } from "@shared/utils/error";
|
|
import auth from "@server/middlewares/authentication";
|
|
import Router from "@koa/router";
|
|
import * as T from "./schema";
|
|
import apexAuthRedirect from "@server/middlewares/apexAuthRedirect";
|
|
import type { Context } from "@server/types";
|
|
import validate from "@server/middlewares/validate";
|
|
import {
|
|
FigmaOAuthNonceCookie,
|
|
FigmaUtils,
|
|
} from "plugins/figma/shared/FigmaUtils";
|
|
import { transaction } from "@server/middlewares/transaction";
|
|
import Logger from "@server/logging/Logger";
|
|
import { IntegrationService, IntegrationType } from "@shared/types";
|
|
import { ValidationError } from "@server/errors";
|
|
import { Integration, IntegrationAuthentication } from "@server/models";
|
|
import { afterCommit } from "@server/storage/transaction";
|
|
import { verifyOAuthStateNonce } from "@server/utils/oauth";
|
|
import { addSeconds } from "date-fns";
|
|
import { Figma } from "../figma";
|
|
import UploadIntegrationLogoTask from "@server/queues/tasks/UploadIntegrationLogoTask";
|
|
|
|
const router = new Router();
|
|
|
|
router.get(
|
|
"figma.callback",
|
|
auth({ optional: true }),
|
|
validate(T.FigmaCallbackSchema),
|
|
apexAuthRedirect<T.FigmaCallbackReq>({
|
|
getTeamId: (ctx) => FigmaUtils.parseState(ctx.input.query.state)?.teamId,
|
|
getRedirectPath: (ctx, team) =>
|
|
FigmaUtils.callbackUrl({
|
|
baseUrl: team.url,
|
|
params: ctx.request.querystring,
|
|
}),
|
|
getErrorPath: () => FigmaUtils.errorUrl("unauthenticated"),
|
|
}),
|
|
transaction(),
|
|
async (ctx: Context<T.FigmaCallbackReq>) => {
|
|
const { code, error, state } = ctx.input.query;
|
|
|
|
// Check error after any sub-domain redirection. Otherwise, the user will be redirected to the root domain.
|
|
if (error) {
|
|
ctx.redirect(FigmaUtils.errorUrl(error));
|
|
return;
|
|
}
|
|
|
|
const parsedState = FigmaUtils.parseState(state);
|
|
if (!parsedState) {
|
|
throw ValidationError("Invalid state");
|
|
}
|
|
|
|
verifyOAuthStateNonce(ctx, FigmaOAuthNonceCookie, parsedState.nonce);
|
|
|
|
const { user } = ctx.auth;
|
|
const { transaction } = ctx;
|
|
|
|
try {
|
|
// validation middleware ensures that code is non-null at this point.
|
|
const oauth = await Figma.oauthAccess(code!);
|
|
const figmaAccount = await Figma.getInstalledAccount(oauth.access_token);
|
|
|
|
const authentication = await IntegrationAuthentication.create(
|
|
{
|
|
service: IntegrationService.Figma,
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
token: oauth.access_token,
|
|
refreshToken: oauth.refresh_token,
|
|
expiresAt: addSeconds(Date.now(), oauth.expires_in),
|
|
scopes: FigmaUtils.oauthScopes,
|
|
},
|
|
{ transaction }
|
|
);
|
|
const integration = await Integration.create<
|
|
Integration<IntegrationType.LinkedAccount>
|
|
>(
|
|
{
|
|
service: IntegrationService.Figma,
|
|
type: IntegrationType.LinkedAccount,
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
authenticationId: authentication.id,
|
|
settings: {
|
|
figma: {
|
|
account: {
|
|
id: figmaAccount.id,
|
|
name: figmaAccount.handle,
|
|
email: figmaAccount.email,
|
|
avatarUrl: figmaAccount.img_url,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{ transaction }
|
|
);
|
|
|
|
await afterCommit(transaction, () =>
|
|
new UploadIntegrationLogoTask().schedule({
|
|
integrationId: integration.id,
|
|
logoUrl: figmaAccount.img_url,
|
|
settingsPath: ["figma", "account", "avatarUrl"],
|
|
})
|
|
);
|
|
|
|
ctx.redirect(FigmaUtils.successUrl());
|
|
} catch (err) {
|
|
Logger.error(
|
|
"Encountered error during Figma OAuth callback",
|
|
toError(err)
|
|
);
|
|
ctx.redirect(FigmaUtils.errorUrl("unknown"));
|
|
}
|
|
}
|
|
);
|
|
|
|
export default router;
|