Evo.Auth/apps/api/src/ldap/ldap.service.ts
2023-10-16 12:15:27 +03:00

45 lines
1.1 KiB
TypeScript

import { env } from '../config/env';
import type { User } from '../types/user';
import type { LdapUser } from './types/user';
import { Injectable } from '@nestjs/common';
import type { AuthenticationOptions } from 'ldap-authentication';
import { authenticate } from 'ldap-authentication';
@Injectable()
export class LdapService {
public async authenticate(login: string, password?: string) {
const options: AuthenticationOptions = {
adminDn: env.LDAP_BIND_DN,
adminPassword: env.LDAP_BIND_CREDENTIALS,
ldapOpts: {
url: env.LDAP_URL,
},
userPassword: password,
userSearchBase: env.LDAP_BASE,
username: login,
usernameAttribute: env.LDAP_ATTRIBUTE,
verifyUserExists: password === undefined,
};
const {
displayName,
department,
title,
mail,
sAMAccountName: username,
}: LdapUser = await authenticate(options);
const user: User = {
department,
displayName,
domain: env.LDAP_DOMAIN,
domainName: `${env.LDAP_DOMAIN}\\${username}`,
mail,
position: title,
username,
};
return user;
}
}