mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +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>
241 lines
8.5 KiB
TypeScript
241 lines
8.5 KiB
TypeScript
import passport from "@outlinewiki/koa-passport";
|
||
import { isURL } from "class-validator";
|
||
import type {
|
||
RESTGetAPICurrentUserGuildsResult,
|
||
RESTGetAPICurrentUserResult,
|
||
RESTGetCurrentUserGuildMemberResult,
|
||
} from "discord-api-types/v10";
|
||
import type { Request } from "koa";
|
||
import Router from "@koa/router";
|
||
|
||
import { Strategy } from "passport-oauth2";
|
||
import { toError } from "@shared/utils/error";
|
||
import { languages } from "@shared/i18n";
|
||
import { slugifyDomain } from "@shared/utils/domains";
|
||
import { parseEmail } from "@shared/utils/email";
|
||
import slugify from "@server/utils/slugify";
|
||
import accountProvisioner from "@server/commands/accountProvisioner";
|
||
import { InvalidRequestError, TeamDomainRequiredError } from "@server/errors";
|
||
import passportMiddleware from "@server/middlewares/passport";
|
||
import type { User } from "@server/models";
|
||
import type { AuthenticationResult, AppContext } from "@server/types";
|
||
import {
|
||
StateStore,
|
||
getTeamFromContext,
|
||
getClientFromOAuthState,
|
||
getUserFromOAuthState,
|
||
request,
|
||
startOAuthFlow,
|
||
withProxyAgent,
|
||
} from "@server/utils/passport";
|
||
import config from "../../plugin.json";
|
||
import env from "../env";
|
||
import { DiscordGuildError, DiscordGuildRoleError } from "../errors";
|
||
import { withUser } from "@server/context";
|
||
|
||
const router = new Router();
|
||
|
||
const scope = ["identify", "email"];
|
||
|
||
if (env.DISCORD_SERVER_ID) {
|
||
scope.push("guilds", "guilds.members.read");
|
||
}
|
||
|
||
if (env.DISCORD_CLIENT_ID && env.DISCORD_CLIENT_SECRET) {
|
||
passport.use(
|
||
config.id,
|
||
withProxyAgent(
|
||
new Strategy(
|
||
{
|
||
clientID: env.DISCORD_CLIENT_ID,
|
||
clientSecret: env.DISCORD_CLIENT_SECRET,
|
||
passReqToCallback: true,
|
||
scope,
|
||
// @ts-expect-error Strategy typings target Express and do not match koa-passport usage
|
||
store: new StateStore(),
|
||
state: true,
|
||
callbackURL: `${env.URL}/auth/${config.id}.callback`,
|
||
authorizationURL:
|
||
"https://discord.com/api/oauth2/authorize?prompt=none",
|
||
tokenURL: "https://discord.com/api/oauth2/token",
|
||
pkce: false,
|
||
},
|
||
async function (
|
||
req: Request,
|
||
accessToken: string,
|
||
refreshToken: string,
|
||
params: { expires_in: number },
|
||
_profile: unknown,
|
||
done: (
|
||
err: Error | null,
|
||
user: User | null,
|
||
result?: AuthenticationResult
|
||
) => void
|
||
) {
|
||
const context: AppContext = req.ctx;
|
||
try {
|
||
const team = await getTeamFromContext(context);
|
||
const client = getClientFromOAuthState(context);
|
||
/** Fetch the user's profile */
|
||
const profile: RESTGetAPICurrentUserResult = await request(
|
||
"GET",
|
||
"https://discord.com/api/users/@me",
|
||
accessToken
|
||
);
|
||
|
||
const email = profile.email;
|
||
if (!email) {
|
||
/** We have the email scope, so this should never happen */
|
||
throw InvalidRequestError("Discord profile email is missing");
|
||
}
|
||
const { domain } = parseEmail(email);
|
||
|
||
if (!domain) {
|
||
throw TeamDomainRequiredError();
|
||
}
|
||
|
||
/** Determine the user's language from the locale */
|
||
const { locale } = profile;
|
||
const language = locale
|
||
? languages.find((l) => l.startsWith(locale))
|
||
: undefined;
|
||
|
||
/** Default user and team names metadata */
|
||
let userName = profile.username;
|
||
let teamName;
|
||
let userAvatarUrl: string = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png`;
|
||
let teamAvatarUrl: string | undefined = undefined;
|
||
let subdomain = slugifyDomain(domain);
|
||
|
||
/**
|
||
* If a Discord server is configured, we will check if the user is a member of the server
|
||
* Additionally, we can get the user's nickname in the server if it exists
|
||
*/
|
||
if (env.DISCORD_SERVER_ID) {
|
||
/** Fetch the guilds a user is in */
|
||
const guilds: RESTGetAPICurrentUserGuildsResult = await request(
|
||
"GET",
|
||
"https://discord.com/api/users/@me/guilds",
|
||
accessToken
|
||
);
|
||
|
||
/** Find the guild that matches the configured server ID */
|
||
const guild = guilds?.find((g) => g.id === env.DISCORD_SERVER_ID);
|
||
|
||
/** If the user is not in the server, throw an error */
|
||
if (!guild) {
|
||
throw DiscordGuildError();
|
||
}
|
||
|
||
/**
|
||
* Get the guild's icon
|
||
* https://discord.com/developers/docs/reference#image-formatting-cdn-endpoints
|
||
**/
|
||
if (guild.icon) {
|
||
const isGif = guild.icon.startsWith("a_");
|
||
if (isGif) {
|
||
teamAvatarUrl = `https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.gif`;
|
||
} else {
|
||
teamAvatarUrl = `https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.png`;
|
||
}
|
||
}
|
||
|
||
teamName = guild.name;
|
||
subdomain = slugify(guild.name);
|
||
|
||
/** If the guild name is a URL, use the subdomain instead – we do not allow URLs in names. */
|
||
if (
|
||
isURL(teamName, {
|
||
require_host: false,
|
||
require_protocol: false,
|
||
})
|
||
) {
|
||
teamName = subdomain;
|
||
}
|
||
|
||
/** Fetch the user's member object in the server for nickname and roles */
|
||
const guildMember: RESTGetCurrentUserGuildMemberResult =
|
||
await request(
|
||
"GET",
|
||
`https://discord.com/api/users/@me/guilds/${env.DISCORD_SERVER_ID}/member`,
|
||
accessToken
|
||
);
|
||
|
||
/** If the user has a nickname in the server, use that as the name */
|
||
if (guildMember.nick) {
|
||
userName = guildMember.nick;
|
||
}
|
||
|
||
/** If the user has a custom avatar in the server, use that as the avatar */
|
||
if (guildMember.avatar) {
|
||
userAvatarUrl = `https://cdn.discordapp.com/guilds/${guild.id}/users/${profile.id}/avatars/${guildMember.avatar}.png`;
|
||
}
|
||
|
||
/** If server roles are configured, check if the user has any of the roles */
|
||
if (env.DISCORD_SERVER_ROLES) {
|
||
const { roles } = guildMember;
|
||
const hasRole = roles?.some((role) =>
|
||
env.DISCORD_SERVER_ROLES?.includes(role)
|
||
);
|
||
|
||
/** If the user does not have any of the roles, throw an error */
|
||
if (!hasRole) {
|
||
throw DiscordGuildRoleError();
|
||
}
|
||
}
|
||
}
|
||
const user =
|
||
context.auth?.user ?? (await getUserFromOAuthState(context));
|
||
|
||
// if a team can be inferred, we assume the user is only interested in signing into
|
||
// that team in particular; otherwise, we will do a best effort at finding their account
|
||
// or provisioning a new one (within AccountProvisioner)
|
||
const ctx = withUser(context, user);
|
||
const result = await accountProvisioner(ctx, {
|
||
team: {
|
||
teamId: team?.id,
|
||
name: teamName,
|
||
domain,
|
||
subdomain,
|
||
avatarUrl: teamAvatarUrl,
|
||
},
|
||
user: {
|
||
email,
|
||
emailVerified: profile.verified,
|
||
name: userName,
|
||
language,
|
||
avatarUrl: userAvatarUrl,
|
||
},
|
||
authenticationProvider: {
|
||
name: config.id,
|
||
providerId: env.DISCORD_SERVER_ID ?? "",
|
||
},
|
||
authentication: {
|
||
providerId: profile.id,
|
||
accessToken,
|
||
refreshToken,
|
||
expiresIn: params.expires_in,
|
||
scopes: scope,
|
||
},
|
||
});
|
||
return done(null, result.user, { ...result, client });
|
||
} catch (err) {
|
||
return done(toError(err), null);
|
||
}
|
||
}
|
||
)
|
||
)
|
||
);
|
||
|
||
router.get(
|
||
config.id,
|
||
startOAuthFlow,
|
||
passport.authenticate(config.id, {
|
||
scope,
|
||
})
|
||
);
|
||
router.get(`${config.id}.callback`, passportMiddleware(config.id));
|
||
}
|
||
|
||
export default router;
|