The setUserByConnection Function Does Not Validate Users Against Upstream Identity Providers
Last Updated:
Overview
This article clarifies the validation behavior of the api.authentication.setUserByConnection function. Specifically, it addresses whether this function performs real-time validation against an upstream Identity Provider (IdP) when used within an Auth0 Custom Token Exchange Action.
Applies To
- Custom Token Exchange
- Actions
api.authentication.setUserByConnection- Enterprise Connections
- Social Connections
Solution
The api.authentication.setUserByConnection function does not contact an upstream IdP to validate a user's existence. The function operates on the principle that the incoming token has already been validated. The key behaviors are:
-
Simulated IdP Response: The function simulates a successful authentication from a federated IdP. It trusts the
user_profileobject passed to it as the payload that an IdP would typically return after a successful user authentication. -
Internal User Store Interaction: For social and enterprise connections, the function's scope is limited to Auth0's internal user store. It saves or updates the user's profile within Auth0 but does not create, modify, or validate the user in the upstream IdP's directory.
-
Developer-Owned Validation: The Custom Token Exchange framework requires the developer to validate the incoming
subject_token. The Action script must securely verify the token's authenticity and integrity before callingsetUserByConnectionto assert the user's identity.
The correct implementation is to first validate the external token and then use the function to create or update the user profile in Auth0:
-
Within the
onExecuteTokenExchangeAction, implement custom logic to securely decode and validate theevent.transaction.subject_token. This step is critical to prevent security risks such as token spoofing. -
After successfully validating the token, extract the necessary user attributes to build a
user_profileobject. NOTE: Theuser_profileobject must include auser_idthat is the unique identifier for the user within the external IdP for that specific connection. -
Call the
api.authentication.setUserByConnectionfunction, passing the connection name and the prepareduser_profileobject.
Auth0 then uses the provided profile to either create a new user or update an existing one in its user store.