Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

7.5 KB
Raw Blame
import { ActivepiecesError, ErrorCode, isNil, tryCatch, tryCatchSync } from '@activepieces/core-utils'
import { OAuth2AuthorizationMethod } from '@activepieces/pieces-framework'
import { safeHttp } from '@activepieces/server-utils'

const REQUEST_TIMEOUT_MS = 10_000

async function discoverAndRegister({ serverUrl, redirectUrl }: DiscoverAndRegisterParams): Promise<DiscoveredOAuth2Client> {
    const authorizationServer = await resolveAuthorizationServer(serverUrl)
    const metadata = await fetchAuthorizationServerMetadata(authorizationServer)

    if (isNil(metadata.registration_endpoint)) {
        throw invalidConnection(`${authorizationServer} does not support Dynamic Client Registration (no registration_endpoint in its metadata). Provide a Client ID and Client Secret from the server's own documentation instead.`)
    }

    const requestedAuthMethod = pickTokenEndpointAuthMethod(metadata.token_endpoint_auth_methods_supported)
    const client = await registerClient({
        registrationEndpoint: metadata.registration_endpoint,
        redirectUrl,
        scope: (metadata.scopes_supported ?? []).join(' '),
        tokenEndpointAuthMethod: requestedAuthMethod,
    })

    if (isNil(client.client_secret) || client.client_secret.length === 0) {
        throw invalidConnection(`${authorizationServer} issues public OAuth2 clients (no client secret), which is not supported yet. Provide a Client ID and Client Secret manually instead.`)
    }

    const registeredAuthMethod = client.token_endpoint_auth_method ?? requestedAuthMethod
    const authorizationMethod = TOKEN_ENDPOINT_AUTH_METHODS[registeredAuthMethod]
    if (isNil(authorizationMethod)) {
        throw invalidConnection(`${authorizationServer} registered the client with token_endpoint_auth_method "${registeredAuthMethod}", which is not supported. Provide a Client ID and Client Secret manually instead.`)
    }

    const resource = canonicalResourceUri(serverUrl)
    return {
        authUrl: withResourceIndicator({ authorizationEndpoint: metadata.authorization_endpoint, resource }),
        tokenUrl: metadata.token_endpoint,
        scopes: (metadata.scopes_supported ?? []).join(' '),
        clientId: client.client_id,
        clientSecret: client.client_secret,
        resource,
        authorizationMethod,
    }
}

function pickTokenEndpointAuthMethod(supported: string[] | undefined): TokenEndpointAuthMethod {
    const methods = supported ?? ['client_secret_basic']
    if (methods.includes('client_secret_basic')) {
        return 'client_secret_basic'
    }
    if (methods.includes('client_secret_post')) {
        return 'client_secret_post'
    }
    return 'client_secret_basic'
}

// An MCP server that skips RFC 9728 is its own authorization server.
async function resolveAuthorizationServer(serverUrl: string): Promise<string> {
    const url = parseUrl(serverUrl)
    const resourcePath = url.pathname === '/' ? '' : url.pathname
    const metadata = await fetchFirst<ProtectedResourceMetadata>([
        `${url.origin}/.well-known/oauth-protected-resource${resourcePath}`,
        `${url.origin}/.well-known/oauth-protected-resource`,
    ])
    return metadata?.authorization_servers?.[0] ?? url.origin
}

async function fetchAuthorizationServerMetadata(authorizationServer: string): Promise<AuthorizationServerMetadata> {
    const issuer = parseUrl(authorizationServer)
    const path = issuer.pathname === '/' ? '' : issuer.pathname.replace(/\/$/, '')
    const candidates = [
        // RFC 8414: the well-known suffix goes between the host and the issuer's path.
        `${issuer.origin}/.well-known/oauth-authorization-server${path}`,
        // OpenID Connect Discovery 1.0: the opposite order — suffix appended after the path.
        `${issuer.origin}${path}/.well-known/openid-configuration`,
    ]
    const metadata = await fetchFirst<AuthorizationServerMetadata>(candidates)
    if (isNil(metadata) || isNil(metadata.authorization_endpoint) || isNil(metadata.token_endpoint)) {
        throw invalidConnection(`Could not discover OAuth2 endpoints for ${authorizationServer}. Tried ${candidates.join(' and ')}. If this server is on a private network, add its host to AP_SSRF_ALLOW_LIST.`)
    }
    return metadata
}

