-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathoauth.controller.ts
More file actions
500 lines (441 loc) · 14.7 KB
/
oauth.controller.ts
File metadata and controls
500 lines (441 loc) · 14.7 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import {
Controller,
Get,
Post,
Query,
Body,
Res,
HttpException,
HttpStatus,
Logger,
} from '@nestjs/common';
import type { Response } from 'express';
import { randomBytes, createHash } from 'crypto';
import { OAuthStateRepository } from '../repositories/oauth-state.repository';
import { ProviderRepository } from '../repositories/provider.repository';
import { ConnectionRepository } from '../repositories/connection.repository';
import { CredentialVaultService } from '../services/credential-vault.service';
import { ConnectionService } from '../services/connection.service';
import { OAuthCredentialsService } from '../services/oauth-credentials.service';
import { AutoCheckRunnerService } from '../services/auto-check-runner.service';
import { getManifest, type OAuthConfig } from '@comp/integration-platform';
interface StartOAuthDto {
providerSlug: string;
organizationId: string;
userId: string;
redirectUrl?: string;
}
interface OAuthCallbackQuery {
code: string;
state: string;
error?: string;
error_description?: string;
}
@Controller({ path: 'integrations/oauth', version: '1' })
export class OAuthController {
private readonly logger = new Logger(OAuthController.name);
constructor(
private readonly oauthStateRepository: OAuthStateRepository,
private readonly providerRepository: ProviderRepository,
private readonly connectionRepository: ConnectionRepository,
private readonly credentialVaultService: CredentialVaultService,
private readonly connectionService: ConnectionService,
private readonly oauthCredentialsService: OAuthCredentialsService,
private readonly autoCheckRunnerService: AutoCheckRunnerService,
) {}
/**
* Check if OAuth credentials are available for a provider
*/
@Get('availability')
async checkAvailability(
@Query('providerSlug') providerSlug: string,
@Query('organizationId') organizationId: string,
) {
if (!providerSlug || !organizationId) {
throw new HttpException(
'providerSlug and organizationId are required',
HttpStatus.BAD_REQUEST,
);
}
return this.oauthCredentialsService.checkAvailability(
providerSlug,
organizationId,
);
}
/**
* Start OAuth flow - returns authorization URL
*/
@Post('start')
async startOAuth(
@Body() body: StartOAuthDto,
): Promise<{ authorizationUrl: string }> {
const { providerSlug, organizationId, userId, redirectUrl } = body;
// Get manifest and OAuth config
const manifest = getManifest(providerSlug);
if (!manifest) {
throw new HttpException(
`Provider ${providerSlug} not found`,
HttpStatus.NOT_FOUND,
);
}
if (manifest.auth.type !== 'oauth2') {
throw new HttpException(
`Provider ${providerSlug} does not use OAuth`,
HttpStatus.BAD_REQUEST,
);
}
const oauthConfig = manifest.auth.config;
// Get OAuth credentials (org-level or platform-level)
const credentials = await this.oauthCredentialsService.getCredentials(
providerSlug,
organizationId,
);
if (!credentials) {
const availability = await this.oauthCredentialsService.checkAvailability(
providerSlug,
organizationId,
);
throw new HttpException(
{
message: `No OAuth credentials available for ${providerSlug}`,
setupInstructions: availability.setupInstructions,
createAppUrl: availability.createAppUrl,
},
HttpStatus.PRECONDITION_FAILED,
);
}
// Ensure provider exists in DB
await this.providerRepository.upsert({
slug: manifest.id,
name: manifest.name,
category: manifest.category,
capabilities: manifest.capabilities,
isActive: manifest.isActive,
});
// Generate PKCE code verifier if needed
let codeVerifier: string | undefined;
let codeChallenge: string | undefined;
if (oauthConfig.pkce) {
codeVerifier = randomBytes(32).toString('base64url');
codeChallenge = createHash('sha256')
.update(codeVerifier)
.digest('base64url');
}
// Create OAuth state record
const oauthState = await this.oauthStateRepository.create({
providerSlug,
organizationId,
userId,
codeVerifier,
redirectUrl,
});
// Build authorization URL, replacing any placeholders with additional OAuth settings
let authorizeUrl = oauthConfig.authorizeUrl;
if (credentials.customSettings && oauthConfig.additionalOAuthSettings) {
// Dynamically replace tokens based on additionalOAuthSettings definition
for (const setting of oauthConfig.additionalOAuthSettings) {
if (setting.token && credentials.customSettings[setting.id]) {
authorizeUrl = authorizeUrl.replace(
setting.token,
String(credentials.customSettings[setting.id]),
);
}
}
}
const authUrl = new URL(authorizeUrl);
// Standard OAuth2 params
authUrl.searchParams.set('client_id', credentials.clientId);
authUrl.searchParams.set('response_type', 'code');
authUrl.searchParams.set('state', oauthState.state);
// Callback URL
const callbackUrl = `${process.env.BASE_URL || 'http://localhost:3333'}/v1/integrations/oauth/callback`;
authUrl.searchParams.set('redirect_uri', callbackUrl);
// Scopes
if (credentials.scopes.length > 0) {
authUrl.searchParams.set('scope', credentials.scopes.join(' '));
}
// PKCE
if (codeChallenge) {
authUrl.searchParams.set('code_challenge', codeChallenge);
authUrl.searchParams.set('code_challenge_method', 'S256');
}
// Additional authorization params from manifest
if (oauthConfig.authorizationParams) {
for (const [key, value] of Object.entries(
oauthConfig.authorizationParams,
)) {
authUrl.searchParams.set(key, String(value));
}
}
this.logger.log(
`Starting OAuth flow for ${providerSlug}, org: ${organizationId} (credentials from ${credentials.source})`,
);
return { authorizationUrl: authUrl.toString() };
}
/**
* OAuth callback - exchanges code for tokens
*/
@Get('callback')
async oauthCallback(
@Query() query: OAuthCallbackQuery,
@Res() res: Response,
): Promise<void> {
const { code, state, error, error_description } = query;
// Handle OAuth errors
if (error) {
this.logger.error(`OAuth error: ${error} - ${error_description}`);
const errorUrl = this.buildRedirectUrl(null, {
error,
error_description: error_description || 'OAuth authorization failed',
});
res.redirect(errorUrl);
return;
}
if (!code || !state) {
const errorUrl = this.buildRedirectUrl(null, {
error: 'invalid_request',
error_description: 'Missing code or state parameter',
});
res.redirect(errorUrl);
return;
}
// Validate state
const oauthState = await this.oauthStateRepository.findByState(state);
if (!oauthState) {
const errorUrl = this.buildRedirectUrl(null, {
error: 'invalid_state',
error_description: 'Invalid or expired OAuth state',
});
res.redirect(errorUrl);
return;
}
if (oauthState.expiresAt < new Date()) {
await this.oauthStateRepository.delete(state);
const errorUrl = this.buildRedirectUrl(
oauthState.redirectUrl,
{
error: 'expired_state',
error_description: 'OAuth state has expired',
},
oauthState.organizationId,
);
res.redirect(errorUrl);
return;
}
try {
// Get manifest and OAuth config
const manifest = getManifest(oauthState.providerSlug);
if (!manifest || manifest.auth.type !== 'oauth2') {
throw new Error(`Invalid provider: ${oauthState.providerSlug}`);
}
const oauthConfig = manifest.auth.config;
// Get OAuth credentials
const credentials = await this.oauthCredentialsService.getCredentials(
oauthState.providerSlug,
oauthState.organizationId,
);
if (!credentials) {
throw new Error('OAuth credentials no longer available');
}
// Exchange code for tokens
const tokens = await this.exchangeCodeForTokens(
oauthConfig,
credentials,
code,
oauthState.codeVerifier,
);
// Get or create provider
const provider = await this.providerRepository.findBySlug(
oauthState.providerSlug,
);
if (!provider) {
throw new Error(`Provider not found: ${oauthState.providerSlug}`);
}
// Get or create connection
let connection = await this.connectionRepository.findByProviderAndOrg(
provider.id,
oauthState.organizationId,
);
if (!connection) {
connection = await this.connectionService.createConnection({
providerSlug: oauthState.providerSlug,
organizationId: oauthState.organizationId,
authStrategy: 'oauth2',
});
}
// Store tokens
await this.credentialVaultService.storeOAuthTokens(connection.id, tokens);
// Provider-specific post-OAuth actions
if (oauthState.providerSlug === 'rippling') {
// Rippling requires calling mark_app_installed to finalize
// See: https://developer.rippling.com/documentation/developer-portal/v2-guides/installation
await this.markRipplingAppInstalled(tokens.access_token);
}
// Clean up state
await this.oauthStateRepository.delete(state);
this.logger.log(
`OAuth completed for ${oauthState.providerSlug}, org: ${oauthState.organizationId}`,
);
// Auto-run checks if possible (fire and forget - don't block the redirect)
this.autoCheckRunnerService
.tryAutoRunChecks(connection.id)
.then((didRun) => {
if (didRun) {
this.logger.log(
`Auto-ran checks for ${oauthState.providerSlug} after OAuth`,
);
}
})
.catch((err) => {
this.logger.warn(
`Failed to auto-run checks after OAuth: ${err.message}`,
);
});
// Redirect to success URL
const successUrl = this.buildRedirectUrl(
oauthState.redirectUrl,
{
success: 'true',
provider: oauthState.providerSlug,
},
oauthState.organizationId,
);
res.redirect(successUrl);
} catch (err) {
this.logger.error(`OAuth callback error: ${err}`);
await this.oauthStateRepository.delete(state);
const errorUrl = this.buildRedirectUrl(
oauthState.redirectUrl,
{
error: 'token_exchange_failed',
error_description:
err instanceof Error
? err.message
: 'Failed to exchange code for tokens',
},
oauthState.organizationId,
);
res.redirect(errorUrl);
}
}
private async exchangeCodeForTokens(
config: OAuthConfig,
credentials: { clientId: string; clientSecret: string },
code: string,
codeVerifier?: string | null,
): Promise<{
access_token: string;
refresh_token?: string;
token_type?: string;
expires_in?: number;
scope?: string;
}> {
const callbackUrl = `${process.env.BASE_URL || 'http://localhost:3333'}/v1/integrations/oauth/callback`;
// Build token request body
const body = new URLSearchParams({
grant_type: 'authorization_code',
code,
redirect_uri: callbackUrl,
});
// Add PKCE verifier if present
if (codeVerifier) {
body.set('code_verifier', codeVerifier);
}
// Add additional token params from manifest
if (config.tokenParams) {
for (const [key, value] of Object.entries(config.tokenParams)) {
body.set(key, String(value));
}
}
// Prepare headers
const headers: Record<string, string> = {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
};
// Add client credentials based on auth method
// Per OAuth 2.0 RFC 6749 Section 2.3.1, when using HTTP Basic auth (header),
// client credentials should NOT be included in the request body
if (config.clientAuthMethod === 'header') {
const creds = Buffer.from(
`${credentials.clientId}:${credentials.clientSecret}`,
).toString('base64');
headers['Authorization'] = `Basic ${creds}`;
} else {
// Default: send in body
body.set('client_id', credentials.clientId);
body.set('client_secret', credentials.clientSecret);
}
// Make token request
const response = await fetch(config.tokenUrl, {
method: 'POST',
headers,
body: body.toString(),
});
if (!response.ok) {
const errorBody = await response.text();
this.logger.error(
`Token exchange failed: ${response.status} - ${errorBody}`,
);
throw new Error(`Token exchange failed: ${response.status}`);
}
const tokens = await response.json();
if (!tokens.access_token) {
throw new Error('No access token in response');
}
return tokens;
}
/**
* Mark Rippling app as installed (required by Rippling)
* See: https://developer.rippling.com/documentation/developer-portal/v2-guides/installation
*/
private async markRipplingAppInstalled(accessToken: string): Promise<void> {
try {
const response = await fetch(
'https://api.rippling.com/platform/api/mark_app_installed',
{
method: 'POST',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${accessToken}`,
},
},
);
if (!response.ok) {
const errorText = await response.text();
this.logger.warn(
`Failed to mark Rippling app as installed: ${response.status} - ${errorText}`,
);
// Don't throw - the OAuth flow itself succeeded
} else {
await response.json(); // consume body
this.logger.log('Rippling app marked as installed');
}
} catch (error) {
this.logger.warn(`Error marking Rippling app as installed: ${error}`);
// Don't throw - the OAuth flow itself succeeded
}
}
private buildRedirectUrl(
baseUrl: string | null | undefined,
params: Record<string, string>,
organizationId?: string,
): string {
// Use provided URL or build default with org ID
let targetUrl: string;
if (baseUrl) {
targetUrl = baseUrl;
} else {
targetUrl = `${process.env.APP_URL || 'http://localhost:3000'}`;
if (organizationId) {
targetUrl += `/${organizationId}/integrations`;
} else {
targetUrl += '/integrations';
}
}
const url = new URL(targetUrl);
for (const [key, value] of Object.entries(params)) {
url.searchParams.set(key, value);
}
return url.toString();
}
}