FusionAuth developer image
FusionAuth developer logo
  • Back to site
  • Expert Advice
  • Blog
  • Developers
  • Downloads
  • Account
  • Contact sales
Navigate to...
  • Welcome
  • Getting Started
    • Getting Started
    • 5-minute Setup Guide
      • Overview
      • Docker
      • Fast Path
      • Sandbox
    • Setup Wizard & First Login
    • Register a User and Login
    • Self-service Registration
    • Start and Stop FusionAuth
    • Core Concepts
      • Overview
      • Users
      • Roles
      • Groups
      • Registrations
      • Applications
      • Tenants
      • Identity Providers
      • Authentication/Authorization
      • Integration Points
    • Example Apps
      • Overview
      • Dart
      • Go
      • Java
      • JavaScript
      • .NET Core
      • PHP
      • Python
      • Ruby
    • Tutorials
      • Overview
      • Java Spring
      • Python Django
  • Installation Guide
    • Overview
    • System Requirements
    • Server Layout
    • Cloud
    • Cluster
    • Docker
    • Fast Path
    • Kubernetes
      • Overview
      • Deployment Guide
      • Minikube Setup
      • Amazon EKS Setup
      • Google GKE Setup
      • Microsoft AKS Setup
    • Kickstart™
    • Homebrew
    • Marketplaces
    • Packages
    • Database
    • FusionAuth App
    • FusionAuth Search
    • Common Configuration
  • Migration Guide
    • Overview
    • General
    • Auth0
    • Keycloak
    • Amazon Cognito
    • Firebase
    • Microsoft Azure AD B2C
    • Tutorial
  • Admin Guide
    • Overview
    • Account Portal
    • Config Management
    • Editions and Features
    • Key Rotation
    • Licensing
    • Monitoring
    • Prometheus Setup
    • Proxy Setup
    • Reference
      • Overview
      • Configuration
      • CORS
      • Data Types
      • Hosted Login Pages Cookies
      • Known Limitations
      • Password Hashes
    • Releases
    • Roadmap
    • Search And FusionAuth
    • Securing
    • Switch Search Engines
    • Technical Support
    • Troubleshooting
    • Upgrading
    • WebAuthn
  • Login Methods
    • Identity Providers
      • Overview
      • Apple
      • Epic Games
      • External JWT
        • Overview
        • Example
      • Facebook
      • Google
      • HYPR
      • LinkedIn
      • Nintendo
      • OpenID Connect
        • Overview
        • Amazon Cognito
        • Azure AD
        • Discord
        • Github
        • Okta
      • Sony PlayStation Network
      • Steam
      • Twitch
      • Twitter
      • SAML v2
        • Overview
        • ADFS
        • Azure AD
        • Okta
      • SAML v2 IdP Initiated
        • Overview
        • Okta
      • Xbox
    • OIDC & OAuth 2.0
      • Overview
      • Endpoints
      • Tokens
      • OAuth Modes
      • URL Validation
    • Passwordless
      • Overview
      • Magic Links
      • WebAuthn & Passkeys
    • SAML v2 IdP
      • Overview
      • Google
      • PagerDuty
      • Tableau Cloud
      • Zendesk
  • Developer Guide
    • Overview
    • API Gateways
      • Overview
      • Amazon API Gateway
      • Kong Gateway
      • ngrok Cloud Edge
    • Client Libraries & SDKs
      • Overview
      • Dart
      • Go
      • Java
      • JavaScript
      • .NET Core
      • Node
      • OpenAPI
      • PHP
      • Python
      • React
      • Ruby
      • Typescript
    • Events & Webhooks
      • Overview
      • Writing a Webhook
      • Securing Webhooks
      • Events
        • Overview
        • Audit Log Create
        • Event Log Create
        • JWT Public Key Update
        • JWT Refresh
        • JWT Refresh Token Revoke
        • Kickstart Success
        • Group Create
        • Group Create Complete
        • Group Delete
        • Group Delete Complete
        • Group Update
        • Group Update Complete
        • Group Member Add
        • Group Member Add Complete
        • Group Member Remove
        • Group Member Remove Complete
        • Group Member Update
        • Group Member Update Complete
        • User Action
        • User Bulk Create
        • User Create
        • User Create Complete
        • User Deactivate
        • User Delete
        • User Delete Complete
        • User Email Update
        • User Email Verified
        • User IdP Link
        • User IdP Unlink
        • User Login Failed
        • User Login Id Dup. Create
        • User Login Id Dup. Update
        • User Login New Device
        • User Login Success
        • User Login Suspicious
        • User Password Breach
        • User Password Reset Send
        • User Password Reset Start
        • User Password Reset Success
        • User Password Update
        • User Reactivate
        • User Reg. Create
        • User Reg. Create Complete
        • User Reg. Delete
        • User Reg. Delete Complete
        • User Registration Update
        • User Reg. Update Complete
        • User Reg. Verified
        • User 2FA Method Add
        • User 2FA Method Remove
        • User Update
        • User Update Complete
    • Guides
      • Overview
      • Application Specific Email Templates
      • Authentication Tokens
      • Exposing A Local Instance
      • JSON Web Tokens
      • Key Master
      • Localization and Internationalization
      • Multi-Factor Authentication
      • Multi-Tenant
      • Passwordless
      • Registration-based Email Verification
      • Searching With Elasticsearch
      • Securing Your APIs
      • Silent Mode
      • Single Sign-on
      • Two Factor (pre 1.26)
    • Integrations
      • Overview
      • CleanSpeak
      • Kafka
      • Twilio
    • Plugins
      • Overview
      • Writing a Plugin
      • Custom Password Hashing
    • User Control & Gating
      • Overview
      • Gate Unverified Users
      • Gate Unverified Registrations
      • User Account Lockout
  • Customization
    • Email & Templates
      • Overview
      • Configure Email
      • Email Templates
      • Email Variables
      • Message Templates
    • Lambdas
      • Overview
      • Apple Reconcile
      • Client Cred. JWT Populate
      • Epic Games Reconcile
      • External JWT Reconcile
      • Facebook Reconcile
      • Google Reconcile
      • HYPR Reconcile
      • JWT Populate
      • LDAP Connector Reconcile
      • LinkedIn Reconcile
      • Nintendo Reconcile
      • OpenID Connect Reconcile
      • SAML v2 Populate
      • SAML v2 Reconcile
      • SCIM Group Req. Converter
      • SCIM Group Resp. Convtr.
      • SCIM User Req. Converter
      • SCIM User Resp. Converter
      • Self-Service Registration
      • Sony PSN Reconcile
      • Steam Reconcile
      • Twitch Reconcile
      • Twitter Reconcile
      • Xbox Reconcile
    • Messengers
      • Overview
      • Generic Messenger
      • Twilio Messenger
    • Themes
      • Overview
      • Examples
      • Helpers
      • Localization
      • Template Variables
      • Kickstart Custom Theme
  • Premium Features
    • Overview
    • Advanced Registration Forms
    • Advanced Threat Detection
    • Application Specific Themes
    • Breached Password Detection
    • Connectors
      • Overview
      • Generic Connector
      • LDAP Connector
      • FusionAuth Connector
    • Entity Management
    • SCIM
      • Overview
      • Azure AD Client
      • Okta Client
      • SCIM-SDK
    • Self Service Account Mgmt
      • Overview
      • Updating User Data & Password
      • Add Two-Factor Authenticator
      • Add Two-Factor Email
      • Add Two-Factor SMS
      • Add WebAuthn Passkey
      • Customizing
      • Troubleshooting
    • WebAuthn
  • APIs
    • Overview
    • Authentication
    • Errors
    • API Explorer
    • Actioning Users
    • API Keys
    • Applications
    • Audit Logs
    • Connectors
      • Overview
      • Generic
      • LDAP
    • Consents
    • Emails
    • Entity Management
      • Overview
      • Entities
      • Entity Types
      • Grants
    • Event Logs
    • Families
    • Forms
    • Form Fields
    • Groups
    • Identity Providers
      • Overview
      • Links
      • Apple
      • External JWT
      • Epic Games
      • Facebook
      • Google
      • HYPR
      • LinkedIn
      • Nintendo
      • OpenID Connect
      • SAML v2
      • SAML v2 IdP Initiated
      • Sony PlayStation Network
      • Steam
      • Twitch
      • Twitter
      • Xbox
    • Integrations
    • IP Access Control Lists
    • JWT
    • Keys
    • Lambdas
    • Login
    • Message Templates
    • Messengers
      • Overview
      • Generic
      • Twilio
    • Multi-Factor/Two Factor
    • Passwordless
    • Reactor
    • Registrations
    • Reports
    • SCIM
      • Overview
      • SCIM User
      • SCIM Group
      • SCIM EnterpriseUser
      • SCIM Service Provider Config.
    • System
    • Tenants
    • Themes
    • Users
    • User Actions
    • User Action Reasons
    • User Comments
    • WebAuthn
    • Webhooks
  • Release Notes

    LDAP Connector Reconcile Lambda

    LDAP Connector Reconcile lambda

    When an LDAP Connector is used to authenticate a user based upon the Tenant connector policies, the LDAP Connector lambda is used to map the LDAP attributes into a FusionAuth user. This lambda runs every time the LDAP Connector runs.

    When you create a new lambda using the FusionAuth UI we will provide you an empty function for you to implement.

    If you are using the API to create the lambda you will need to ensure your function has the following signature:

    
    function reconcile(user, userAttributes) {
      // Lambda code goes here
    }

    This lambda must contain a function named reconcile that takes two parameters. The parameters that the lambda is passed are:

    • user - the FusionAuth User object. You can modify this, however you cannot modify the username or email attributes once the account is linked.

    • userAttributes - the user attributes returned from LDAP during authentication. This is read-only.

    The FusionAuth user object is well documented the User API documentation. The userAttributes object may contain various values returned by the LDAP server.

    LDAP Attributes

    LDAP attributes can be returned to FusionAuth in a string form or a byte array. Some attributes are considered non-string values and need to be provided in a byte array to be useful in the Lambda function.

    A non-string attribute should be requested as a byte array. To request an attribute as a byte array, use the ;binary LDAP attribute option as a suffix on your requested attribute. For example, instead of requesting objectGUID, you will request objectGUID;binary.

    Helper Functions

    FusionAuth provides helper functions available in the Lambda function under the namespace FusionAuth.

    Active Directory Object GUID to UUID

    When using this connector with Microsoft Active Directory, the objectGUID attribute will need to be configured to be returned as a byte array. This can be accomplished by appending the suffix ;binary as an LDAP attribute option to the objectGUID in the requested attributes configuration.

    Values requested as a byte array will be provided to the lambda function as a Base64 encoded string. Here is an example usage of the FusionAuth helper to convert this base64 encoded string representation of the objectGUID to a UUID.

    
    // Example usage to convert a Base64 encoded Microsoft Active Directory objectGUID to a valid FusionAuth UUID
    user.id = FusionAuth.ActiveDirectory.b64GuidToString(userAttributes['objectGUID;binary']);

    Assigning the lambda

    Once a lambda is created, you may use it when adding an LDAP Connector in the Connector configuration.

    Navigate to Settings → Connectors and click Add and select LDAP when prompted to select a connector type.

    Example lambda

    The following is a simple example of an LDAP Connector reconcile lambda. You will need to modify it to suit your needs.

    
    // This is an example LDAP Connector reconcile, modify this to your liking.
    function reconcile(user, userAttributes) {
    
      // Uncomment this line to see the userAttributes object printed to the event log
      // console.info(JSON.stringify(userAttributes, null, 2));
    
      // This assumes the 'uid' attribute is a string form of a UUID in the format
      // `8-4-4-4-12`. It will be necessary to ensure an attribute is returned by your LDAP
      // connection that can be used for the FusionAuth user Id.
      user.id = userAttributes.uid;
      user.active = true;
    
      // if migrating users, tag them by uncommenting the below lines
      // user.data = {};
      // user.data.migrated = true;
    
      user.email = userAttributes.mail;
      user.fullName = userAttributes.cn;
    
      // In this example, the registration is hard coded, you may also build this
      // dynamically based upon the returned LDAP attributes.
      user.registrations = [{
        applicationId: "5d562fea-9ba9-4d5c-b4a3-e57bb254d6db",
        roles = ['user', 'admin']
      }];
    
    }

    Example Active Directory lambda

    Active Directory does not have a uid attribute, and delivers the GUID as a binary value.

    To enable the Connector to work with Active Directory, you must request this attribute: objectGUID;binary, decode it into a binary GUID, then convert that to a version 4 UUID. Then you can assign that value to the user.id property.

    objectGUID;binary must be specified in your Requested Attributes of the LDAP connector. In other words, specifying only objectGUID will not pass the proper attribute value to the LDAP reconcile lambda.

    The below Lambda does this:

    
    // Using the response from an LDAP connector, reconcile the User.
    function reconcile(user, userAttributes) {
    
      user.email = userAttributes.userPrincipalName;
      user.firstName = userAttributes.givenName;
      user.lastName  = userAttributes.sn;
      user.active    = true;
    
      // if you are using FusionAuth 1.19.7 or later, you can use the built in method and omit the decodeBase64 and guidToString functions. This is recommended.
      // user.id = FusionAuth.ActiveDirectory.b64GuidToString(userAttributes['objectGuid;binary']);
    
      user.id = guidToString(userAttributes['objectGUID;binary']);
    }
    
    function decodeBase64(string)
    {
      var b=0,l=0, r='',
        m='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
      string.split('').forEach(function (v) {
        b=(b<<6)+m.indexOf(v); l+=6;
        if (l>=8) r+=String.fromCharCode((b>>>(l-=8))&0xff);
      });
      return r;
    }
    
    function guidToString(b64)
    {
        var x = decodeBase64(b64);
    
        var ret = "";
    
        for (i = 3; i >= 0; i--)
        {
            ret += ('00'+x.charCodeAt(i).toString(16)).substr(-2,2);
        }
        ret += "-";
        for (i = 5; i >= 4; i--)
        {
            //ret = ret + ('00' + (charCode & 0xFF00) >> 8);
            ret += ('00'+x.charCodeAt(i).toString(16)).substr(-2,2);
        }
        ret += "-";
        for (i = 7; i >= 6; i--)
        {
            //ret = ret + ('00' + (charCode & 0xFF00) >> 8);
            ret += ('00'+x.charCodeAt(i).toString(16)).substr(-2,2);
        }
        ret += "-";
        for (i = 8; i <= 9; i++)
        {
            //ret = ret + ('00' + (charCode & 0xFF00) >> 8);
            ret += ('00'+x.charCodeAt(i).toString(16)).substr(-2,2);
        }
        ret += "-";
        for (i = 10; i < 16; i++)
        {
            //ret = ret + ('00' + (charCode & 0xFF00) >> 8);
            ret += ('00'+x.charCodeAt(i).toString(16)).substr(-2,2);
        }
    
        return ret;
    }

    Thanks to community member Bradley Kite for providing this code.

    Feedback

    How helpful was this page?

    See a problem?

    File an issue in our docs repo

    Have a question or comment to share?

    Visit the FusionAuth community forum.

    © 2023 FusionAuth
    How-to
    Subscribe for developer updates