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

13.7 KB
Raw Blame

icon: 🔗

App Connections

Encrypted credential records (OAuth2 tokens, API keys, basic/custom auth, OIDC props) that flow steps use to call external services. Support automatic OAuth2 refresh with distributed locking, a project-or-platform scope model, and a project-scoped "replace" that rewires flow references from one connection to another.

Entity

AppConnection: id, displayName, externalId (stable ref in flow settings, survives rename), type, status (ACTIVE/EXPIRED/ERROR), value (encrypted AES-256), platformId, pieceName/Version, projectIds[], scope (PROJECT/PLATFORM), preSelectForNewProjects.

Connection types (8)

OAUTH2, CLOUD_OAUTH2 (exchanged via secrets.activepieces.com), PLATFORM_OAUTH2 (platform-managed OAuth app), SECRET_TEXT, BASIC_AUTH, CUSTOM_AUTH (opt-in refresh callback), NO_AUTH, OIDC.

How it works

  • OAuth2 auto-refresh on retrieval: lockAndRefreshConnection() refreshes 15 min early; acquires Redis lock keyed ${platformId}_${externalId} (60s) so projects sharing a connection serialize; re-encrypts tokens; sets status ERROR on invalid refresh. API responses always strip refresh_token + client_secret.
  • Custom-auth refresh: piece defines a refresh.generate callback; token_refresh_at = now + expiresIn - min(15min, expiresIn/2); dispatched via EXECUTE_TOKEN_REFRESH worker job. Support cached in pieceRefreshSupportCache (LRU 500, 5-min TTL). Timeout keeps old creds (no ERROR); engine error → ERROR.
  • OIDC: AP acts as an OIDC identity provider so pieces get short-lived cloud creds (e.g. AWS AssumeRoleWithWebIdentity). Engine calls POST /api/v1/worker/oidc-token with {audience, expiresInSeconds?} → RS256 JWT sub: platform:{id}:project:{id}, TTL default/cap 1h. Public discovery: /.well-known/openid-configuration + /jwks.json; kid is an RFC 7638 SHA-256 thumbprint. Signing key auto-generated + persisted (encrypted) to the shared flag table with first-writer-wins (INSERT ... ON CONFLICT DO NOTHING), no env var needed.

Endpoints

POST /v1/app-connections (upsert, validates via worker EXECUTE_VALIDATION), POST /:id (update meta), GET (filters), GET /owners, POST /replace, DELETE /:id, POST /oauth2/authorization-url (optional scope subset).