async function fetchFirst<T>(candidates: string[]): Promise<T | null> {
    for (const url of candidates) {
        const { data: response, error } = await tryCatch(() => safeHttp.axios.get<T>(url, { timeout: REQUEST_TIMEOUT_MS }))
        if (isNil(error) && !isNil(response)) {
            return response.data
        }
    }
    return null
}

async function registerClient({ registrationEndpoint, redirectUrl, scope, tokenEndpointAuthMethod }: RegisterClientParams): Promise<ClientRegistrationResponse> {
    const { data: response, error } = await tryCatch(() => safeHttp.axios.post<ClientRegistrationResponse>(registrationEndpoint, {
        client_name: 'Activepieces',
        redirect_uris: [redirectUrl],
        grant_types: ['authorization_code', 'refresh_token'],
        response_types: ['code'],
        token_endpoint_auth_method: tokenEndpointAuthMethod,
        ...(scope.length > 0 ? { scope } : {}),
    }, {
        headers: { 'content-type': 'application/json' },
        timeout: REQUEST_TIMEOUT_MS,
    }))
    if (!isNil(error) || isNil(response)) {
        throw invalidConnection(`Dynamic Client Registration failed at ${registrationEndpoint}: ${error instanceof Error ? error.message : String(error)}`)
    }
    return response.data
}

function withResourceIndicator({ authorizationEndpoint, resource }: WithResourceIndicatorParams): string {
    const url = new URL(authorizationEndpoint)
    url.searchParams.set('resource', resource)
    return url.toString()
}

function canonicalResourceUri(serverUrl: string): string {
    const url = parseUrl(serverUrl)
    return `${url.origin}${url.pathname}`.replace(/\/$/, '')
}

function parseUrl(url: string): URL {
    const { data: parsed, error } = tryCatchSync(() => new URL(url))
    if (!isNil(error) || isNil(parsed)) {
        throw invalidConnection(`"${url}" is not a valid URL.`)
    }
    return parsed
}

function invalidConnection(error: string): ActivepiecesError {
    return new ActivepiecesError({
        code: ErrorCode.INVALID_APP_CONNECTION,
        params: { error },
    })
}

const TOKEN_ENDPOINT_AUTH_METHODS: Partial<Record<string, OAuth2AuthorizationMethod>> = {
    client_secret_basic: OAuth2AuthorizationMethod.HEADER,
    client_secret_post: OAuth2AuthorizationMethod.BODY,
}

export const oauth2Discovery = { discoverAndRegister }

type TokenEndpointAuthMethod = 'client_secret_basic' | 'client_secret_post'

type DiscoverAndRegisterParams = {
    serverUrl: string
    redirectUrl: string
}

type RegisterClientParams = {
    registrationEndpoint: string
    redirectUrl: string
    scope: string
    tokenEndpointAuthMethod: TokenEndpointAuthMethod
}

type WithResourceIndicatorParams = {
    authorizationEndpoint: string
    resource: string
}

export type DiscoveredOAuth2Client = {
    authUrl: string
    tokenUrl: string
    scopes: string
    clientId: string
    clientSecret: string
    resource: string
    authorizationMethod: OAuth2AuthorizationMethod
}

type ProtectedResourceMetadata = {
    authorization_servers?: string[]
}

type AuthorizationServerMetadata = {
    authorization_endpoint: string
    token_endpoint: string
    registration_endpoint?: string
    scopes_supported?: string[]
    token_endpoint_auth_methods_supported?: string[]
}

type ClientRegistrationResponse = {
    client_id: string
    client_secret?: string
    token_endpoint_auth_method?: string
}