Open MercatoDocsSearch docsAPI reference

Framework

OAuth2 Grants for Integrations

Connect an organization to an external service once and let its authorized users work through that grant. The provider adapter defines how to exchange and refresh tokens, while the callback must return to the organization that initiated the connection.

The integrations module ships a core for tenant-level OAuth2 authorization-code grants: one grant per Open Mercato organization and integration. A provider module or package that needs delegated access with a refresh token builds on it instead of writing its own token storage, refresh, PKCE or locking code.

The core guarantees:

The core owns the protocol helpers and the grant service. The provider owns its Provider Descriptor, its initiate and callback routes, its detail-page tab and its health check. No first-party integration uses the core yet.

Terms

TermMeaning
Grant Owner{ integrationId, tenantId, organizationId } (OAuthGrantOwner). Grants are per organization; userId is always absent or null. In a bundle, the grant belongs to the child integration.
Client ConfigurationThe OAuth app registration the admin copies from the provider console: the integration's credentials with clientId, clientSecret and an optional scopes (space- or comma-separated; blank means the descriptor's defaultScopes). Read child-then-bundle, like every credential.
Provider DescriptorA code constant declaring the provider's endpoints, client authentication, PKCE mode and scopes (OAuthProviderDescriptor). Passed to every grant-service call; there is no global registry.
Grant Statusactive or invalidated. A read reports the Grant Read Status: active, invalidated, unavailable (the grant can't be read, possibly a KMS outage) or none.
Connection StateWhat the provider tab shows, in precedence order: not_configured > not_connected > unavailable > invalidated > active. A missing production APP_URL is the separate flag baseUrlMissing.
Grant RevisionrevisedAt, moved by Connect and by an External Account selection, never by Refresh. It is the optimistic-lock version of updateProviderData and disconnect.
External AccountThe provider-side account the grant is used against (an organization in an accounting system, a mailbox), chosen after consent and stored as the provider-owned, non-secret providerData.

Public import paths

Only these paths are a contract. Every export carrying @internal JSDoc in them, and every other file under lib/oauth/, is an implementation detail and may change without notice.

Import pathUse
@open-mercato/core/modules/integrations/lib/oauth/descriptorOAuthProviderDescriptor, OAuthGrantOwner, OAuthGrantError, OAuthDescriptorError and the closed unions (OAuthTokenFailure, OAuthConnectFailure, OAuthConnectionState, OAuthUnavailableReason, …)
…/lib/oauth/pkcecreatePkcePair()
…/lib/oauth/authorizationbuildAuthorizationUrl(descriptor, input), exchangeAuthorizationCode(descriptor, client, input)
…/lib/oauth/redirectresolveOAuthRedirectUri(req, path)
…/lib/oauth/resource-challengeclassifyResourceChallenge(status, wwwAuthenticate)
…/lib/oauth/token-endpointOAuthTokenEndpointResponse, OAuthClientAuthMethod
…/lib/oauth/grant-serviceIntegrationOAuthGrantService and its result types (OAuthAccessToken, OAuthConnectResult, OAuthGrantInspection, OAuthDisconnectResult, OAuthLastDisconnect)
…/lib/oauth/healthmapInspectionToHealth(inspection), OAUTH_HEALTH_CODES, OAuthHealthCode
…/lib/oauth/testing/fakeAuthorizationServerstartFakeAuthorizationServer(options) for Jest suites
@open-mercato/core/helpers/integration/oauthGrantTestRoutePlaywright helpers for the test-only route (see Testing)
@open-mercato/core/modules/communication_channels/lib/oauth-statecreateOAuthState, verifyOAuthState, consumeOAuthStateOnce, OAuthStateConsumeStore, OAuthStateError (its code values are additive-only, replay included)
@open-mercato/shared/lib/db/advisoryLockwithAdvisoryXactLock, AdvisoryLockUnavailableError, AdvisoryLockUnavailableReason, AdvisoryLockOptions (the lock helper the grant service uses)

Resolve the grant service from DI as integrationOAuthGrantService; never build it yourself. Every closed union is additive-only: keep a default branch when you switch over one. Branch on OAuthGrantError.code, .reason and .providerErrorCode, never on the message. instanceof checks against OAuthGrantError, OAuthDescriptorError, OAuthTokenEndpointError and AdvisoryLockUnavailableError hold even when two copies of their module are loaded in one process.

Provider Descriptor

import type { OAuthProviderDescriptor } from '@open-mercato/core/modules/integrations/lib/oauth/descriptor'

export const myProviderDescriptor: OAuthProviderDescriptor = {
  integrationId: 'my_provider',
  authorizationEndpoint: 'https://auth.example.com/oauth/authorize',
  tokenEndpoint: 'https://auth.example.com/oauth/token',
  revocationEndpoint: 'https://auth.example.com/oauth/revoke',
  clientAuthMethod: 'client_secret_basic',
  pkce: 'S256',
  defaultScopes: ['accounting.read', 'offline_access'],
}
FieldDefaultNotes
integrationIdrequiredMust equal the Grant Owner's integrationId.
authorizationEndpoint, tokenEndpointrequiredAbsolute https: URLs without credentials or fragment. http: is accepted only on a loopback host, outside production or with the test flag on.
revocationEndpoint – RFC 7009. Without it a Disconnect erases locally and logs oauth_revocation_unsupported.
clientAuthMethodclient_secret_basicOr client_secret_post.
pkceS256none only for a provider that rejects PKCE.
defaultScopesrequiredUsed when the Client Configuration sets no scopes.
extraAuthorizeParams{}Appended to the authorization URL; never overrides a protocol parameter.
requiresRefreshTokentrueChecked at Connect only.
defaultAccessTokenTtlSec3600Used when the token response has no expires_in. Either value is clamped to 1 s – 1 year.
refreshSkewMs120000The default minValidityMs of getAccessToken, capped at half of each token's lifetime, so tokens that live shorter than the skew are not refreshed on every call. An explicit minValidityMs is not capped.

Every descriptor-taking call validates the whole descriptor and the owner before any I/O and throws OAuthDescriptorError (a programming error, not a Connect Failure) when either is malformed.

Connect: the initiate and callback routes

The provider writes two routes on the core helpers. The core never sees the request, so the route contract below is normative.

Initiate (an authenticated route behind a Connect or Reconnect button):

  1. Apply the organization resolution order.
  2. Read the Client Configuration with integrationCredentialsService.resolve(integrationId, scope). Without a clientId and clientSecret the Connect fails with client_misconfigured.
  3. Build the redirect URI with resolveOAuthRedirectUri(req, callbackPath).
  4. Create a PKCE pair with createPkcePair().
  5. Create the state with createOAuthState({ userId, tenantId, organizationId, providerKey, extra: { codeVerifier, replaceUnreadable } }) and set its cookie on an HttpOnly, SameSite=Lax cookie with a provider-specific name.
  6. Redirect to buildAuthorizationUrl(descriptor, { clientId, redirectUri, state: stateParam, scopes, codeChallenge }).

Callback (the provider redirects the admin's browser here):

const callbackStateExtraSchema = z.object({
  codeVerifier: z.string().min(43),
  replaceUnreadable: z.boolean().optional(),
})

const payload = verifyOAuthState({
  cookie: stateCookie,
  expectedUserId: auth.sub,
  expectedProviderKey: 'my_provider',
  expectedState: url.searchParams.get('state') ?? undefined,
})
await consumeOAuthStateOnce(container.resolve<OAuthStateConsumeStore>('cache'), payload)
const extra = callbackStateExtraSchema.parse(payload.extra)
if (!payload.organizationId) throw new OAuthGrantError('connect_state_invalid')
const owner = { integrationId: 'my_provider', tenantId: payload.tenantId, organizationId: payload.organizationId }
const tokenResponse = await exchangeAuthorizationCode(myProviderDescriptor, { clientId, clientSecret }, {
  code,
  redirectUri: resolveOAuthRedirectUri(req, callbackPath),
  codeVerifier: extra.codeVerifier,
})
await integrationOAuthGrantService.completeConnect(myProviderDescriptor, owner, tokenResponse, {
  clientId,
  requestedScopes: scopes,
  replaceUnreadable: extra.replaceUnreadable === true,
  actorUserId: auth.sub,
})

clientId, clientSecret and scopes come from the owner's Client Configuration. Around these calls the callback checks the state's tenant and the user's access (rule 2), runs the mutation guards (rule 1) and maps every failure to a Connect Failure code for the redirect back to the provider tab. completeConnect updates the live grant in place (Reconnect) or inserts one, clears providerData (a non-empty value moves to previousProviderData) and returns { reconnect, replacedUnreadable, previousProviderData, revisedAt }.

Connect FailureWhen
connect_cancelledThe provider returned error=access_denied.
connect_state_invalidA missing, expired, replayed or mismatched state; a state store that can't record the single use; a missing PKCE verifier; a state tenant or user access that no longer matches.
connect_exchange_failedThe code exchange failed, or no refresh_token arrived while requiresRefreshToken (except on a Reconnect over an active grant with the same clientId, which keeps the stored one).
connect_grant_unreadableA plain Reconnect over a grant whose envelope doesn't decrypt or validate.
connect_persist_failedThe exchange succeeded but the grant couldn't be stored (lock deadline, no encryption key, database).
client_misconfiguredinvalid_client / unauthorized_client, or no Client Configuration at initiate.
organization_scope_requiredNo organization could be resolved.
oauth_base_url_not_configured, connect_origin_rejectedNo APP_URL in production, or a request from an origin that isn't an allowed app origin.

A failed Connect never changes an existing grant. Tokens it obtained are discarded, not revoked, because revoking could end a provider authorization another grant shares.

Provider route contract

Every provider implementation MUST:

  1. Require integrations.credentials.manage on initiate, callback, External Account save and Disconnect. Run the registry mutation guards with runRouteMutationGuards (@open-mercato/shared/lib/crud/route-mutation-guard) with resourceKind: 'integrations.oauth_grant' and operation custom for the callback, update for External Account save and delete for Disconnect; return its response when blocked and call runAfterSuccess() only after the grant write committed. On initiate, External Account save and Disconnect, resolve the organization in the order below.
  2. Bind the state to the Grant Owner: the callback takes the owner from the verified state, never from the current organization selection, and checks that the state's tenant is the session's tenant and that the user still holds integrations.credentials.manage in the state's organization (connect_state_invalid otherwise).
  3. Verify the state with expectedProviderKey and expectedState set, then, before the code exchange, call consumeOAuthStateOnce with the request container's cache. A replayed state, a cache that can't be resolved or a failing store call is connect_state_invalid (fail closed).
  4. Store the PKCE verifier in the state extra and pass it to exchangeAuthorizationCode.
  5. Use a provider-specific cookie name, never the hub's om_cc_oauth_state.
  6. Build the redirect URI with resolveOAuthRedirectUri(req, path) in both legs, passing the request.
  7. On External Account save and Disconnect, carry the Grant Revision: the tab sends buildOptimisticLockHeader(revisedAt), the route reads it with readOptimisticLockExpected(req) and passes it as expectedRevisedAt, and returns the grant service's standard 409 body.
  8. Show the redirect URI on the tab with a copy button, and disable Connect and Reconnect while inspectGrant().baseUrlMissing.
  9. Not declare defaultState.isEnabled: true until the integration-state upsert defaults fix (#6915) lands; otherwise the first health run, or any state write without isEnabled, silently disables the integration.
  10. In state unavailable, offer "Replace unreadable connection" behind a confirm dialog only when inspectGrant reports unavailableReason: 'unreadable'. Otherwise (no encryption key, an unopened field-level layer, or a grant sealed before encryption was switched off) offer "Disconnect anyway" (force: true) behind a confirm dialog saying that the app must also be removed at the provider. Carry replaceUnreadable only in the encrypted state, never as a callback query parameter; every other Connect passes false.
  11. Before updateProviderData, compare the selection with previousProviderData: save a different External Account only after the admin confirms a warning that existing sync cursors and external-id mappings refer to the previous one. With no previousProviderData but existing cursors or mappings (after a replaced connection or a Disconnect), warn that the account can't be verified.

Duties the helpers can't enforce

Organization resolution order

On initiate, External Account save and Disconnect, in this order:

  1. Reject with 403 a super admin acting in another tenant: the auth context carries an actorTenantId that differs from tenantId. Never decide by role name.
  2. Reject with 403 an explicitly empty organization scope: resolveOrganizationScopeForRequest({ container, auth, request }) from @open-mercato/core/modules/directory/utils/organizationScope, then isExplicitlyEmptyOrganizationScope(scope).
  3. Resolve the organization with resolveActiveOrganizationId(auth) (@open-mercato/shared/lib/auth/organizationScope). "All organizations" falls back to the admin's own organization; answer organizationScopeRequiredResponse() (400) only when it returns null.
  4. Reject with 403 a user without integrations.credentials.manage in that organization (rbacService.userHasAllFeatures, wildcard-aware). The request dispatcher checks the feature in the switcher's organization, which can differ.

The test-only route packages/core/src/modules/integrations/api/post/test-oauth-grants/route.ts follows this order and the mutation-guard rule and can serve as a reference.

Using a token

const grants = container.resolve<IntegrationOAuthGrantService>('integrationOAuthGrantService')
const owner = { integrationId: 'my_provider', tenantId, organizationId }
let token = await grants.getAccessToken(myProviderDescriptor, owner)
let response = await callResource(token.accessToken)
const challenge = classifyResourceChallenge(response.status, response.headers.get('www-authenticate'))
if (response.status === 401 && challenge === null) {
  token = await grants.getAccessToken(myProviderDescriptor, owner, { rejectedAccessToken: token.accessToken })
  response = await callResource(token.accessToken)
}

getAccessToken(descriptor, owner, options?) returns an access token valid for at least minValidityMs (default descriptor.refreshSkewMs) and refreshes it under the Grant Lock when needed. Options: minValidityMs, forceRefresh (for a keep-alive job) and rejectedAccessToken (after a resource 401: one forced refresh, only if the stored token still equals it). A second 401 is a resource error, never a reason to reconnect.

A failure throws OAuthGrantError with one Token Failure code:

CodeMeaningWhat to do
transientNetwork error, timeout, 5xx, 429, a lock deadline or a transient database error.Back off. While the stored token is still valid it is returned with degraded: true instead of throwing (never for a rejectedAccessToken call).
grant_invalidatedThe provider rejected the grant (invalid_grant), or it expired with no refresh token.Stop; an admin must reconnect. The detail page shows the reauth banner.
client_misconfiguredinvalid_client, unauthorized_client, another permanent 4xx, a changed clientId or a missing Client Configuration (reason tells them apart).Stop; an admin fixes the Client Configuration.
not_connectedNo live grant."Connect first", never "reconnect".
platform_unavailableNo encryption key, or the grant can't be decrypted or validated. No token call was made.Fail closed; may be a KMS outage.

Rules for callers:

External Account selection

updateProviderData(descriptor, owner, data, { expectedRevisedAt, actorUserId }) stores the provider-owned, non-secret selection, clears previousProviderData and moves the Grant Revision. It throws not_connected, platform_unavailable or transient, and the platform's 409 for a stale expectedRevisedAt. Apply rule 11 before calling it. Every Connect, including a Reconnect, starts with an empty providerData. For a provider that needs an External Account, the Token Provider works meanwhile, but the adapter refuses to sync until one is chosen.

Disconnect

disconnect(descriptor, owner, { expectedRevisedAt, force, actorUserId }) erases the grant under the lock (5 s waiter deadline), then, after releasing it, revokes the stored refresh token (8 s), unless the owner reconnected meanwhile or the grant was invalidated. If the commit fails with an unknown outcome, it checks whether its oauth_disconnected entry committed and, if so, still revokes. Provider-side cleanup that needs a token (webhooks, subscriptions) belongs before the Disconnect call, with getAccessToken.

Health check

Register a health check service in the provider's DI and return the pure mapping of the inspection:

import type { IntegrationOAuthGrantService } from '@open-mercato/core/modules/integrations/lib/oauth/grant-service'
import { mapInspectionToHealth } from '@open-mercato/core/modules/integrations/lib/oauth/health'

export function createMyProviderHealthCheck({ integrationOAuthGrantService }: { integrationOAuthGrantService: IntegrationOAuthGrantService }) {
  return {
    async check(_credentials: Record<string, unknown> | null, scope: { tenantId: string; organizationId: string }) {
      const owner = { integrationId: 'my_provider', tenantId: scope.tenantId, organizationId: scope.organizationId }
      return mapInspectionToHealth(await integrationOAuthGrantService.inspectGrant(myProviderDescriptor, owner))
    },
  }
}

inspectGrant makes no token call, takes no lock and writes nothing, so the OAuth health check is network-free by design. That is a deliberate exception to the om-integration-builder rule that a health check validates real connectivity: probing the token endpoint would refresh on most probes, compete for the lock and exceed the 10 s health timeout. The inspection carries no token; a provider that wants a real resource call makes it with getAccessToken.

Health codeStatusFrom the inspection, in precedence order
oauth.platform_unavailableunhealthystatus: 'unavailable', or an unreadable Client Configuration
oauth.not_connectedunhealthyno live grant
oauth.invalidatedunhealthystatus: 'invalidated'
oauth.client_changedunhealthythe grant's clientId differs from the Client Configuration
oauth.client_misconfiguredunhealthythe Client Configuration has no client ID or secret, or the last refresh failed with client_misconfigured
oauth.transientdegradedthe last refresh failed with transient
oauth.scope_driftdegradedmissingScopes is not empty
oauth.connectedhealthyotherwise
oauth.scope_insufficientunhealthynever from inspectGrant; only from the provider's own resource call

Detail page: tab, banner and API field

The provider tab's own duties:

Logs

Grant operations write integration log entries under the plain integrationId, never with token material. Connect, External Account selection and Disconnect entries carry the acting user's id; every disconnect entry carries a disconnectId.

Log codeLevel
integrations.oauth_connected, integrations.oauth_external_account_selectedinfo
integrations.oauth_connect_persist_failedwarn
integrations.oauth_invalidatedwarn
integrations.oauth_disconnected + integrations.oauth_revocation_pendinginfo + warn
integrations.oauth_revocation_confirmed, …_unsupported, …_skipped_reconnected, …_skipped_invalidatedinfo
integrations.oauth_revocation_failederror

Grant writes emit neither integrations.credentials.updated nor integrations.state.updated, and there is no grant event or notification yet.

Configuration

Reserved __oauth_grant suffix

A tenant-level grant is stored as an integration_credentials row with the id <integrationId>__oauth_grant. Therefore:

Never write a grant row through integrationCredentialsService: only the grant service may write it, under the Grant Lock. DI overrides of integrationCredentialsService don't apply to grant rows.

Testing

The test-only route, the test_oauth_grant integration and OM_ENABLE_TEST_OAUTH_GRANTS are not contracts, apart from one STABLE testing rule: with the flag on, a descriptor may use loopback http: endpoints, so a provider's test seam can point it at an in-process fake.

Contract summary

The full list and its classes are in BACKWARD_COMPATIBILITY.md (section "OAuth2 Grant Lifecycle Core"). Design background: .ai/specs/2026-09-27-app-spec-oauth2-grant-lifecycle.md and .ai/specs/2026-10-01-oauth2-grant-lifecycle-core.md.

Previous: Integrations & Data Sync · Next: Messages System