SAML v2 Reconcile Lambda
On this page
When an SAML v2 identity provider is used to complete a federated login request, FusionAuth will use the configured linking strategy and well known SAML v2 attributes to reconcile the user. FusionAuth will attempt to match the user information returned from the SAML v2 identity provider to an existing user account or create a new one.
You may optionally utilize a lambda to customize the user and user registration during the authentication event.
The SAML response from an IdP often includes custom attributes. To copy custom attributes to fields on the FusionAuth user, use a lambda.When you create a new lambda using the FusionAuth Admin UI, you will be provided an empty function to implement.
Starting in version 1.65.0, you can use attribute mappings to map IdP claims directly to FusionAuth user and registration fields without writing a lambda. Reconcile lambdas and attribute mappings are mutually exclusive. Attempting to configure both on a given IdP results in a validation error.
Lambda Structure#
When you create the lambda with the API, give the function the following signature:
function reconcile(user, registration, samlResponse, context) {
// Lambda code goes here
}
This lambda must contain a function named reconcile that accepts the following parameters:
user: the FusionAuth User object. You can modify this object. However, you can't change the email or username attributes after FusionAuth links the user.registration: the FusionAuth UserRegistration object. You can modify this object.
samlResponse: the SAML v2 response object. This object is read-only.context: Available since1.64.0an object containing the context of the request, including access to secrets. This object is read-only.
The User API and Registration API documentation describe the user and registration objects. The SAML response object follows the structure of the XML document, without the DOM object model. The SAML response has the following fields:
SAML Response Fields#
samlResponse.assertion.attributes A map of the attributes of the user. This is sometimes call the claims of the user. Since SAML attributes can be multi-valued, you will need to wrap single values in Arrays like this:
samlResponse.assertion.attributes['firstName'] = [user.firstName];
samlResponse.assertion.conditions.audiences A list of the audiences for this SAML response. By default, the audience or issuer, in that order, from the corresponding SAML identity provider configuration are used.
samlResponse.assertion.conditions.notBefore The instant that this assertion starts being valid. This is the number of milliseconds since Epoch UTC.
samlResponse.assertion.conditions.notOnOrAfter The instant that this assertion stops being valid. This is the number of milliseconds since Epoch UTC.
samlResponse.assertion.issuer The issuer of this SAML assertion. This defaults to the issuer from the corresponding SAML identity provider configuration.
samlResponse.assertion.subject.nameID.format The NameID format for the id of the subject (user). FusionAuth uses the emailaddress format specified by the SAML specifications. It is not recommended that you change this unless you know what you are doing.
1.28.0samlResponse.assertion.subject.nameID.id The NameID id of the subject (user). This defaults to the user's email address. It is not recommended that you change this unless you know what you are doing.
1.28.0samlResponse.assertion.subject.nameIDs The list of NameId objects.
Prior to version 1.28.0, this field was a single entry but has since been converted to an array.
samlResponse.assertion.subject.nameIDs[x].format The NameID format for the id of the subject (user). FusionAuth uses the urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress format specified by the SAML specifications by default. It is not recommended that you change this unless you know what you are doing.
samlResponse.assertion.subject.nameIDs[x].id The NameID id of the subject (user). This defaults to the user's email address. It is not recommended that you change this unless you know what you are doing.
samlResponse.assertion.subject.confirmation.inResponseTo This is the ID from the SAML request. It is not recommended that you change this unless you know what you are doing.
samlResponse.assertion.subject.confirmation.method The confirmation method FusionAuth uses for the subject (user). This default to Bearer. It is not recommended that you change this unless you know what you are doing.
samlResponse.assertion.subject.confirmation.notBefore The instant that this assertion's subject starts being valid. This is the number of milliseconds since Epoch UTC.
samlResponse.assertion.subject.confirmation.notOnOrAfter The instant that this assertion's subject stops being valid. This is the number of milliseconds since Epoch UTC.
samlResponse.assertion.subject.confirmation.recipient The recipient of the subject. This defaults to the callback URL (ACS).
samlResponse.assertions An array containing all assertions on the SAML response. You can use this array to inspect the original assertions on the response instead of the combined assertion.
samlResponse.destination The destination of the SAML response. This defaults to the callback URL (ACS).
samlResponse.id A unique identifier for the SAML response that is generated by FusionAuth.
samlResponse.inResponseTo This is the ID from the SAML request. It is not recommended that you change this unless you know what you are doing.
samlResponse.issueInstant The instant that this assertion was created. This is the number of milliseconds since Epoch UTC.
samlResponse.issuer This is the issuer of the SAML response. This defaults to the name of this FusionAuth deployment.
samlResponse.status.code The status code of the SAML response. Whenever the lambda is called, this will always be Success.
samlResponse.status.message The status message of the SAML response. Whenever the lambda is called, this will always be null.
Assigning The Lambda#
After you create the lambda, assign the lambda to one or more SAML v2 identity providers.
Navigate to Settings -> Identity Providers . Select your existing SAML v2 configuration. If you haven't configured SAML v2 yet, click Add provider and select SAML v2.
Example Lambda#
The samlResponse.assertion.attributes map holds a list of values for every attribute, even when the attribute has one value. The following lambda reads the first value of the name and department attributes. The attribute names in the example are the claim URIs that Azure AD sends. When an attribute is missing, the lambda keeps the saved value.
The lambda also sets the registration roles from the groups attribute. The groupRoles map lists the IdP groups that grant a role, and the lambda drops groups with no mapping. When the response has no groups attribute, the lambda leaves the roles unchanged.
// fills in the profile from SAML attributes and assigns roles from the groups attribute
var claims = 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/';
var groupRoles = {
'Analytics Admins': 'admin',
'Analytics Users': 'viewer'
};
function reconcile(user, registration, samlResponse, context) {
var attributes = samlResponse.assertion.attributes || {};
// every attribute value is a list, even when the attribute has one value
function first(name) {
var values = attributes[name];
return values && values.length > 0 ? values[0] : null;
}
user.firstName = first(claims + 'givenname') || user.firstName;
user.lastName = first(claims + 'surname') || user.lastName;
registration.data = registration.data || {};
registration.data.department = first('department') || registration.data.department;
// a missing groups attribute leaves the registration roles unchanged
if (attributes.groups) {
registration.roles = attributes.groups
.map(function (group) {
return groupRoles[group];
})
.filter(Boolean);
}
}To see what your IdP returns in the samlResponse object while you develop a lambda, print the object to the Event Log. Add the following line to the lambda:
console.info(JSON.stringify(samlResponse, null, 2));
Related
Learn how to map the data from an Apple login to a FusionAuth user and registration with a reconcile lambda.
Learn how to map the data from an Epic Games login to a FusionAuth user and registration with a reconcile lambda.
Learn how to map the claims in an external JWT to a FusionAuth user and registration with a reconcile lambda.
Learn how to map the data from a Facebook login to a FusionAuth user and registration with a reconcile lambda.