Files
triggerdotdev--trigger.dev/apps/webapp/app/models/runConnection.server.ts
Eric Allam c0dfa8048a feat: BYO Auth (#491)
* feat: BYO Auth

Define client-side auth resolvers to be able to supply custom authentication credentials for integrations before a run is performed

- Added new defineAuthResolver
- Update all integrations to support the new auth resolvers
- Strip internal symbols from .d.ts in integrations and trigger-sdk
- Added BYO Auth docs
- Update Dynamic Schedule to support associated account IDs
- Create external accounts just-in-time
- Added Account ID field to test job when there are external auth integrations
- Show Account ID on run dashboard
- Added new Run error state called “Unresolved auth”

* Added changeset

* Remove @internal from TriggerIntegration public methods

* Add void to the result union

* DynamicTriggers now work with the new BYO auth system, and added a bunch of docs and docs changes

* Add additional key material for registering dynamic trigger task

* Add new define* instance methods to the overview
2023-09-22 08:54:31 -07:00

75 lines
1.8 KiB
TypeScript

import type { Integration, RunConnection } from "@trigger.dev/database";
import type { ConnectionAuth } from "@trigger.dev/core";
import type { ConnectionWithSecretReference } from "~/services/externalApis/integrationAuthRepository.server";
import { integrationAuthRepository } from "~/services/externalApis/integrationAuthRepository.server";
export type ResolvableRunConnection = RunConnection & {
integration: Integration;
connection: ConnectionWithSecretReference | null;
};
export async function resolveRunConnections(
connections: Array<ResolvableRunConnection>
): Promise<{ auth: Record<string, ConnectionAuth>; success: boolean }> {
let allResolved = true;
const result: Record<string, ConnectionAuth> = {};
for (const connection of connections) {
if (connection.integration.authSource !== "HOSTED") {
continue;
}
const auth = await resolveRunConnection(connection);
if (!auth) {
allResolved = false;
continue;
}
result[connection.key] = auth;
}
return { auth: result, success: allResolved };
}
export async function resolveRunConnection(
connection: ResolvableRunConnection
): Promise<ConnectionAuth | undefined> {
if (!connection.connection) {
return;
}
const response = await integrationAuthRepository.getCredentials(connection.connection);
if (!response) {
return;
}
return {
type: "oauth2",
scopes: response.scopes,
accessToken: response.accessToken,
};
}
export async function resolveApiConnection(
connection?: ConnectionWithSecretReference
): Promise<ConnectionAuth | undefined> {
if (!connection) {
return;
}
const response = await integrationAuthRepository.getCredentials(connection);
if (!response) {
return;
}
return {
type: "oauth2",
scopes: response.scopes,
accessToken: response.accessToken,
};
}