npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easy-ldap-auth

v1.0.0

Published

Makes LDAP authentication easy, with just enough options

Downloads

34

Readme

easy-ldap-auth

Wrapper for the ldapjs library that makes LDAP authentication easy, with just enough options. Inspired by ldap-authentication.

Why?

I wanted a package that was straightforward to use, with a clean and modern codebase. So I wrote this.

Features

  • :sparkles: Easy to use, with a clean API.
  • :wrench: Full type definitions.
  • :shield: Consistent errors.
  • :bug: Easy debugging with logs provided by the debug package.
  • :rocket: Used in production.

Installation

npm install easy-ldap-auth

Usage

let { singleAuthentication, singleSearch, InvalidUserCredentialsError } = require('easy-ldap-auth');

async function authenticate(username, password) {
    try {
        let user = await singleAuthentication({
            url: 'ldap://ldap.forumsys.com:389',
            adminDn: 'cn=read-only-admin,dc=example,dc=com',
            adminPassword: 'password',
            userSearchBaseDn: 'dc=example,dc=com',
            userSearchAttribute: 'uid',
            username,
            password,
        });
        // bind success, `user` is the user LDAP entry.
        return user;
    } catch (error) {
        if (error instanceof InvalidUserCredentialsError) {
            // invalid user credentials
            return null;
        } else {
            // ldapjs errors, like: admin bind error, connection error or timeout
            throw error;
        }
    }
}

async function searchUser(username) {
    try {
        let user = await singleSearch({
            url: 'ldap://ldap.forumsys.com:389',
            adminDn: 'cn=read-only-admin,dc=example,dc=com',
            adminPassword: 'password',
            userSearchBaseDn: 'dc=example,dc=com',
            userSearchAttribute: 'uid',
            username,
        });
        // `user` is the user LDAP entry or null if not found.
        return user;
    } catch (error) {
        // ldapjs errors, like: admin bind error, connection error or timeout
    }
}

Running tests

Clone this repository, then run npm install and then npm test. An internet connection is required as the forumsys' public LDAP server is used for testing.

API

singleSearch(options)

Perform a single search in a LDAP server. First, it binds as an admin user, then searchs for the given username in a given baseDn, and unbind the admin connection.

| Parameter | Type | Description | | --------- | ---- | ----------- | | options | SingleSearchOptions | The options to use (see below). |

Returns a Promise that resolves to the user LDAP entry if it is found, or null if it wasn't.

singleAuthentication(options)

Perform a single authentication agains a LDAP server. First, it binds as an admin user, then searchs for the given username in a given baseDn, and unbind the admin connection. If the user is found, the function tries to bind to that user, resolving to the previous info if success, and then unbinding the user. If anything goes wrong, it throws an error. If the credentials for the user are invalid, it throws an InvalidUserCredentialsError.

| Parameter | Type | Description | | --------- | ---- | ----------- | | options | SingleAuthenticationOptions | The options to use (see below). |

Returns a Promise that resolves to the user LDAP entry if the credentials are valid, or rejects with an InvalidUserCredentialsError (see below) if the credentials are invalid.

Objects

SingleSearchOptions

| Property | Type | Description | | --------- | ---- | ----------- | | url | string | The LDAP server URL. | | adminDn | string | The admin user DN. | | adminPassword | string | The admin user password. | | userSearchBaseDn | string | The base DN to search for the user. | | userSearchAttribute | string | The attribute to search for the user. | | username | string | The username to search for. | | timeout | number | (optional) The timeout to use for the connection, in milliseconds. Defaults to 5000. | | tlsOptions | object | (optional) The TLS options to use for the connection. See Node docs for more info. |

SingleAuthenticationOptions (extends SingleSearchOptions)

| Property | Type | Description | | --------- | ---- | ----------- | | password | string | The password to use for the user. |

InvalidUserCredentialsError (extends Error)

| Property | Type | Description | | --------- | ---- | ----------- | | username | string | The username searched. | | userFound | boolean | Whether the user was found or not (if false, invalid user; if true, invalid password). |


License

This sotftware is MIT licensed. Please check the LICENSE file for the full text.