Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
feat(backend:auth): allow cn LDAP attribute and add AD-specific logic
Hadrien Kerlero6998b1af24bc
diff --git a/backend/src/applications/users/constants/user.ts b/backend/src/applications/users/constants/user.tsindex d1d0157a..c12bb5e0 100644--- a/backend/src/applications/users/constants/user.ts+++ b/backend/src/applications/users/constants/user.ts@@ -6,7 +6,7 @@export const USER_PASSWORD_MIN_LENGTH = 8export const USER_MAX_PASSWORD_ATTEMPTS = 10-export const USER_LOGIN_VALIDATION = /^[a-zA-Z0-9\-\\._]{2,255}$/+export const USER_LOGIN_VALIDATION = /^(?! )(?!.* $)[a-zA-Z0-9\-\\._ ]{2,255}$/export const USER_PATH = {TMP: 'tmp',diff --git a/backend/src/authentication/constants/auth-ldap.ts b/backend/src/authentication/constants/auth-ldap.tsindex 24c7b11c..25e6b1fe 100644--- a/backend/src/authentication/constants/auth-ldap.ts+++ b/backend/src/authentication/constants/auth-ldap.ts@@ -6,6 +6,7 @@export enum LDAP_LOGIN_ATTR {UID = 'uid',+ CN = 'cn',SAM = 'sAMAccountName',UPN = 'userPrincipalName'}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.tsindex 451313de..436675d5 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@@ -24,6 +24,7 @@ type LdapUserEntry = Entry & Record<LDAP_LOGIN_ATTR | (typeof LDAP_COMMON_ATTR)[export class AuthMethodLdapService implements AuthMethod {private readonly logger = new Logger(AuthMethodLdapService.name)private readonly ldapConfig = configuration.auth.ldap+ private readonly isAD = this.ldapConfig.attributes.login === LDAP_LOGIN_ATTR.SAM || this.ldapConfig.attributes.login === LDAP_LOGIN_ATTR.UPNprivate clientOptions: ClientOptions = { timeout: 6000, connectTimeout: 6000, url: '' }constructor(@@ -35,7 +36,7 @@ export class AuthMethodLdapService implements AuthMethod {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+ // Allow guests to be authenticated from db and check if the current user is defined as activereturn this.usersManager.logUser(user, password, ip)}if (!user.isActive) {@@ -49,12 +50,12 @@ export class AuthMethodLdapService implements AuthMethod {if (user) {let authSuccess = falseif (scope) {- // try user app password+ // Try user app passwordauthSuccess = await this.usersManager.validateAppPassword(user, password, ip, scope)}this.usersManager.updateAccesses(user, ip, authSuccess).catch((e: Error) => this.logger.error(`${this.validateUser.name} : ${e}`))if (authSuccess) {- // logged with app password+ // Logged with app passwordreturn user}}@@ -73,10 +74,9 @@ export class AuthMethodLdapService implements AuthMethod {private async checkAuth(login: string, password: string): Promise<LdapUserEntry | false> {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 ? ldapLogin : `${this.ldapConfig.attributes.login}=${ldapLogin},${this.ldapConfig.baseDN}`+ const bindUserDN = this.isAD ? ldapLogin : `${this.ldapConfig.attributes.login}=${ldapLogin},${this.ldapConfig.baseDN}`let client: Clientlet error: anyfor (const s of this.ldapConfig.servers) {@@ -137,7 +137,7 @@ export class AuthMethodLdapService implements AuthMethod {private async updateOrCreateUser(identity: CreateUserDto, user: UserModel): Promise<UserModel> {if (user === null) {- // create+ // Createconst createdUser = await this.adminUsersManager.createUserOrGuest(identity, identity.role)const freshUser = await this.usersManager.fromUserId(createdUser.id)if (!freshUser) {@@ -150,7 +150,7 @@ export class AuthMethodLdapService implements AuthMethod {this.logger.error(`${this.updateOrCreateUser.name} - user login mismatch : ${identity.login} !== ${user.login}`)throw new HttpException('Account matching error', HttpStatus.FORBIDDEN)}- // update: check if user information has changed+ // Update: check if user information has changedconst identityHasChanged: UpdateUserDto = Object.fromEntries((await Promise.all(@@ -168,7 +168,7 @@ export class AuthMethodLdapService implements AuthMethod {try {if (identityHasChanged?.role != null) {if (user.role === USER_ROLE.ADMINISTRATOR && !this.ldapConfig.adminGroup) {- // Prevent removing admin role when adminGroup was removed or not defined+ // Prevent removing the admin role when adminGroup was removed or not defineddelete identityHasChanged.role}}@@ -180,7 +180,7 @@ export class AuthMethodLdapService implements AuthMethod {}Object.assign(user, identityHasChanged)if ('lastName' in identityHasChanged || 'firstName' in identityHasChanged) {- // force fullName update in current user model+ // Force fullName update in the current user modeluser.setFullName(true)}} catch (e) {@@ -264,18 +264,22 @@ export class AuthMethodLdapService implements AuthMethod {// 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: (&(|([email protected])(sAMAccountName=john.doe)(uid=john.doe))(*extraFilter*))+ // Output: (&(|([email protected])(sAMAccountName=john.doe)(cn=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)+ const dbLogin = this.dbLogin(login)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 })- ]+ filters: this.isAD+ ? [+ new EqualityFilter({ attribute: LDAP_LOGIN_ATTR.UPN, value: login }),+ new EqualityFilter({ attribute: LDAP_LOGIN_ATTR.SAM, value: dbLogin })+ ]+ : [+ new EqualityFilter({ attribute: LDAP_LOGIN_ATTR.UID, value: dbLogin }),+ new EqualityFilter({ attribute: LDAP_LOGIN_ATTR.CN, value: dbLogin })+ ]})// Convert to LDAP filter stringdiff --git a/environment/environment.dist.yaml b/environment/environment.dist.yamlindex dc21ce6f..42ad0a78 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`, used to authenticate the user+ # login attribute: `uid` | `cn` | `sAMAccountName` | `userPrincipalName`, used to authenticate the user# default: `uid`login: uid# email attribute: `mail` or `email`