Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
import { auth } from "@crm/auth/auth";
import { isGoogleConfigured, isMicrosoftConfigured } from "@crm/auth/env";
import { WORKSPACE_ID, workspaceRoleOf } from "@crm/auth/organization";
import {
canConfigureSso,
ssoCallbackBase,
ssoCallbackURL,
ssoProviderName,
} from "@crm/auth/sso";
import type { Db, Prisma } from "@crm/db";
import {
BadRequestException,
ForbiddenException,
HttpException,
Injectable,
InternalServerErrorException,
Logger,
} from "@nestjs/common";
import { APIError } from "better-auth/api";
import { z } from "zod";
import { InjectDatabase } from "../database/database.constants";
import {
type ListResult,
type OrderByColumns,
paginate,
resolveOrderBy,
} from "../trpc/list-input";
import type {
DeleteSsoProviderInput,
RegisterSsoProviderInput,
SignInOptions,
SsoProvider,
SsoProviderListInput,
SsoSettings,
} from "./sso.contracts";
const PROVIDER_SELECT = {
providerId: true,
issuer: true,
domain: true,
oidcConfig: true,
samlConfig: true,
} satisfies Prisma.SsoProviderSelect;
type ProviderRow = Prisma.SsoProviderGetPayload<{
select: typeof PROVIDER_SELECT;
}>;
const SORTABLE: OrderByColumns<Prisma.SsoProviderOrderByWithRelationInput> = {
providerId: (dir) => ({ providerId: dir }),
domain: (dir) => ({ domain: dir }),
issuer: (dir) => ({ issuer: dir }),
};
const STATUS_BY_CODE = new Map<string, number>([
["BAD_REQUEST", 400],
["UNAUTHORIZED", 401],
["FORBIDDEN", 403],
["NOT_FOUND", 404],
["CONFLICT", 409],
["UNPROCESSABLE_ENTITY", 400],
]);
function splitDomains(value: string): string[] {
return value
.split(",")
.map((part) =>
part
.trim()
.replace(/^https?:\/\//i, "")
.replace(/\/.*$/, ""),
)
.map((part) => part.toLowerCase())
.filter(Boolean);
}
const oidcConfig = z
.object({ clientId: z.string().catch("") })
.catch({ clientId: "" });
type OidcConfig = z.infer<typeof oidcConfig>;
function lastFour(clientId: string): string | null {
return clientId.length >= 4 ? clientId.slice(-4) : null;
}
function readOidcConfig(value: string | null): OidcConfig | null {
if (!value) return null;
try {
return oidcConfig.parse(JSON.parse(value));
} catch {
return null;
}
}
function toProvider(row: ProviderRow): SsoProvider {
const oidc = readOidcConfig(row.oidcConfig);
return {
providerId: row.providerId,
name: ssoProviderName(row.providerId),
type: row.samlConfig ? "saml" : "oidc",
issuer: row.issuer,
domains: splitDomains(row.domain),
clientIdLastFour: oidc ? lastFour(oidc.clientId) : null,
callbackURL: ssoCallbackURL(row.providerId),
};
}
@Injectable()
export class SsoService {
private readonly logger = new Logger(SsoService.name);
constructor(@InjectDatabase() private readonly db: Db) {}
async signInOptions(): Promise<SignInOptions> {
const rows = await this.db.ssoProvider.findMany({
where: { organizationId: WORKSPACE_ID },
select: { providerId: true },
orderBy: { providerId: "asc" },
});
return {
google: isGoogleConfigured(),
microsoft: isMicrosoftConfigured(),
providers: rows.map((row) => ({
providerId: row.providerId,
name: ssoProviderName(row.providerId),
})),
};
}
async settings(userId: string): Promise<SsoSettings> {
return {
canConfigure: canConfigureSso(await workspaceRoleOf(userId, this.db)),
callbackBase: ssoCallbackBase(),
};
}
async list(input: SsoProviderListInput): Promise<ListResult<SsoProvider>> {
const where = this.searchWhere(input.q);
const { skip, take } = paginate(input);
const [rows, total] = await Promise.all([
this.db.ssoProvider.findMany({
where,
skip,
take,
select: PROVIDER_SELECT,
orderBy: resolveOrderBy(input, SORTABLE, { providerId: "asc" }),
}),
this.db.ssoProvider.count({ where }),
]);
return { rows: rows.map(toProvider), total, facetCounts: {} };
}
async register(
userId: string,
headers: Headers,
input: RegisterSsoProviderInput,
): Promise<SsoProvider> {
await this.requireConfigurer(userId);
const domains = splitDomains(input.domain);
if (domains.length === 0) {
throw new BadRequestException(
"Give the email domain your people sign in with, for example acme.com.",
);
}
await this.call(() =>
auth.api.registerSSOProvider({
headers,
body: {
providerId: input.providerId,
issuer: input.issuer,
domain: domains.join(","),
organizationId: WORKSPACE_ID,
oidcConfig: {
clientId: input.clientId,
clientSecret: input.clientSecret,
pkce: true,
},
},
}),
);
this.logger.log({
message: "SSO provider registered",
userId,
providerId: input.providerId,
issuer: input.issuer,
});
const row = await this.db.ssoProvider.findUniqueOrThrow({
where: { providerId: input.providerId },
select: PROVIDER_SELECT,
});
return toProvider(row);
}
async remove(
userId: string,
headers: Headers,
input: DeleteSsoProviderInput,
): Promise<{ providerId: string }> {
await this.requireConfigurer(userId);
await this.call(() =>
auth.api.deleteSSOProvider({
headers,
body: { providerId: input.providerId },
}),
);
this.logger.log({
message: "SSO provider removed",
userId,
providerId: input.providerId,
});
return { providerId: input.providerId };
}
private searchWhere(q: string): Prisma.SsoProviderWhereInput {
const term = q.trim();
const where: Prisma.SsoProviderWhereInput = {
organizationId: WORKSPACE_ID,
};
if (term) {
where.OR = [
{ providerId: { contains: term, mode: "insensitive" } },
{ domain: { contains: term, mode: "insensitive" } },
{ issuer: { contains: term, mode: "insensitive" } },
];
}
return where;
}
private async call<T>(run: () => Promise<T>): Promise<T> {
try {
return await run();
} catch (error) {
if (error instanceof APIError) {
const status =
STATUS_BY_CODE.get(error.body?.code ?? "") ?? error.statusCode;
throw new HttpException(
error.body?.message ?? "The identity provider could not be saved.",
status,
);
}
this.logger.error(
{ message: "SSO provider call failed" },
error instanceof Error ? error.stack : String(error),
);
throw new InternalServerErrorException(
"Could not reach the identity provider.",
);
}
}
private async requireConfigurer(userId: string): Promise<void> {
if (!canConfigureSso(await workspaceRoleOf(userId, this.db))) {
throw new ForbiddenException(
"Only an owner or an admin can change how people sign in.",
);
}
}
}