0.2.1 โ€ข Published 5 months ago

@raddiamond/nexauth-core v0.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

NexAuth Core

A pluggable authentication core supporting Local, Active Directory (AD/LDAP), and UI-based login strategies. Designed for multi-tenant or flexible backend environments using TypeScript and TypeORM.


๐Ÿ”ง Installation

npm install @raddiamond/nexauth-core

โœจ Features

  • ๐Ÿ” Local database-backed authentication
  • ๐Ÿง‘โ€๐Ÿ’ผ Active Directory (LDAP) login support
  • ๐Ÿ–ฅ๏ธ React UI components for authentication
  • ๐Ÿข Multi-tenant support with client secrets
  • โšก Custom user entity support
  • ๐Ÿ”„ Full control over how users and roles are resolved
  • ๐Ÿงช Drop-in compatibility with passport via a custom strategy

๐Ÿง  Overview

This library expects a TenantContext object to define the authentication method (local, AD, or UI) and its required configuration. It supports:

  • Your own user entity via LocalUserLookupOptions
  • External LDAP servers via ADUserLookupOptions
  • UI-based authentication via UIProviderConfig
  • Client secrets for secure communication
  • Custom password field and logic
  • Custom role mapping

๐Ÿ”„ TenantContext Interface

import { DataSource } from 'typeorm';

export interface TenantContext {
  identityProviderType: 'local' | 'ad' | 'ui';
  dbConnection?: DataSource;
  tenantId?: string;
  clientSecret?: string;

  localUser?: LocalUserLookupOptions<any>;
  adConfig?: {
    url: string;
    bindDN: string;
    bindCredentials: string;
    baseDN: string;
    userLookup: ADUserLookupOptions;
  };
  uiConfig?: UIProviderConfig;
}

๐Ÿ” LocalUserLookupOptions

Used when identityProviderType = 'local'. This allows authentication against a custom entity (e.g., User) in your database.

export interface LocalUserLookupOptions<T> {
  entity: new () => T;
  matchField: keyof T;
  passwordField?: string; // Defaults to "passwordHash"
  comparePassword?: (plain: string, hashed: string) => Promise<boolean>; // Defaults to bcrypt.compare
  extractRoles?: (user: T) => string[];
}

Example

import { createIdentityProvider } from '@raddiamond/nexauth-core';
import { User } from './entities/User';
import dataSource from './db';

const provider = createIdentityProvider({
  identityProviderType: 'local',
  dbConnection: dataSource,
  localUser: {
    entity: User,
    matchField: 'email',
    passwordField: 'encrypted_password',
    extractRoles: (user) => user.roles || [],
  },
});

๐Ÿง‘โ€๐Ÿ’ผ ADUserLookupOptions

Used when identityProviderType = 'ad'. This performs authentication via LDAP/AD bind + user search.

export interface ADUserLookupOptions {
  matchField: string; // e.g., "mail", "sAMAccountName"
  extractRoles?: (ldapUser: Record<string, string>) => string[];
  additionalAttributes?: string[]; // Extra LDAP fields to fetch
}

Example

import { createIdentityProvider } from '@raddiamond/nexauth-core';

const provider = createIdentityProvider({
  identityProviderType: 'ad',
  adConfig: {
    url: 'ldap://your-ad-server:389',
    bindDN: 'cn=admin,dc=example,dc=org',
    bindCredentials: 'password',
    baseDN: 'dc=example,dc=org',
    userLookup: {
      matchField: 'mail',
      extractRoles: (user) => user.memberOf ? user.memberOf.split(',') : [],
      additionalAttributes: ['memberOf'],
    },
  },
});

๐Ÿ–ฅ๏ธ UI Components

NexAuth Core now includes React components for authentication UIs:

import { AuthUI } from '@raddiamond/nexauth-core';

function App() {
  return (
    <AuthUI
      clientId="your-client-id"
      clientSecret="your-client-secret"
      tenantId="your-tenant-id"
      apiUrl="https://your-backend.com/api"
      logo="/path/to/logo.png"
      theme="light"
      showRegister={true}
    />
  );
}

See UI_COMPONENTS.md for full documentation on UI components.


๐Ÿข Tenant Management

The TenantManager class helps manage multiple tenants and client secrets:

import { TenantManager } from '@raddiamond/nexauth-core';

// Initialize tenant manager
const tenantManager = new TenantManager();

// Register a tenant
tenantManager.registerTenant('tenant1', {
  identityProviderType: 'local',
  dbConnection: dataSource,
  localUser: {
    entity: User,
    matchField: 'email',
  }
});

// Configure UI provider
const uiConfig = tenantManager.configureUIProvider(
  'tenant1',        // tenantId
  'my-client',      // clientId
  '/auth/callback', // redirectUrl
  ['https://myapp.com'] // allowedOrigins
);

console.log(`Client Secret: ${uiConfig.clientSecret}`);

๐Ÿงช Passport Integration

A NexAuthStrategy is provided for Passport.js integration:

import passport from 'passport';
import { NexAuthStrategy } from '@raddiamond/nexauth-core';
import { User } from './entities/User';
import dataSource from './db';

passport.use(
  new NexAuthStrategy(
    {
      field: 'username', // form field name
      providerOptions: {
        identityProviderType: 'local',
        dbConnection: dataSource,
        localUser: {
          entity: User,
          matchField: 'email',
          passwordField: 'password',
        },
      },
    },
    (user, done) => {
      done(null, user); // Passport user session
    }
  )
);

๐Ÿงฑ Session Example

passport.serializeUser((user, done) => {
  done(null, user);
});

passport.deserializeUser((user, done) => {
  done(null, user);
});

๐Ÿ“„ License

MIT โ€“ ยฉ Raddiamond LTD