SP adapter
Use this script type to make application-specific changes during the processing of the
authentication request on the SP side, such as updating the SPNameQualifier attribute.
- Next-generation example script
- Legacy example script
- Script bindings
The script provides hooks at the following points:
| Processing phase | Description |
|---|---|
|
Invoked before Advanced Identity Cloud sends the single sign-on request to the IDP. |
|
Invoked before single sign-on processing begins on the SP side, when Advanced Identity Cloud receives the response from the IDP. |
|
Invoked when single sign-on processing succeeds. |
|
Invoked when single sign-on processing fails. |
|
Invoked when the processing of a new name identifier succeeds. |
|
Invoked when the association of a name identifier between an SP and IDP is successfully terminated. |
|
Invoked before the single logout process starts on the SP side, while the authenticated session is still valid. |
|
Invoked after the single logout process succeeds when the authenticated session has been invalidated. |
Update authentication request using a legacy script
This task assumes your environment is already correctly configured for single sign-on using SAML 2.0, where Advanced Identity Cloud is the hosted SP.
Complete the following steps to implement an example SP adapter script that updates the SPNameQualifier attribute in the authentication request:
Create the script
-
In the Advanced Identity Cloud admin console, create a script of type SAML2 SP Adapter.
-
In the JavaScript field, paste the template SAML2 SP Adapter Script.
-
Copy the SAML2 SP Adapter Script and paste in the JavaScript field.
-
Add code to the
preSingleSignOnRequestfunction to change the value ofSPNameQualifierin the authentication request. Optionally, add code to redirect a successful login in thepostSingleSignOnSuccessfunction.For example:
function preSingleSignOnRequest() { logger.error("In preSingleSignOnRequest"); authnRequest.getNameIDPolicy().setSPNameQualifier("mySP-Updated"); } function postSingleSignOnSuccess() { logger.error("In postSingleSignOnSuccess"); response.sendRedirect("https://example.com"); return true; } -
Save your changes and close the editor.
Set session properties using a next-generation script
This example uses a next-generation script to set SAML attributes in the current session and conditionally redirects the authenticated user to a website:
Create the script
-
Under Native Consoles > Access Management, go to Realms > Realm Name > Scripts, and click +New Script.
-
Provide a suitable name for your script and select the following values:
- Script Type
-
Saml2 SP Adapter - Evaluator Version
-
Next Generation
-
Click Create.
-
In the Script field, paste the template SAML2 SP Adapter Script (Next Gen).
-
Replace the
postSingleSignOnSuccessfunction with the following script:function postSingleSignOnSuccess() { var redirectOccurred = false; try { if (!ssoResponse || !session) { logger.error("Missing ssoResponse or session object."); return false; } // Set response attributes as session properties var issueInstant = ssoResponse.issueInstant; var issuer = ssoResponse.issuer ? ssoResponse.issuer.value : "Unknown"; session.setProperty("issueInstant", issueInstant); session.setProperty("issuer", issuer); logger.info("[issueInstant]: " + issueInstant + " [issuer]: " + issuer); // get address from assertion's attribute statement var assertion = ssoResponse.assertion[0]; if (assertion && assertion.attributeStatements) { var statements = assertion.attributeStatements; for (var i = 0; i < statements.length; i++) { var attributes = statements[i].attribute; if (attributes && attributes.length > 0) { // Look for the 'Address' attribute for (var j = 0; j < attributes.length; j++) { if (attributes[j].name === "Address") { var addressValue = attributes[j].attributeValueString; if (addressValue && addressValue.length > 0) { var address = addressValue[0]; logger.info("[postaladdress]: " + address); session.setProperty("address", address); // Redirect based on SAML address attribute if (responseHelper) { if (address === 'UK') { responseHelper.sendRedirect("https://loremipsum.io/"); } else { responseHelper.sendRedirect("https://example.com/"); } redirectOccurred = true; } return redirectOccurred; } } } } } } } catch (e) { logger.error("Error in postSingleSignOnSuccess: " + e.toString()); } return redirectOccurred; }
Configure the IdP
-
Configure Advanced Identity Cloud to use the updated SP adapter script:
-
Go to Applications > Federation > Entity Providers > hosted IdP > Advanced.
-
Select your custom next-generation script from the SP Adapter Script list.
-
Save your changes.
-
-
Map the attributes required for the script:
-
Go to Realms > realm name > Applications > Federation > Entity Providers > hosted IdP > Assertion Processing.
-
Add the following mapping to the Attribute Map:
- SAML Attribute
-
Address - Local Attribute
-
postaladdress
-
Save your changes.
-
-
Update a test user and set their address to
UK:-
Click Identities > test user and set the following attribute:
- Home Address
-
UK
-
Test the script
-
To test your changes, perform an SP-initated SSO flow using your UK test user.
Verify that the user is redirected to
https://loremipsum.ioand that the logging output contains values for the SSO response attributes, for example:INFO: [issueInstant]: 1770649129000 [issuer]: identityprovider1INFO: [postaladdress]: UK