OauthTokenExchangeHandler configures a handler for the OAuth 2.0 token exchange flow. It works with an Apex class to validate tokens issued by an external identity provider and map the users represented by those tokens to Salesforce users. The required tokenHandlerApex element identifies that class, which must extend Oauth2TokenExchangeHandler.
The metadata controls enablement, application-specific handler selection, supported token categories, and permission to provision users. The Apex class supplies the validation and user-mapping logic. Keep the handler's API name, its display label, its Apex class name, and the application API names distinct: they serve different purposes.
Key Elements
Boolean elements accept true or false. The application settings below are nested within enablements.
| Element | What the Element Controls |
|---|---|
developerName, masterLabel
|
Required handler API name and display label, respectively. Neither is the value used for the token request's token_handler parameter. |
tokenHandlerApex |
Required reference to the Apex class that validates tokens and maps users. The class must extend Oauth2TokenExchangeHandler. |
isEnabled |
Required enablement flag. Setting it to true does not complete enablement without application settings in enablements. |
enablements |
An array of nested OauthTokenExchHandlerApp settings. Each entry identifies an application, an Apex execution user, and whether this is the application's default handler. |
apexExecutionUser |
Required within each enablement entry. Identifies the user who runs the Apex handler. Salesforce recommends an integration user. |
connectedApp, externalClientApp
|
Identify the application for an enablement entry using its API name. connectedApp references ConnectedApp metadata. |
isDefault |
Required within each enablement entry. Controls selection for that application when a token request omits token_handler. |
isAccessTokenSupported |
Required. Indicates support for OAuth 2.0 access tokens, including opaque tokens and JSON Web Token (JWT)-based access tokens. |
isJwtSupported |
Required. Indicates support for tokens in JWT format, including JWT-based access tokens. |
isIdTokenSupported |
Required. Indicates support for OpenID Connect ID tokens. |
isRefreshTokenSupported |
Required. Indicates support for OAuth 2.0 refresh tokens. |
isSaml2Supported |
Required. Indicates support for SAML 2.0 assertions. |
isUserCreationAllowed |
Required. Controls whether the token exchange flow can provision a new Salesforce user rather than only map to an existing user. |
isProtected |
Governs whether components created in a subscriber org can link to or reference the handler. This is a packaging setting, not a runtime enablement setting. |
isContactCreationAllowed |
For internal use only. It is not a documented administrator-facing contact-provisioning control. |
Behavior and Constraints
Enablement and handler selection
A handler can be enabled for multiple connected apps or external client apps. Setting isEnabled to true is only part of enablement: add enablements entries specifying the applications and the users who run the Apex handler.
The execution user and the mapped Salesforce user have different roles. apexExecutionUser runs the handler's Apex code. The mapping logic identifies or creates the Salesforce user represented by the external token.
A token request can select a handler with the optional token_handler parameter. Its value is the handler's Apex class name, not the OauthTokenExchangeHandler developer name or display label. When the request omits that parameter, Salesforce uses the application's default handler. Changing isDefault therefore changes selection for requests that do not explicitly identify a handler. The default is application-specific, not org-wide.
Supported tokens
The token-support flags describe overlapping categories, not mutually exclusive modes. isAccessTokenSupported covers both opaque and JWT-based OAuth 2.0 access tokens. isJwtSupported covers tokens in JWT format, including JWT-based access tokens. A JWT-based access token therefore belongs to both categories.
The other flags cover OpenID Connect ID tokens, OAuth 2.0 refresh tokens, and SAML 2.0 assertions. Interpret these settings alongside the associated Apex validation logic; the metadata does not replace that logic.
User provisioning
User creation depends on both configuration and Apex behavior. When isUserCreationAllowed is true, canCreateUser in getUserForTokenSubject is true, and the user does not exist in Salesforce, the handler sets up a new User object. Salesforce automatically inserts that object to finish creating the user.
The flag alone does not supply the mapping or user-construction logic. Also, isContactCreationAllowed is for internal use only; do not interpret it as a documented contact-provisioning option.
There are no additional access requirements specific to this metadata type.
Deployment and Retrieval
OauthTokenExchangeHandler components use the .oauthtokenexchangehandler suffix and reside in the oauthtokenexchangehandlers folder. The type extends Metadata and inherits fullName. A package.xml manifest can use OauthTokenExchangeHandler as the type name and * as the member value for wildcard retrieval.
The definition requires description, developerName, masterLabel, tokenHandlerApex, isEnabled, isUserCreationAllowed, and all five token-support flags. Each enablement entry requires apexExecutionUser and isDefault, with the application identified through connectedApp or externalClientApp.
Keep the references distinct when preparing a deployment: tokenHandlerApex identifies the Apex class, while the application fields contain application API names. The nested OauthTokenExchHandlerApp settings belong inside the handler's enablements array.
isProtected governs whether components created in a subscriber org can link to or reference the handler. It does not replace isEnabled or the application enablement entries. See Protected Components in Managed Packages.
Relationships
The handler references the Apex class that implements token validation and user mapping, and its enablement entries associate it with applications. externalClientApp identifies an external client app by API name, while apexExecutionUser identifies the user who runs the handler. These identities are separate from the handler's own developerName.
References
-
ApexClass:tokenHandlerApex. -
ConnectedApp:connectedApp, withinenablements.
Documentation Inconsistencies
The official declarative XML example opens <connectedApp> but closes it with the misspelled </conectedApp>. That example is not well-formed XML as printed. Use the matching closing tag </connectedApp>.
Common Questions
Why isn't my token exchange handler enabled when isEnabled is true?
isEnabled alone does not complete enablement. Add enablements entries that specify the application, apexExecutionUser, and isDefault.
What name goes in the token_handler parameter?
Use the handler's Apex class name. Do not use the OauthTokenExchangeHandler developer name or display label. If the request omits token_handler, Salesforce uses the application's default handler.
Does isUserCreationAllowed create Salesforce users automatically?
Not by itself. The Apex handler must supply the mapping and user-construction logic. When creation is allowed, canCreateUser is true, and the user does not exist, the handler sets up a User object that Salesforce automatically inserts.
Can a JWT access token fall under both token-support flags?
Yes. isAccessTokenSupported includes JWT-based access tokens, and isJwtSupported includes tokens in JWT format. The categories overlap and must be interpreted with the Apex validation logic.
How do I retrieve all OauthTokenExchangeHandler components?
Use OauthTokenExchangeHandler as the type name and * as the member value in package.xml. Components use the .oauthtokenexchangehandler suffix in the oauthtokenexchangehandlers folder.