-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathindex.ts
More file actions
291 lines (255 loc) · 9.61 KB
/
Copy pathindex.ts
File metadata and controls
291 lines (255 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import type {
DirectorySyncEffect,
DirectorySyncStatus,
OrgSsoStatus,
PluginDatabaseConfig,
SsoBeginError,
SsoCompleteError,
SsoController,
SsoDecisionError,
SsoFlow,
SsoMutationError,
SsoPlugin,
SsoPortalError,
SsoProfile,
SsoResolutionDecision,
SsoRouteDecision,
SsoValidateError,
SsoWebhookError,
SsoWebhookEvent,
} from "@trigger.dev/plugins";
import type { PrismaClient } from "@trigger.dev/database";
import { ResultAsync } from "neverthrow";
import { SsoFallback } from "./fallback.js";
export type { SsoController } from "@trigger.dev/plugins";
export type SsoPrismaInput = PrismaClient | { primary: PrismaClient; replica: PrismaClient };
export type SsoCreateOptions = {
// When true, skip loading the plugin. Useful for tests and for
// contributors who don't have the cloud plugin installed.
forceFallback?: boolean;
// Override the dynamic importer. Lets tests inject a fake plugin
// module or a synthetic ERR_MODULE_NOT_FOUND failure without touching
// the real plugin install on disk.
importer?: (moduleName: string) => Promise<{ default: SsoPlugin }>;
// Writer/reader connection URLs + pool sizes for a plugin that owns its
// own database client, resolved by the host from its env so the plugin
// follows the host's writer/replica topology. The fallback ignores this —
// it queries through the Prisma clients passed as `SsoPrismaInput`.
database?: PluginDatabaseConfig;
};
// Loads the cloud plugin lazily; falls back to the OSS no-op
// implementation if not installed. Synchronous create() avoids
// top-level await (not supported in the webapp's CJS build).
export class LazyController implements SsoController {
private readonly _init: Promise<SsoController>;
constructor(prisma: SsoPrismaInput, options?: SsoCreateOptions) {
this._init = this.load(prisma, options);
}
private async load(prisma: SsoPrismaInput, options?: SsoCreateOptions): Promise<SsoController> {
if (options?.forceFallback) {
return new SsoFallback(prisma).create();
}
const moduleName = "@triggerdotdev/plugins/sso";
const importer =
options?.importer ?? ((m: string) => import(m) as Promise<{ default: SsoPlugin }>);
try {
const module = await importer(moduleName);
const plugin: SsoPlugin = module.default;
console.log("SSO: using plugin implementation");
return plugin.create({ database: options?.database });
} catch (err) {
// Distinguish the two failure modes the dynamic import can hit:
//
// 1. The plugin itself is absent (no install) — expected on OSS
// deployments. Quiet by default; logged when SSO_LOG_FALLBACK=1
// so contributors can opt into a visible signal locally.
// 2. The plugin module loaded but its initialization failed
// (transitive dep missing, syntax error, …). Always logged
// loudly because this indicates a real bug.
//
// Node throws ERR_MODULE_NOT_FOUND for both cases, so we
// disambiguate by checking whether the missing specifier is the
// plugin's own module name.
const code = (err as NodeJS.ErrnoException | undefined)?.code;
const message = err instanceof Error ? err.message : String(err);
const isModuleNotFound = code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND";
const isPluginItselfMissing = isModuleNotFound && message.includes(moduleName);
if (!isPluginItselfMissing) {
console.error(
"SSO: plugin found but failed to load; falling back to default implementation",
err
);
} else if (process.env.SSO_LOG_FALLBACK === "1" || process.env.SSO_LOG_FALLBACK === "true") {
console.log("SSO: no plugin installed (ERR_MODULE_NOT_FOUND); using fallback");
}
return new SsoFallback(prisma).create();
}
}
private c(): Promise<SsoController> {
return this._init;
}
// Bridges a Promise<ResultAsync<T,E>> back into a ResultAsync<T,E>.
// The `load()` method above always resolves (it catches and falls
// back), so `this.c()` is safe to lift via fromSafePromise. Inner
// controller methods are expected to never throw — they return
// errors via the Result instead — so the .andThen flatten is total.
private call<T, E>(factory: (c: SsoController) => ResultAsync<T, E>): ResultAsync<T, E> {
return ResultAsync.fromSafePromise(this.c().then(factory)).andThen((r) => r);
}
async isUsingPlugin(): Promise<boolean> {
return (await this.c()).isUsingPlugin();
}
getStatus(organizationId: string): ResultAsync<OrgSsoStatus, SsoDecisionError> {
return this.call((c) => c.getStatus(organizationId));
}
generatePortalLink(params: {
organizationId: string;
userId: string;
intent: "sso" | "domain_verification" | "dsync";
returnUrl: string;
}): ResultAsync<{ url: string }, SsoPortalError> {
return this.call((c) => c.generatePortalLink(params));
}
setEnforced(params: {
organizationId: string;
enforced: boolean;
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.setEnforced(params));
}
setJitProvisioningEnabled(params: {
organizationId: string;
enabled: boolean;
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.setJitProvisioningEnabled(params));
}
setJitDefaultRole(params: {
organizationId: string;
roleId: string | null;
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.setJitDefaultRole(params));
}
updateConfig(params: {
organizationId: string;
enforced: boolean;
jitProvisioningEnabled: boolean;
jitDefaultRoleId: string | null;
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.updateConfig(params));
}
getDirectorySyncStatus(
organizationId: string
): ResultAsync<DirectorySyncStatus, SsoDecisionError> {
return this.call((c) => c.getDirectorySyncStatus(organizationId));
}
setDirectoryGroupRole(params: {
organizationId: string;
groupId: string;
roleId: string | null;
}): ResultAsync<{ effects: DirectorySyncEffect[] }, SsoMutationError> {
return this.call((c) => c.setDirectoryGroupRole(params));
}
setDirectoryDefaultRole(params: {
organizationId: string;
roleId: string | null;
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.setDirectoryDefaultRole(params));
}
setAllowExternalDomainSync(params: {
organizationId: string;
allowed: boolean;
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.setAllowExternalDomainSync(params));
}
getMembershipPolicy(
organizationId: string
): ResultAsync<{ manualMembershipAllowed: boolean }, SsoDecisionError> {
return this.call((c) => c.getMembershipPolicy(organizationId));
}
setAllowManualMembership(params: {
organizationId: string;
allowed: boolean;
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.setAllowManualMembership(params));
}
recordMembershipRemoval(params: {
organizationId: string;
userId: string;
reason: "manual_removal" | "self_leave";
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.recordMembershipRemoval(params));
}
clearMembershipRemoval(params: {
organizationId: string;
userId: string;
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.clearMembershipRemoval(params));
}
decideRouteForEmail(email: string): ResultAsync<SsoRouteDecision, SsoDecisionError> {
return this.call((c) => c.decideRouteForEmail(email));
}
beginAuthorization(params: {
email: string;
redirectTo: string;
flow: SsoFlow;
}): ResultAsync<{ url: string }, SsoBeginError> {
return this.call((c) => c.beginAuthorization(params));
}
completeAuthorization(params: {
code: string;
state: string;
}): ResultAsync<{ profile: SsoProfile; redirectTo: string; flow: SsoFlow }, SsoCompleteError> {
return this.call((c) => c.completeAuthorization(params));
}
completeIdpInitiatedAuthorization(params: {
code: string;
}): ResultAsync<{ profile: SsoProfile; redirectTo: string }, SsoCompleteError> {
return this.call((c) => c.completeIdpInitiatedAuthorization(params));
}
validateSession(params: {
userId: string;
idpOrgId: string;
connectionId: string;
}): ResultAsync<{ valid: boolean }, SsoValidateError> {
return this.call((c) => c.validateSession(params));
}
resolveSsoIdentity(params: {
profile: SsoProfile;
}): ResultAsync<SsoResolutionDecision, SsoMutationError> {
return this.call((c) => c.resolveSsoIdentity(params));
}
attachSsoIdentity(params: {
userId: string;
profile: SsoProfile;
}): ResultAsync<void, SsoMutationError> {
return this.call((c) => c.attachSsoIdentity(params));
}
evaluateJit(params: {
userId: string;
idpOrgId: string;
}): ResultAsync<
{ shouldProvision: boolean; organizationId: string; roleId: string | null },
SsoMutationError
> {
return this.call((c) => c.evaluateJit(params));
}
verifyWebhook(params: {
rawBody: string;
headers: Record<string, string>;
}): ResultAsync<{ event: SsoWebhookEvent }, SsoWebhookError> {
return this.call((c) => c.verifyWebhook(params));
}
processWebhookEvent(
event: SsoWebhookEvent
): ResultAsync<{ effects: DirectorySyncEffect[] }, SsoWebhookError> {
return this.call((c) => c.processWebhookEvent(event));
}
}
class Sso {
// Synchronous — returns a lazy controller that resolves any installed
// plugin on first call.
create(prisma: SsoPrismaInput, options?: SsoCreateOptions): SsoController {
return new LazyController(prisma, options);
}
}
const loader = new Sso();
export default loader;