Oauth2TokenExchangeHandler Class

Use this class to create a token exchange handler that validates tokens from an external identity provider and maps the token’s subject to a Salesforce user during the OAuth 2.0 token exchange flow. The handler can also be used to create users by setting up a new User object and returning it to Salesforce for automatic insertion.

Namespace

Auth

Oauth2TokenExchangeHandler Methods

The following are methods for Oauth2TokenExchangeHandler.

getUserForTokenSubject(networkId, result, canCreateUser, appDeveloperName, appType)

Finds the subject defined in the external identity provider’s token so that it can be mapped to a Salesforce subject.

Signature

public User getUserForTokenSubject(Id networkId, Auth.TokenValidationResult result, Boolean canCreateUser, String appDeveloperName, Auth.IntegratingAppType appType)

Parameters

networkId
Type: Id
The identifier for the Salesforce user, if one exists.
result
Type: Auth.TokenValidationResult
The result of the token validation performed by the validateIncomingToken method in the Auth.Oauth2TokenExchangeHandler class.
canCreateUser
Type: Boolean
Specifies whether the handler can set up a User object if no user exists. Salesforce automatically inserts the user into this object.
appDeveloperName
Type: String
The developer name of the Salesforce connected app or external client app that���s being used to integrate your app with Salesforce.
appType
Type: Auth.IntegratingAppType
Specifies whether your app is integrated with Salesforce as a connected app or as an external client app.

Return Value

Type: User

Returns a User object with the user information obtained from the token, from Salesforce, and from callouts to the identity provider, if applicable. The User object can be an existing user record or a new user that hasn’t been inserted in the database. If it’s a new user, Salesforce automatically inserts the user on behalf of the token exchange handler.

validateIncomingToken(appDeveloperName, appType, incomingToken, tokenType)

Validates an access token, refresh token, ID token, SAML 2.0 assertion, or JWT passed from an external identity provider during the OAuth 2.0 token exchange flow.

Signature

public Auth.TokenValidationResult validateIncomingToken(String appDeveloperName, Auth.IntegratingAppType appType, String incomingToken, Auth.OAuth2TokenExchangeType tokenType)

Parameters

appDeveloperName
Type: String
The developer name of the Salesforce connected app or external client app that’s being used to integrate your app with Salesforce.
appType
Type: Auth.IntegratingAppType
Specifies whether your app is integrated with Salesforce as a connected app or as an external client app.
incomingToken
Type: String
The token from the external identity provider.
tokenType
Type: Auth.OAuth2TokenExchangeType
The type of token from the external identity provider. It can be an access token, a refresh token, an ID token, a SAML 2.0 assertion, or any token that’s formatted as a JSON Web Token (JWT).

Return Value

Type: Auth.TokenValidationResult

Returns information about whether the token is valid, data extracted from the token, the token itself, and the token type. It can also return a custom error message if the validation failed.