subject_token_type. This will provide you with information about the user for the transaction. With this information, your code should also enforce the authorization policy needed for the transaction. Once you are sure the transaction can proceed, you can confirm it by setting the corresponding user. Auth0 will then issue access, ID, and refresh tokens for this user. You can think of this as a way to authenticate users.
Each Custom Token Exchange transaction generates a tenant event log. Successful transactions generate event logs of type secte, while failed transactions generate event logs of type fecte. Use these log types to help you understand any errors you may receive. Errors from the /oauth/token endpoint reveal less details.
Custom Token Exchange gives you the added flexibility to set the user for the transaction by taking on the additional responsibility of securely validating the corresponding subject token that identifies the user for the transaction.Remember that subject tokens used with Custom Token Exchange can be any token format or type you require, as long as your Action code can interpret them. You must implement strong validation of the tokens you receive and accept. If you fail to do so, you open yourself up to different attack vectors, such as spoofing or replay attacks, resulting in bad actors being able to authenticate with someone else’s user ID.To learn about different options for implementing secure validation of your subject tokens, read and apply the recommendations included in Example Use Cases and Code Samples. Make sure you also take into consideration and apply Attack Protection capabilities.
api.authentication.setUserById(user_id)
Sets user attributes based on a specified user ID for any connection type. This allows you to specify an existing user without updating the profile. This method fails if the user does not exist or is blocked.| Parameter | Description |
|---|---|
user_id | The user ID, such as auth0|55562040asf0aef. |
api.authentication.setUserByConnection(connection_name, user_profile, options)
Sets a user and their associated profile attributes in a specified connection. This is equivalent to returning the specified user profile from the federated IdP (or the corresponding Custom DB with Import Users to Auth0 disabled) when a user logs into this connection. You can configure whether this operation should create the user if it does not exist, and whether it should update the profile using the provided user profile attributes. The login count will be incremented for each user that is logged in viasetUserByConnection(). This method always fails for blocked users.
Custom Token Exchange EA currently supports
setUserByConnection() for Auth0 database connections, Enterprise and Social connections, and Custom DBs with Import Users to Auth0 disabled.| Parameter | Description |
|---|---|
connection_name | The name of the connection where the user profile will be set. Limited to 512 characters. |
user_profile | An object containing the user profile attributes to be set. Limited to 24 properties. |
options | An object specifying update and creation behavior.{updateBehavior: ‘replace’ | ‘none’,creationBehavior: ‘create_if_not_exists’ | ‘none’,}If the user exists, updateBahaviour does the following:
|
Supported user profile attributes
ThesetUserByConnection() method allows you to set profile attributes supported by the Update a User endpoint:
user_id(required): user’s unique identifier for this connection/provider. It is typically the user ID provided by the external identity provider for the connection. This is the only required parameter when bothcreationBehaviourandupdateBehaviourare set tonone.emailemail_verified. Defaults tofalse.usernamephone_numberphone_verified. Defaults tofalse.namegiven_namefamily_namenicknamepicture
Supported connection strategies
The current version provides support for the following connection strategies. ThesetUserByConnection() method fails for other strategies. Please contact Auth0 support to request adding support for other strategies.
Auth0 database connections:
- Custom database connections with Import Users to Auth0 disabled
- Custom Social Connections
- Apple
- Github
- Windowslive
Creation behavior
Users are dynamically created only whencreationBehavior is set to create_if_not_exists.
When creating users:
- You must provide an identifier as configured by your connection. By default, an email is required.
- For connections that use Flexible Identifiers and Attributes, you may provide a username and phone number if the corresponding attribute is enabled for the connection.
-
For connections that don’t use Flexible Identifiers and Attributes:
- You must provide an email address.
-
You may provide a username when the connection’s Require Username is set to
true. To learn more, read Adding Username for Database Connections. -
You cannot provide a
phone_number.
- You may specify
email_verifiedandphone_verified.
Set
creationBehavior to none when you want to log the user in but don’t want to create the user if they do not already exist in the connection.Future iterations of Custom Token Exchange will make the email attribute optional dependent on the connection configuration.Update behavior
User profile is updated only whenupdateBehavior is set to replace.
The following attributes cannot be modified and Auth0 returns an error when trying to change its value:
emailusernamephone_numberemail_verifiedphone_verified
If you want to use
setUserByConnection() to update a user profile that already contains email, username, or phone_number attributes, you must pass these attributes with the same value they already have. Otherwise, the method returns an error. Additionally, this action does not update the upstream Enterprise/Social IdP or the Custom DB, so make your updates match the latest version of the profile on the IdP/Custom DB if you want them to be in sync.Set
updateBehavior to none when you want to log the user in but don’t want to change any profile attributes if they already exist in the connection.Email verification
Auth0 automatically sends verification emails when you create a user withemail_verified=false. You can override this behavior by specifying verify_email=false as a user profile attribute. It won’t be stored as part of the user profile.
api.authentication.setOrganization(organization)
Sets the organization for the transaction. Useful when you want to set an organization even when the incoming Custom Token Exchange request does not explicitly indicate one. When the incoming request includes an organization parameter and you set one with this method, both need to match to the same organization or the Action will fail.| Parameter | Description |
|---|---|
organization | String. If the Use Organization Names in Authentication API is allowed for your tenant, you can provide the |
Example
Application configuration
To be able to set an organization to the transaction, the client making the Custom Token Exchange request must be able to use Organizations. Make sureorganization_usage is set to allow or require. To learn more, read Define Organization Behaviour.
Membership validation
For thesetOrganization() method to succeed, the following rules regarding connections and organization membership apply:
- The connection must be enabled for the target organization. To determine that:
- When using
setUserById(): The system relies on the user’s profile primary connection. - When using
setUserByConnection(): The system uses the specific connection provided in the method call, even if it is a secondary connection for the user in the case of linked accounts.
- When using
- The user must be a member of the organization:
setUserById(): Auto-membership does not apply. The user must already be a member of the organization for the transaction to succeed.setUserByConnection(): Auto-membership applies if configured for the corresponding connection. If the user is not a member and auto-membership is enabled, they are automatically added, and the transaction succeeds. If auto-membership is disabled and the user is not a member, the transaction fails.
Organization sign-up
User creation is permitted irrespective of the organization sign-up configuration on the connection.Set metadata
Unlike the Update a User endpoint, thesetUserByConnection() method does not allow you to set user or application metadata. Instead, you can use api.user.setAppMetadata. To learn how to correctly use metadata, read How Metadata Works in User Profiles. For metadata best practices, read How to Manage User Metadata with the Post-login Trigger.
api.user.setAppMetadata(name, value)
Sets application metadata for the user that is logging in. This method follows a merge behavior, so you can indicate the new attributes to add or the ones to be updated without affecting the existing ones. To remove an attribute, set its value tonull.
| Parameters | Description |
|---|---|
name | String. The name of the metadata property. |
value | String, object or array. The name of the metadata property. |
api.user.setUserMetadata(name, value)
Sets general metadata for the user that is logging in. This method follows a merge behavior, so you can indicate the new attributes to add or the ones to be updated without affecting the already existing ones. To remove an attribute, set its value tonull.
| Parameters | Description |
|---|---|
name | String. The name of the metadata property. |
value | String, object or array. The name of the metadata property. |
api.access.deny(code, reason)
Denies the login transaction and returns an error to the caller.| Parameter | Description |
|---|---|
code | A string returned in the error property in the response. Two standard error codes can be used:
If you use your own error code, it returns a 400 status code. |
reason | A string returned in the error_description property in the response. |
api.access.rejectInvalidSubjectToken(reason)
Denies the transaction and increments the counter of failed attempts for the external IP from which the request is coming. The Custom Token Exchange request is rejected with a400 Bad Request error response with the error code invalid_request.
When the maximum number of failed attempts is reached, Auth0 blocks traffic for a period of time for all Custom Token Exchange requests coming from that IP with a 429 Too Many Requests error response with error code too_many_attempts. To learn more read Attack Protection.
Use this method whenever you receive a Custom Token Exchange request with a subject token that is not properly signed/encrypted or expired, or under any circumstance that points to any non-legitimate usage such as in a spoofing or replay attack. This allows Auth0 to activate protection as per your configuration.
By default, Suspicious IP Throttling allows for 10 max attempts at a rate of 6 attempts/hour. To learn more, read Attack Protection.
| Parameter | Description |
|---|---|
reason | A string returned in the error_description property in the response |
api.cache
Store and retrieve data that persists across executions. These methods are useful for caching data used for subject token validation, such as public keys for signature validation. This can help improve performance when fetching the keys from ajwks-uri.
api.cache.delete(key)
Delete a record describing a cached value at the suppliedkey if it exists.
Returns a CacheWriteResult object with type: "success" if a value was removed from the cache. A failed operation returns type: "error". For errors, the returned object will have a code property that indicates the nature of the failure.
| Parameter | Description |
|---|---|
key | String. The key of the record stored in the cache. |
api.cache.get(key)
Retrieve a record describing a cached value at the suppliedkey, if it exists. If a record is found, the cached value can be found at the value property of the returned object.
Returns a cache record if an item is found in the cache for the supplied key. Cache records are objects with a value property holding the cached value as well as an expires_at property indicating the maximum expiry of the record in milliseconds since the Unix epoch.
Important: This cache is designed for short-lived, ephemeral data. Items may not be available in later transactions even if they are within their supplied lifetime.
| Parameter | Description |
|---|---|
key | String. The key of the record stored in the cache. |
api.cache.set(key, value, [options])
Store or update a string value in the cache at the specified key. Values stored in this cache are scoped to the Trigger in which they are set. They are subject to the Actions Cache Limits. Values stored in this way will have lifetimes of up to the specifiedttl or expires_at values. If no lifetime is specified, a default lifetime of 15 minutes will be used. Lifetimes may not exceed the maximum duration listed at Actions Cache Limits.
Returns CacheWriteSuccess if the values are stored successfully. Otherwise, you will receive CacheWriteError.
| Parameter | Description |
|---|---|
key | String. The key of the record stored in the cache. |
value | String. The value of the record to be stored. |
options | Optional object. Options for adjusting cache behavior. |
options.expires_at | Optional number. The absolute expiry time in milliseconds since the unix epoch. While cached records may be evicted earlier, they will never remain beyond the supplied expires_at.Note: This value should not be supplied if a value was also provided for ttl. If both options are supplied, the earlier expiry of the two will be used. |
options.ttl | Optional number. The time-to-live value of this cache entry in milliseconds. While cached values may be evicted earlier, they will never remain beyond the supplied ttl.Note: This value should not be supplied if a value was also provided for expires_at. If both options are supplied, the earlier expiry of the two will be used. |