Gotchas

  • Deleting a PLATFORM-scope connection via the project route is rejected 403 — delete those via platform admin DELETE /v1/global-connections/:id.

  • Replace: platform/global connections can be the source, but deleteSourceConnection on a platform source → 403; deleting a project source while a published version still references it → 409. Draft versions always updated; published only when requested.

  • Deleting a connection does NOT cascade to flows; they fail at runtime with a validation error.

  • Global (platform-scope) connections require globalConnectionsEnabled; bulk-delete in the project UI skips them client-side.

  • AP_ENFORCE_CONNECTION_PIECE_BINDING (default false) makes a step resolve only connections whose pieceName equals the step's own piece; a mismatch raises a USER-level ConnectionPieceMismatchError. The check lives in the engine's connection-resolver, not the worker endpoint. Set the var on the app container — the engine cannot read process.env (sandbox env is an allowlist), so the flag rides WorkerSettings → SandboxSettings → sandbox env, the same path as AP_DEV_PIECES. Code / loop / router steps have no piece, so a missing name is a denial — they lose connection access entirely, and enabling the flag breaks flows that feed a connection into custom JS.

  • metadata.accountIdentifier (the "which account is this" label) must be rewritten on every upsert, never left untouched — spreadIfDefined omits the column and TypeORM upsert(connection, ['id']) then leaves the old value in place, so a reconnect that fails to resolve would keep labelling the connection with an account it no longer authenticates as. mergeConnectionMetadata also strips the key from caller-supplied metadata, because metadata is a caller-owned jsonb bag: without that, any WRITE_APP_CONNECTION holder can forge the label. Note POST /:id (update) still replaces the whole bag.

  • A pasted service-account JSON is attacker-controlled config, and token_uri inside it is an SSRF vector. A Google service-account key file carries its own token_uri/auth_uri, and google-auth-library (GoogleAuth, JWT) honours them — so { ...JSON.parse(raw) } handed to googleAuthOptions.credentials lets whoever pasted the file redirect the OAuth token exchange to any host, link-local metadata included, behind a valid-looking type: service_account. Forward only the fields you use (client_email, private_key, project_id if you check it); never spread the parsed object. This bit the Vertex AI provider in review, and packages/pieces/community/google-vertexai/.../common.ts still does { ...raw, private_key } — worse there than in a provider, because a piece connection needs only connection-write, not platform admin. The same new GoogleAuth/new JWT shape recurs across the google-* pieces, so grep before assuming one is narrow. Note .claude/rules/safe-http.md does not catch this: the request is made inside the auth library, never through safeHttp.axios, so the rule's "admin config reaching outbound HTTP" clause has to be applied by hand.

  • Never hand a Google OAuth2Client the whole OAuth2 connection value. Pass setCredentials({ access_token }) only. The client is built with no client ID or secret (setCredentials ignores any client_id in the value), but the value carries a refresh_token and no expiry_date. In google-auth-library 10.x that keeps the retry-on-401/403 path open: the library tries its own refresh, sends only {refresh_token, grant_type}, and Google's 400 invalid_request "Could not determine client ID from request." replaces the real 401/403. This hits every OAuth2 flavour, and the library refresh gains nothing, because the server refreshes 15 minutes before expiry (oauth2Util.isExpired). Every Google piece's createGoogleClient follows this, and each has test/oauth2-client.test.ts asserting it; a new Google piece should copy that test. #11529 fixed only Slides, and #15648 put the bug back, which is why each piece now has the test. Upstream report: #16268.

  • The two live OAuth2 redirect pages disagree on percent-decoding, so getCode decodes conditionally. Do not collapse it back to an unconditional decode, in either direction. redirect.tsx decodes once via URLSearchParams, while https://secrets.activepieces.com/redirect - the page every CLOUD_OAUTH2 connect uses, and which does not live in this repo - posts the code raw, zero decodes (verified 2026-09-08: ?code=k1%2Fk2 posts 'k1%2Fk2'). getCode decodes only when oauth2Type === AppConnectionType.CLOUD_OAUTH2 - the same condition oauth2Utils.resolveRedirectUrl uses to pick the secrets page - falling back to the raw value if decodeURIComponent throws. The two live in separate files and nothing ties them together, so a third redirect page must be added to both: pick its URL in resolveRedirectUrl and state its decode contract in getCode, or the pair drifts and you are back at this bug. The Mustache page in app.ts is a third sender on paper only: setupApp mounts under the /api prefix, so it serves at /api/redirect, nothing in the repo points at it, and a bare /redirect always falls through to the SPA. Deleting the decode outright was tried in #14879 and reverted 21 hours later in #14926, because it broke every managed OAuth app, self-hosted as much as Cloud: CLOUD_OAUTH2 means "managed app", not "on Cloud", and platform.cloudAuthEnabled defaults to true everywhere. It also fired much more readily than the bug it fixed, since a raw posted code breaks on any /, + or = while the double decode needs a literal %. Grepping this repo for the senders is not enough; the cloud one is invisible here - and neither are tests, by themselves: #14879 shipped three of them, CI does run web tests (ci.yml, --filter=web), and they passed, because all three modelled redirect.tsx's contract only. oauth2-authorization-code-decode.test.ts now covers both contracts, and fails in both directions. See decision The browser normalises OAuth2 authorization codes, because the cloud redirect contract is frozen.

  • An OAuth2 connection with no authorization_method sends its client credentials in the form body (client_secret_post) on both claim and refresh. credentialsOauth2Service defaults to OAuth2AuthorizationMethod.BODY, and the value is set either from the piece's auth.authorizationMethod or by discovery. Any flow that registers its own client must carry back the token_endpoint_auth_method the server registered. Otherwise strict servers (Keycloak, Hydra, Spring AS) reject the token call with invalid_client. Dynamic Client Registration (000039) does this: oauth2Discovery maps client_secret_basic → HEADER and client_secret_post → BODY, then returns the result as discovered.authorization_method. Server code imports OAuth2AuthorizationMethod from @activepieces/pieces-framework. The copy in @activepieces/shared is not exported from its index, so importing it from there gives undefined at runtime.

  • Adding an optional field to an AppConnectionValue shape (e.g. BaseOAuth2ConnectionValue) needs no migration and is safe in both rolling-deploy directions, but confirm each of these rather than assuming it (checked in full when RFC 8707's resource was added, 000039): (1) value is one EncryptedObject column (AppConnectionSchema.value: EncryptedObject) — an encrypted JSON blob, not per-field DB columns, so a new key is purely additive at the application layer. (2) The upsert/response zod schemas here don't use .strict(), so an old server silently drops a field a newer client sends, and a new server sees undefined for a field an old client never sends — neither direction throws. (3) removeRefreshTokenAndClientSecret redacts via spread-and-overwrite ({ ...connection.value, refresh_token: '(REDACTED)' }), not an explicit field allowlist, so a new field survives redaction automatically — check this specifically if you ever rewrite it as an allowlist. (4) removeSensitiveData strips the entire value object as one unit before an API response, so a new field gets the same blanket treatment as everything else in value — no per-field decision needed. (5) BaseOAuth2ConnectionValue is defined twice, independently, in packages/core/piece-types (piece-facing context.auth) and packages/core/shared (what the server actually persists) — they've already diverged (authorization_method exists only on the shared one), and the two packages can't share the type (core-piece-types may only import core-utils). Check which one a given call site actually resolves (grep -rn 'export type BaseOAuth2ConnectionValue' packages/core/) before editing — the wrong one type-checks in isolation and only fails once the real call site's package rebuilds against it.

Key files

Entry point: appConnectionService, exported from the app-connection service and reached through appConnectionModule, registered in packages/server/api/src/app/app.ts.

  • packages/server/api/src/app/app-connection/ — backend module: controllers (project, platform, worker), entity, module wiring, and the app-connection-service/ folder holding the service, handler, and OAuth2 handlers
  • packages/server/api/src/app/core/security/oidc/ — OIDC provider: key manager, token controller, discovery controller, module
  • packages/core/shared/src/lib/automation/app-connection/ — shared types, enums, value unions, and the upsert/read DTOs under dto/
  • packages/web/src/features/connections/ — frontend slice: api/ clients, hooks/ TanStack Query hooks, components/ global and rename dialogs, utils/ OAuth2 redirect and name-uniqueness helpers
  • packages/web/src/app/connections/ — connection dialogs and per-auth-type form settings (new, create/edit, replace, reconnect, OIDC, OAuth2, custom, basic, secret text)
  • packages/web/src/app/routes/connections/ — project connections list page
  • packages/web/src/app/routes/platform/setup/connections/ — platform-wide global connections page

Paths verified 2026-07-17.

Gotchas

  • needRefresh looks the piece up by connection.pieceVersion, so a CUSTOM_AUTH connection whose stored version is no longer served fails as ConnectionNotFound, not as a version error. Any 404 from GET /v1/worker/app-connections/:externalId is mapped by the engine to "connection not found", which points debugging at credentials. On a dev instance this happens the moment a dev piece's version is bumped; in production it would need a published version to disappear from the registry. The lookup result is cached per piece version in pieceRefreshSupportCache.
  • status: ACTIVE only means the credentials validated when the connection was saved. Runs never re-check it. After an out-of-band password rotation (e.g. a customer vault like CyberArk rotating a service account), the connection still shows as connected while every step fails with the provider's own auth error. Recheck (POST /v1/app-connections/:id/revalidate) re-runs the piece's validate and flips it to ERROR. Custom-auth props reach the piece unchanged on both the save and run paths. Secret-manager resolution only touches {{<id>|ap_sep_v1|<path>}} strings. So a provider auth error (Oracle ORA-01017) means the provider rejected exactly what was typed or stored. Reproduced on 0.91.0 with oracle-database 0.1.15, 2026-09-29.