SCIM User Response 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 Response Converter lambda converts a FusionAuth user into the SCIM User response, and optionally the SCIM EnterpriseUser extension, that FusionAuth returns to the SCIM client. FusionAuth runs the lambda after FusionAuth acts on 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(scimUser, user, context) {
// Lambda code goes here
}
The lambda must contain a function named convert that accepts the following parameters:
scimUser: the SCIM response. You can modify this object.user: the FusionAuth user. 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 scimUser object holds the SCIM User response JSON, plus the SCIM EnterpriseUser extension. See SCIM User and SCIM EnterpriseUser extension.
Before the lambda runs, FusionAuth fills in the following scimUser fields:
id: the FusionAuth user Id.externalIdactiveschemas: the core User schema. For requests to the EnterpriseUsers endpoint, FusionAuth adds the enterprise extension schema.meta:created,lastModified,resourceType, andlocation, the URI of the user resource.
The lambda can add to or change any field in scimUser.
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 Response Converter lambda. The default lambda copies the FusionAuth user to the core SCIM User attributes. The lambda returns each extension stored under user.data.extensions and adds the extension schema to schemas. The default SCIM User Request Converter lambda stores extensions in the same place.
The default lambda always returns one email and one phone number. When the user has no mobile phone, the phone number entry has no value.
function convert(scimUser, user) {
// Un-comment this line to see the user object printed to the event log
// console.info(JSON.stringify(user, null, 2));
scimUser.active = user.active;
scimUser.userName = user.username;
scimUser.name = {
formatted: user.fullName,
familyName: user.lastName,
givenName: user.firstName,
middleName: user.middleName,
honorificPrefix: user.data.honorificPrefix,
honorificSuffix: user.data.honorificSuffix
};
scimUser.phoneNumbers = [{
primary: true,
value: user.mobilePhone,
type: "mobile"
}];
scimUser.emails = [{
primary: true,
value: user.email,
type: "work"
}];
// Optionally return any custom extensions stored in user.data
if (user.data && user.data.extensions) {
for (var extension in user.data.extensions) {
if (scimUser.schemas.indexOf(extension) === -1) {
scimUser.schemas.push(extension);
}
scimUser[extension] = user.data.extensions[extension];
}
}
}Example: Return Enterprise Attributes From User Data#
The following lambda reverses the enterprise attributes example for the SCIM User Request Converter. The lambda makes these changes to the default lambda:
- The lambda builds the enterprise extension from the
user.datafields that the request converter wrote. When the user has none of those fields, the lambda leaves out the extension. - The lambda builds the manager
$reffromscimUser.meta.location, by swapping the user Id at the end of the URI for the manager Id. - The lambda adds the enterprise schema to
schemaswhen the SCIM client called the core Users endpoint. - The lambda returns
emailsandphoneNumbersonly when the user has an email or mobile phone.
// builds the SCIM user from the user, and the enterprise extension from the user.data keys the request converter wrote
var enterpriseSchema = 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User';
var enterpriseFields = ['employeeNumber', 'costCenter', 'organization', 'division', 'department'];
function convert(scimUser, user, context) {
scimUser.active = user.active;
scimUser.userName = user.username;
scimUser.name = {
formatted: user.fullName,
familyName: user.lastName,
givenName: user.firstName,
middleName: user.middleName,
honorificPrefix: user.data.honorificPrefix,
honorificSuffix: user.data.honorificSuffix
};
// omit empty multi-valued attributes instead of sending entries with no value
if (user.email) {
scimUser.emails = [{ primary: true, type: 'work', value: user.email }];
}
if (user.mobilePhone) {
scimUser.phoneNumbers = [{ primary: true, type: 'mobile', value: user.mobilePhone }];
}
var enterprise = {};
enterpriseFields.forEach(function (field) {
if (user.data[field]) {
enterprise[field] = user.data[field];
}
});
if (user.data.managerId) {
enterprise.manager = {
value: user.data.managerId,
// the manager is another user, so swap the user Id at the end of this user's location
$ref: scimUser.meta.location.replace(/[^/]+$/, user.data.managerId),
displayName: user.data.managerName
};
}
if (Object.keys(enterprise).length === 0) {
return;
}
if (scimUser.schemas.indexOf(enterpriseSchema) === -1) {
scimUser.schemas.push(enterpriseSchema);
}
scimUser[enterpriseSchema] = enterprise;
}Related
Learn how to convert an incoming SCIM user request into a FusionAuth user with a lambda.
Learn how to convert a FusionAuth group into the SCIM group response a SCIM client expects.
Learn how to convert an incoming SCIM group request into a FusionAuth group with a lambda.
Learn how to configure Azure AD/Microsoft Entra ID as a SCIM client.