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>
200 lines
4.8 KiB
TypeScript
200 lines
4.8 KiB
TypeScript
import type {
|
|
InferAttributes,
|
|
InferCreationAttributes,
|
|
Transaction,
|
|
} from "sequelize";
|
|
import { Op } from "sequelize";
|
|
import {
|
|
BeforeDestroy,
|
|
BelongsTo,
|
|
Column,
|
|
CreatedAt,
|
|
DataType,
|
|
Default,
|
|
ForeignKey,
|
|
HasMany,
|
|
Table,
|
|
IsUUID,
|
|
PrimaryKey,
|
|
Scopes,
|
|
} from "sequelize-typescript";
|
|
import type { AuthenticationProviderSettings } from "@shared/types";
|
|
import AuthenticationHelper from "@server/models/helpers/AuthenticationHelper";
|
|
import Model from "@server/models/base/Model";
|
|
import { ValidationError } from "../errors";
|
|
import Team from "./Team";
|
|
import UserAuthentication from "./UserAuthentication";
|
|
import Length from "./validators/Length";
|
|
|
|
import type { Context } from "@server/types";
|
|
import type { DestroyOptions } from "sequelize";
|
|
|
|
@Scopes(() => ({
|
|
withUserAuthentication: (userId: string) => ({
|
|
include: [
|
|
{
|
|
model: UserAuthentication,
|
|
as: "userAuthentications",
|
|
required: true,
|
|
where: {
|
|
userId,
|
|
},
|
|
},
|
|
],
|
|
}),
|
|
}))
|
|
@Table({
|
|
tableName: "authentication_providers",
|
|
modelName: "authentication_provider",
|
|
updatedAt: false,
|
|
})
|
|
class AuthenticationProvider extends Model<
|
|
InferAttributes<AuthenticationProvider>,
|
|
Partial<InferCreationAttributes<AuthenticationProvider>>
|
|
> {
|
|
@IsUUID(4)
|
|
@PrimaryKey
|
|
@Default(DataType.UUIDV4)
|
|
@Column(DataType.UUID)
|
|
id: string;
|
|
|
|
@Length({
|
|
max: 255,
|
|
msg: "name must be 255 characters or less",
|
|
})
|
|
@Column(DataType.STRING)
|
|
name: string;
|
|
|
|
@Default(true)
|
|
@Column(DataType.BOOLEAN)
|
|
enabled: boolean;
|
|
|
|
@Length({
|
|
max: 255,
|
|
msg: "providerId must be 255 characters or less",
|
|
})
|
|
@Column(DataType.STRING)
|
|
providerId: string;
|
|
|
|
/** Provider-specific settings such as group sync configuration. */
|
|
@Column(DataType.JSONB)
|
|
settings: AuthenticationProviderSettings | null;
|
|
|
|
@CreatedAt
|
|
createdAt: Date;
|
|
|
|
// associations
|
|
|
|
@BelongsTo(() => Team, "teamId")
|
|
team: Team;
|
|
|
|
@ForeignKey(() => Team)
|
|
@Column(DataType.UUID)
|
|
teamId: string;
|
|
|
|
@HasMany(() => UserAuthentication, "authenticationProviderId")
|
|
userAuthentications: UserAuthentication[];
|
|
|
|
// instance methods
|
|
|
|
/**
|
|
* The human-readable display name for this provider, resolved from the
|
|
* plugin registry. Falls back to the raw provider name.
|
|
*/
|
|
get displayName(): string {
|
|
return (
|
|
AuthenticationHelper.providers.find((p) => p.value.id === this.name)
|
|
?.name ?? this.name
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create an OAuthClient for this provider, if possible.
|
|
*
|
|
* @returns A configured OAuthClient instance
|
|
*/
|
|
get oauthClient() {
|
|
return AuthenticationHelper.providers
|
|
.find((p) => p.value.id === this.name)
|
|
?.value.oauthClient?.();
|
|
}
|
|
|
|
/**
|
|
* Check if this provider can be disabled or destroyed.
|
|
* Throws an error if this is the last enabled authentication provider.
|
|
*
|
|
* @param transaction - Database transaction to use for the check.
|
|
* @throws ValidationError if disabling is not allowed.
|
|
*/
|
|
private async checkCanBeDisabled(
|
|
transaction?: Transaction | null
|
|
): Promise<void> {
|
|
// Check if email sign-in is enabled for the team first
|
|
const team = await Team.findByPk(this.teamId, {
|
|
transaction,
|
|
lock: transaction?.LOCK.SHARE,
|
|
});
|
|
if (team?.emailSigninEnabled) {
|
|
return;
|
|
}
|
|
|
|
const otherEnabledProviders = await (
|
|
this.constructor as typeof AuthenticationProvider
|
|
).findAll({
|
|
transaction,
|
|
lock: transaction?.LOCK.SHARE,
|
|
where: {
|
|
teamId: this.teamId,
|
|
enabled: true,
|
|
id: {
|
|
[Op.ne]: this.id,
|
|
},
|
|
},
|
|
limit: 1,
|
|
});
|
|
|
|
if (otherEnabledProviders.length === 0) {
|
|
throw ValidationError("At least one authentication provider is required");
|
|
}
|
|
}
|
|
|
|
@BeforeDestroy
|
|
static async checkBeforeDestroy(
|
|
instance: AuthenticationProvider,
|
|
options: DestroyOptions
|
|
) {
|
|
if (instance.enabled) {
|
|
await instance.checkCanBeDisabled(options.transaction);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Disable this authentication provider after ensuring it's allowed.
|
|
*
|
|
* @param ctx - context containing the transaction.
|
|
* @returns The updated AuthenticationProvider instance.
|
|
* @throws ValidationError if disabling is not allowed.
|
|
*/
|
|
disable: (ctx: Context) => Promise<AuthenticationProvider> = async (ctx) => {
|
|
const { transaction } = ctx;
|
|
await this.checkCanBeDisabled(transaction);
|
|
|
|
return this.updateWithCtx(ctx, {
|
|
enabled: false,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Enable this authentication provider.
|
|
*
|
|
* @param ctx - context containing the transaction.
|
|
* @returns The updated AuthenticationProvider instance.
|
|
*/
|
|
enable: (ctx: Context) => Promise<AuthenticationProvider> = async (ctx) =>
|
|
this.updateWithCtx(ctx, {
|
|
enabled: true,
|
|
});
|
|
}
|
|
|
|
export default AuthenticationProvider;
|