SCIM User Request Converter Lambda
On this page
A SCIMAn open standard defining an HTTP REST protocol and schema for automating user and group provisioning across systems. User Request Converter lambda converts an incoming SCIM User request, and optionally the SCIM EnterpriseUser extension, into a FusionAuth user. FusionAuth runs the lambda before FusionAuth creates or updates the user.
When you create a lambda in the Admin UI, FusionAuth provides an empty function for you to implement.
Lambda Structure#
If you create the lambda with the API, the function must have the following signature:
function convert(user, options, scimUser, context) {
// Lambda code goes here
}
The lambda must contain a function named convert that accepts the following parameters:
user: the FusionAuth user. You can modify this object. FusionAuth passes a new user with an emptydataobject. The lambda sets every user field that comes from the SCIM request.options: the request options. You can modify this object. The object has the following properties:options.disableDomainBlockoptions.sendSetPasswordEmail: applies only when FusionAuth creates a user.options.sendSetPasswordIdentityType: applies only when FusionAuth creates a user.options.skipVerification
scimUser: the SCIM request. This object is read-only.context: Available since1.64.0the context of the request, including access to secrets. This object is read-only.
The User API documents the FusionAuth user. The options properties match the Create User API parameters outside the user object, such as disableDomainBlock.
FusionAuth ignores any other options property, including options.applicationId. The lambda can't register the user with an application. To add registrations to provisioned users, see Adding Registrations.
The scimUser object holds the SCIM User request JSON, plus the SCIM EnterpriseUser extension when the client sends one. See SCIM User and SCIM EnterpriseUser extension.
Assign the Lambda#
After you create the lambda, assign the lambda to a tenant on the SCIM tab of the tenant configuration.
Default Lambda#
FusionAuth provides a default SCIM User Request Converter lambda. The default lambda copies the core SCIM User attributes to the FusionAuth user. The lambda uses the email and phone number marked primary. The lambda stores each extension, such as the enterprise extension, under user.data.extensions, keyed by the schema URN.
function convert(user, options, scimUser) {
// Un-comment this line to see the scimUser object printed to the event log
// console.info(JSON.stringify(scimUser, null, 2));
// Request options
// Note, sendSetPasswordEmail is only utilized during a user create request.
// options.applicationId = null;
// options.disableDomainBlock = false;
// options.sendSetPasswordEmail = false;
// options.skipVerification = false;
user.active = scimUser.active;
user.data.honorificPrefix = scimUser.name && scimUser.name.honorificPrefix;
user.data.honorificSuffix = scimUser.name && scimUser.name.honorificSuffix;
user.firstName = scimUser.name && scimUser.name.givenName;
user.fullName = scimUser.name && scimUser.name.formatted;
user.lastName = scimUser.name && scimUser.name.familyName;
user.middleName = scimUser.name && scimUser.name.middleName;
user.password = scimUser.password;
user.username = scimUser.userName;
// user.email
if (scimUser.emails) {
for (var i = 0; i < scimUser.emails.length; i++) {
if (scimUser.emails[i].primary) {
user.email = scimUser.emails[i].value;
}
}
}
// user.mobilePhone
if (scimUser.phoneNumbers) {
for (var j = 0; j < scimUser.phoneNumbers.length; j++) {
if (scimUser.phoneNumbers[j].primary) {
user.mobilePhone = scimUser.phoneNumbers[j].value;
}
}
}
// Handle the Enterprise User extension and other custom extensions
if (scimUser.schemas) {
for (var k = 0; k < scimUser.schemas.length; k++) {
var schema = scimUser.schemas[k];
if (schema !== 'urn:ietf:params:scim:schemas:core:2.0:User') {
user.data = user.data || {};
user.data.extensions = user.data.extensions || {};
user.data.extensions[schema] = scimUser[schema] || {};
}
}
}
}Example: Store Enterprise Attributes in User Data#
The following lambda extends the default lambda for a SCIM client that sends the enterprise extension. The lambda makes these changes:
- The lambda copies
employeeNumber,costCenter,organization,division, anddepartmentto top-leveluser.datafields, which you can search on and read in other lambdas. The lambda skips empty values. - The lambda stores the manager Id and display name in
user.data.managerIdanduser.data.managerName. - The lambda falls back to the first email and phone number when the SCIM client marks none as primary.
- The lambda treats a missing
activevalue as active, because SCIM clients may omitactive.
// copies SCIM core and enterprise attributes to the user, with enterprise fields as top-level user.data keys
var enterpriseSchema = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User';
var enterpriseFields = ['employeeNumber', 'costCenter', 'organization', 'division', 'department'];
// the primary entry, or the first entry when the client marks none as primary
function preferred(entries) {
if (!entries || entries.length === 0) {
return undefined;
}
var primary = entries.filter(function (entry) {
return entry.primary;
});
return (primary[0] || entries[0]).value;
}
function convert(user, options, scimUser, context) {
var name = scimUser.name || {};
// SCIM clients may omit active, so treat a missing value as active
user.active = scimUser.active !== false;
user.username = scimUser.userName;
user.password = scimUser.password;
user.email = preferred(scimUser.emails);
user.mobilePhone = preferred(scimUser.phoneNumbers);
user.firstName = name.givenName;
user.middleName = name.middleName;
user.lastName = name.familyName;
user.fullName = name.formatted;
user.data.honorificPrefix = name.honorificPrefix;
user.data.honorificSuffix = name.honorificSuffix;
var enterprise = scimUser[enterpriseSchema];
if (!enterprise) {
return;
}
enterpriseFields.forEach(function (field) {
if (enterprise[field]) {
user.data[field] = enterprise[field];
}
});
if (enterprise.manager && enterprise.manager.value) {
user.data.managerId = enterprise.manager.value;
user.data.managerName = enterprise.manager.displayName;
}
}Pair this lambda with the enterprise attributes example for the SCIM User Response Converter, so FusionAuth returns the same attributes to the SCIM client.
Related
Learn how to convert a FusionAuth user into the SCIM user response a SCIM client expects.
Learn how to convert an incoming SCIM group request into a FusionAuth group with a lambda.
Learn how to convert a FusionAuth group into the SCIM group response a SCIM client expects.
Learn how to configure Azure AD/Microsoft Entra ID as a SCIM client.