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

5.8 KB
Raw Blame
import { isNil, spreadIfDefined } from '@activepieces/core-utils'
import { McpOAuthClient } from '@activepieces/shared'
import { FastifyBaseLogger, FastifyReply } from 'fastify'
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'
import { z } from 'zod'
import { securityAccess } from '../../../core/security/authorization/fastify-security'
import { domainHelper } from '../../../helper/domain-helper'
import { mcpOAuthClientAuth } from '../client/mcp-oauth-client-auth'
import { mcpOAuthCodeService } from '../code/mcp-oauth-code.service'
import { DEFAULT_MCP_OAUTH_SCOPES } from '../mcp-oauth-scopes'
import { mcpOAuthValidation } from '../mcp-oauth-validation'
import { mcpOAuthOidcService } from '../oidc/mcp-oauth-oidc.service'
import { mcpOAuthTokenService, OAuthTokenError } from './mcp-oauth-token.service'

export const mcpOAuthTokenController: FastifyPluginAsyncZod = async (app) => {

    app.post('/token', TokenRequest, async (req, reply) => {
        const { grant_type, client_id } = req.body
        const authorizationHeader = req.headers.authorization
        void reply.header('Cache-Control', 'no-store').header('Pragma', 'no-cache')

        try {
            if (grant_type === 'authorization_code') {
                return await handleAuthorizationCode({ authorizationHeader, body: req.body, reply, issuer: domainHelper.getPublicUrlFromRequest({ req }) })
            }
            if (grant_type === 'refresh_token') {
                return await handleRefreshToken({ authorizationHeader, body: req.body, reply, log: req.log })
            }
            return await reply.status(400).send({ error: 'unsupported_grant_type' })
        }
        catch (e) {
            if (e instanceof OAuthTokenError) {
                return reply.status(400).send({
                    error: e.errorCode,
                    error_description: e.errorDescription,
                })
            }
            req.log.error({ error: e, clientId: client_id }, 'OAuth token error')
            return reply.status(500).send({ error: 'server_error' })
        }
    })
}

async function authenticateClient({ authorizationHeader, body, reply }: HandlerParams): Promise<McpOAuthClient | null> {
    const result = await mcpOAuthClientAuth.authenticate({
        authorizationHeader,
        clientId: body.client_id,
        clientSecret: body.client_secret,
    })
    if (result.status === 'anonymous') {
        await reply.status(400).send({ error: 'invalid_request', error_description: 'Missing client_id' })
        return null
    }
    if (result.status === 'error') {
        await reply.status(400).send(result.payload)
        return null
    }
    return result.client
}

async function handleAuthorizationCode({ authorizationHeader, body, reply, issuer }: AuthorizationCodeParams): Promise<void> {
    const { code, code_verifier, redirect_uri } = body
    if (!code || !code_verifier || !redirect_uri) {
        await reply.status(400).send({ error: 'invalid_request', error_description: 'Missing code, code_verifier, or redirect_uri' })
        return
    }

    const client = await authenticateClient({ authorizationHeader, body, reply })
    if (isNil(client)) return

    const authCode = await mcpOAuthCodeService.consume({ code, clientId: client.clientId, redirectUri: redirect_uri })
    if (isNil(authCode)) {
        await reply.status(400).send({ error: 'invalid_grant', error_description: 'Invalid or expired authorization code' })
        return
    }

    const scopes = authCode.scopes ?? DEFAULT_MCP_OAUTH_SCOPES
    const tokens = await mcpOAuthTokenService.exchangeCode({
        redirectUris: client.redirectUris,
        codeVerifier: code_verifier,
        codeChallenge: authCode.codeChallenge,
        codeChallengeMethod: authCode.codeChallengeMethod,
        clientId: client.clientId,
        userId: authCode.userId,
        projectId: authCode.projectId,
        platformId: authCode.platformId,
        scopes,
    })
    const idToken = await mcpOAuthOidcService.issueIdToken({
        userId: authCode.userId,
        platformId: authCode.platformId,
        clientId: client.clientId,
        scopes,
        nonce: authCode.nonce,
        issuer,
    })

    await reply.status(200).send({ ...tokens, ...spreadIfDefined('id_token', idToken) })
}

async function handleRefreshToken({ authorizationHeader, body, reply, log }: RefreshTokenParams): Promise<void> {
    const { refresh_token } = body
    if (!refresh_token) {
        await reply.status(400).send({ error: 'invalid_request', error_description: 'Missing refresh_token' })
        return
    }

    const client = await authenticateClient({ authorizationHeader, body, reply })
    if (isNil(client)) return

    const tokens = await mcpOAuthTokenService.refreshAccessToken({
        redirectUris: client.redirectUris,
        refreshToken: refresh_token,
        clientId: client.clientId,
        log,
    })

    await reply.status(200).send(tokens)
}

type HandlerParams = {
    authorizationHeader: string | undefined
    body: TokenRequestBody
    reply: FastifyReply
}

type RefreshTokenParams = HandlerParams & {
    log: FastifyBaseLogger
}

type AuthorizationCodeParams = HandlerParams & {
    issuer: string
}

const tokenRequestSchema = z.object({
    grant_type: z.string().max(64),
    code: z.string().max(128).optional(),
    client_id: z.string().max(64).optional(),
    client_secret: z.string().max(512).optional(),
    code_verifier: z.string().max(256).optional(),
    redirect_uri: mcpOAuthValidation.storableText(2048).optional(),
    refresh_token: z.string().max(512).optional(),
    resource: z.string().max(2048).optional(),
})

const TokenRequest = {
    config: { security: securityAccess.public() },
    schema: {
        hide: true,
        body: tokenRequestSchema,
    },
}

type TokenRequestBody = z.infer<typeof tokenRequestSchema>