Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

feat(backend:auth): enhance LDAP authentication configuration with upnSuffix and netbiosName parameters

johaven5a5d62317198
diff --git a/backend/src/authentication/auth.config.ts b/backend/src/authentication/auth.config.ts
index 37f0ceda..38a10c7d 100644
--- a/backend/src/authentication/auth.config.ts
+++ b/backend/src/authentication/auth.config.ts
@@ -149,6 +149,14 @@ export class AuthMethodLdapConfig {
@IsOptional()
@IsString()
adminGroup?: string
+
+ @IsOptional()
+ @IsString()
+ upnSuffix?: string
+
+ @IsOptional()
+ @IsString()
+ netbiosName?: string
}
export class AuthConfig {
diff --git a/backend/src/authentication/guards/auth-basic.strategy.ts b/backend/src/authentication/guards/auth-basic.strategy.ts
index bfba94ba..f5f5d6ca 100644
--- a/backend/src/authentication/guards/auth-basic.strategy.ts
+++ b/backend/src/authentication/guards/auth-basic.strategy.ts
@@ -30,6 +30,8 @@ export class AuthBasicStrategy extends PassportStrategy(BasicStrategy, 'basic')
}
async validate(req: FastifyRequest, loginOrEmail: string, password: string): Promise<Omit<UserModel, 'password'> | null> {
+ loginOrEmail = loginOrEmail.trim()
+ password = password.trim()
this.logger.assign({ user: loginOrEmail })
const authBasicUser = `${this.CACHE_KEY_PREFIX}-${req.headers['authorization'].split(' ').at(-1).toLowerCase()}`
const userFromCache: any = await this.cache.get(authBasicUser)
diff --git a/backend/src/authentication/guards/auth-local.strategy.ts b/backend/src/authentication/guards/auth-local.strategy.ts
index fe24aaab..e555cb4f 100755
--- a/backend/src/authentication/guards/auth-local.strategy.ts
+++ b/backend/src/authentication/guards/auth-local.strategy.ts
@@ -5,7 +5,7 @@
*/
import { Injectable, UnauthorizedException } from '@nestjs/common'
-import { PassportStrategy, AbstractStrategy } from '@nestjs/passport'
+import { AbstractStrategy, PassportStrategy } from '@nestjs/passport'
import type { FastifyRequest } from 'fastify'
import { PinoLogger } from 'nestjs-pino'
import { Strategy } from 'passport-local'
@@ -22,6 +22,8 @@ export class AuthLocalStrategy extends PassportStrategy(Strategy, 'local') imple
}
async validate(req: FastifyRequest, loginOrEmail: string, password: string): Promise<UserModel> {
+ loginOrEmail = loginOrEmail.trim()
+ password = password.trim()
this.logger.assign({ user: loginOrEmail })
const user: UserModel = await this.authMethod.validateUser(loginOrEmail, password, req.ip)
if (user) {
diff --git a/backend/src/authentication/services/auth-methods/auth-method-ldap.service.spec.ts b/backend/src/authentication/services/auth-methods/auth-method-ldap.service.spec.ts
index 6d12afd3..f452c87f 100644
--- a/backend/src/authentication/services/auth-methods/auth-method-ldap.service.spec.ts
+++ b/backend/src/authentication/services/auth-methods/auth-method-ldap.service.spec.ts
@@ -201,7 +201,7 @@ describe(AuthMethodLdapService.name, () => {
usersManager.updateAccesses.mockRejectedValueOnce(new Error('updateAccesses success flow boom'))
const resB = await authMethodLdapService.validateUser('john', 'pwd', '192.168.1.10')
expect(adminUsersManager.createUserOrGuest).toHaveBeenCalledWith(
- { login: 'john', email: '[email protected]', password: 'pwd', firstName: 'John', lastName: 'Doe' },
+ { login: 'john', email: '[email protected]', password: 'pwd', firstName: 'John', lastName: 'Doe', role: 1 },
expect.anything() // USER_ROLE.USER
)
expect(resB).toBe(createdUser)
diff --git a/backend/src/authentication/services/auth-methods/auth-method-ldap.service.ts b/backend/src/authentication/services/auth-methods/auth-method-ldap.service.ts
index bc8e9526..451313de 100644
--- a/backend/src/authentication/services/auth-methods/auth-method-ldap.service.ts
+++ b/backend/src/authentication/services/auth-methods/auth-method-ldap.service.ts
@@ -5,7 +5,7 @@
*/
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common'
-import { AndFilter, Client, ClientOptions, Entry, EqualityFilter, InvalidCredentialsError } from 'ldapts'
+import { AndFilter, Client, ClientOptions, Entry, EqualityFilter, InvalidCredentialsError, OrFilter } from 'ldapts'
import { CONNECT_ERROR_CODE } from '../../../app.constants'
import { USER_ROLE } from '../../../applications/users/constants/user'
import type { CreateUserDto, UpdateUserDto } from '../../../applications/users/dto/create-or-update-user.dto'
@@ -23,12 +23,7 @@ type LdapUserEntry = Entry & Record<LDAP_LOGIN_ATTR | (typeof LDAP_COMMON_ATTR)[
@Injectable()
export class AuthMethodLdapService implements AuthMethod {
private readonly logger = new Logger(AuthMethodLdapService.name)
- private readonly servers: string[] = configuration.auth.ldap.servers
- private readonly baseDN: string = configuration.auth.ldap.baseDN
- private readonly filter: string = configuration.auth.ldap.filter
- private readonly loginAttribute: LDAP_LOGIN_ATTR = configuration.auth.ldap.attributes.login
- private readonly emailAttribute: string = configuration.auth.ldap.attributes.email
- private readonly adminGroup: string = configuration.auth.ldap.adminGroup
+ private readonly ldapConfig = configuration.auth.ldap
private clientOptions: ClientOptions = { timeout: 6000, connectTimeout: 6000, url: '' }
constructor(
@@ -37,7 +32,7 @@ export class AuthMethodLdapService implements AuthMethod {
) {}
async validateUser(login: string, password: string, ip?: string, scope?: AUTH_SCOPE): Promise<UserModel> {
- let user: UserModel = await this.usersManager.findUser(this.normalizeLogin(login), false)
+ let user: UserModel = await this.usersManager.findUser(this.dbLogin(login), false)
if (user) {
if (user.isGuest) {
// allow guests to be authenticated from db and check if the current user is defined as active
@@ -64,9 +59,9 @@ export class AuthMethodLdapService implements AuthMethod {
}
}
return null
- } else if (!entry[this.loginAttribute] || !entry[this.emailAttribute]) {
+ } else if (!entry[this.ldapConfig.attributes.login] || !entry[this.ldapConfig.attributes.email]) {
this.logger.error(`${this.validateUser.name} - required ldap fields are missing :
- [${this.loginAttribute}, ${this.emailAttribute}] =>
+ [${this.ldapConfig.attributes.login}, ${this.ldapConfig.attributes.email}] =>
(${JSON.stringify(entry)})`)
return null
}
@@ -77,27 +72,27 @@ export class AuthMethodLdapService implements AuthMethod {
}
private async checkAuth(login: string, password: string): Promise<LdapUserEntry | false> {
- const loginAttr = this.loginAttribute
- const isAD = loginAttr === LDAP_LOGIN_ATTR.SAM || loginAttr === LDAP_LOGIN_ATTR.UPN
+ const ldapLogin = this.buildLdapLogin(login)
+ const isAD = this.ldapConfig.attributes.login === LDAP_LOGIN_ATTR.SAM || this.ldapConfig.attributes.login === LDAP_LOGIN_ATTR.UPN
// AD: bind directly with the user input (UPN or DOMAIN\user)
// Generic LDAP: build DN from login attribute + baseDN
- const bindUserDN = isAD ? login : `${loginAttr}=${login},${this.baseDN}`
+ const bindUserDN = isAD ? ldapLogin : `${this.ldapConfig.attributes.login}=${ldapLogin},${this.ldapConfig.baseDN}`
let client: Client
let error: any
- for (const s of this.servers) {
+ for (const s of this.ldapConfig.servers) {
client = new Client({ ...this.clientOptions, url: s })
try {
await client.bind(bindUserDN, password)
- return await this.checkAccess(client, login)
+ return await this.checkAccess(ldapLogin, client)
} catch (e) {
if (e.errors?.length) {
for (const err of e.errors) {
- this.logger.warn(`${this.checkAuth.name} - ${login} : ${err}`)
+ this.logger.warn(`${this.checkAuth.name} - ${ldapLogin} : ${err}`)
error = err
}
} else {
error = e
- this.logger.warn(`${this.checkAuth.name} - ${login} : ${e}`)
+ this.logger.warn(`${this.checkAuth.name} - ${ldapLogin} : ${e}`)
}
if (error instanceof InvalidCredentialsError) {
return false
@@ -112,10 +107,10 @@ export class AuthMethodLdapService implements AuthMethod {
return false
}
- private async checkAccess(client: Client, login: string): Promise<LdapUserEntry | false> {
- const searchFilter = this.buildUserFilter(login, this.filter)
+ private async checkAccess(login: string, client: Client): Promise<LdapUserEntry | false> {
+ const searchFilter = this.buildUserFilter(login, this.ldapConfig.filter)
try {
- const { searchEntries } = await client.search(this.baseDN, {
+ const { searchEntries } = await client.search(this.ldapConfig.baseDN, {
scope: 'sub',
filter: searchFilter,
attributes: ALL_LDAP_ATTRIBUTES
@@ -172,7 +167,7 @@ export class AuthMethodLdapService implements AuthMethod {
if (Object.keys(identityHasChanged).length > 0) {
try {
if (identityHasChanged?.role != null) {
- if (user.role === USER_ROLE.ADMINISTRATOR && !this.adminGroup) {
+ if (user.role === USER_ROLE.ADMINISTRATOR && !this.ldapConfig.adminGroup) {
// Prevent removing admin role when adminGroup was removed or not defined
delete identityHasChanged.role
}
@@ -213,10 +208,13 @@ export class AuthMethodLdapService implements AuthMethod {
}
private createIdentity(entry: LdapUserEntry, password: string): CreateUserDto {
- const isAdmin = typeof this.adminGroup === 'string' && this.adminGroup && entry[LDAP_COMMON_ATTR.MEMBER_OF]?.includes(this.adminGroup)
+ const isAdmin =
+ typeof this.ldapConfig.adminGroup === 'string' &&
+ this.ldapConfig.adminGroup &&
+ entry[LDAP_COMMON_ATTR.MEMBER_OF]?.includes(this.ldapConfig.adminGroup)
return {
- login: this.normalizeLogin(entry[this.loginAttribute]),
- email: entry[this.emailAttribute] as string,
+ login: this.dbLogin(entry[this.ldapConfig.attributes.login]),
+ email: entry[this.ldapConfig.attributes.email] as string,
password: password,
role: isAdmin ? USER_ROLE.ADMINISTRATOR : USER_ROLE.USER,
...this.getFirstNameAndLastName(entry)
@@ -240,23 +238,48 @@ export class AuthMethodLdapService implements AuthMethod {
return { firstName: '', lastName: '' }
}
- private normalizeLogin(login: string, toLowerCase = true): string {
- const normalized = (login.includes('\\') ? login.split('\\').slice(-1)[0] : login).trim()
- return toLowerCase ? normalized.toLowerCase() : normalized
+ private dbLogin(login: string): string {
+ if (login.includes('@')) {
+ return login.split('@')[0]
+ } else if (login.includes('\\')) {
+ return login.split('\\').slice(-1)[0]
+ }
+ return login
+ }
+
+ private buildLdapLogin(login: string): string {
+ if (this.ldapConfig.attributes.login === LDAP_LOGIN_ATTR.UPN) {
+ if (this.ldapConfig.upnSuffix && !login.includes('@')) {
+ return `${login}@${this.ldapConfig.upnSuffix}`
+ }
+ } else if (this.ldapConfig.attributes.login === LDAP_LOGIN_ATTR.SAM) {
+ if (this.ldapConfig.netbiosName && !login.includes('\\')) {
+ return `${this.ldapConfig.netbiosName}\\${login}`
+ }
+ }
+ return login
}
private buildUserFilter(login: string, extraFilter?: string): string {
// Build a safe LDAP filter to search for a user.
// Important: - Values passed to EqualityFilter are auto-escaped by ldapts
// - extraFilter is appended as-is (assumed trusted configuration)
- // Output: (&(|(userPrincipalName=john.doe)(sAMAccountName=john.doe)(uid=john.doe))(*extraFilter*))
+ // Output: (&(|([email protected])(sAMAccountName=john.doe)(uid=john.doe))(*extraFilter*))
+
+ // Handle the case where the sAMAccountName is provided in domain-qualified format (e.g., SYNC_IN\\user)
+ // Note: sAMAccountName is always stored without the domain in Active Directory.
+ const uid = this.dbLogin(login)
- // OR clause: accept UPN, sAMAccountName, or uid
- const normalizedLogin = this.normalizeLogin(login, false)
+ const or = new OrFilter({
+ filters: [
+ new EqualityFilter({ attribute: LDAP_LOGIN_ATTR.UPN, value: login }),
+ new EqualityFilter({ attribute: LDAP_LOGIN_ATTR.SAM, value: uid }),
+ new EqualityFilter({ attribute: LDAP_LOGIN_ATTR.UID, value: uid })
+ ]
+ })
- const eq = new EqualityFilter({ attribute: this.loginAttribute, value: normalizedLogin })
// Convert to LDAP filter string
- let filterString = new AndFilter({ filters: [eq] }).toString()
+ let filterString = new AndFilter({ filters: [or] }).toString()
// Optionally append an extra filter from config (trusted source)
if (extraFilter && extraFilter.trim()) {
diff --git a/environment/environment.dist.yaml b/environment/environment.dist.yaml
index 34e5b581..171b41b1 100755
--- a/environment/environment.dist.yaml
+++ b/environment/environment.dist.yaml
@@ -120,7 +120,7 @@ auth:
# filter, e.g: (acl=admin)
filter:
attributes:
- # login attribute: `uid` | `sAMAccountName` | `userPrincipalName`
+ # login attribute: `uid` | `sAMAccountName` | `userPrincipalName`, used to authenticate the user
# default: `uid`
login: uid
# email attribute: `mail` or `email`
@@ -128,6 +128,10 @@ auth:
email: mail
# adminGroup: The CN of a group containing Sync-in administrators (e.g., administrators)
adminGroup:
+ # upnSuffix: AD domain suffix used with `userPrincipalName` to build UPN-style logins (e.g., user@`sync-in.com`)
+ upnSuffix:
+ # netbiosName: NetBIOS domain name used with `sAMAccountName` to build legacy logins (e.g., `SYNC_IN`\user)
+ netbiosName:
applications:
files:
# required