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): add LDAP/OIDC local password fallback and admin break-glass access
johaven23a93b5ae104
diff --git a/backend/src/applications/users/services/users-manager.service.ts b/backend/src/applications/users/services/users-manager.service.tsindex 2194c414..37f802f6 100755--- a/backend/src/applications/users/services/users-manager.service.ts+++ b/backend/src/applications/users/services/users-manager.service.ts@@ -71,7 +71,7 @@ export class UsersManager {return user ? new UserModel(user, removePassword) : null}- async logUser(user: UserModel, password: string, ip: string, scope?: AUTH_SCOPE): Promise<UserModel> {+ async logUser(user: UserModel, password: string, ip: string, scope?: AUTH_SCOPE): Promise<UserModel | null> {this.validateUserAccess(user, ip)let authSuccess: boolean = await comparePassword(password, user.password)if (!authSuccess && scope) {diff --git a/backend/src/authentication/auth.config.ts b/backend/src/authentication/auth.config.tsindex 8a7f0c3b..af21ab88 100644--- a/backend/src/authentication/auth.config.ts+++ b/backend/src/authentication/auth.config.ts@@ -109,17 +109,17 @@ export class AuthConfig {@Type(() => AuthTokenConfig)token: AuthTokenConfig- @ValidateIf((o: AuthConfig) => o.method === 'ldap')+ @ValidateIf((o: AuthConfig) => o.method === AUTH_PROVIDER.LDAP)@IsDefined()@IsObject()@ValidateNested()@Type(() => AuthMethodLDAPConfig)ldap: AuthMethodLDAPConfig- @ValidateIf((o: AuthConfig) => o.method === 'oidc')+ @ValidateIf((o: AuthConfig) => o.method === AUTH_PROVIDER.OIDC)@IsDefined()@IsObject()@ValidateNested()@Type(() => AuthMethodOIDCConfig)- oidc: AuthMethodOIDCConfig = new AuthMethodOIDCConfig()+ oidc: AuthMethodOIDCConfig}diff --git a/backend/src/authentication/providers/ldap/auth-ldap.config.ts b/backend/src/authentication/providers/ldap/auth-ldap.config.tsindex 8a88cadd..9ec70da0 100644--- a/backend/src/authentication/providers/ldap/auth-ldap.config.ts+++ b/backend/src/authentication/providers/ldap/auth-ldap.config.ts@@ -8,6 +8,7 @@ import { Transform, Type } from 'class-transformer'import {ArrayNotEmpty,IsArray,+ IsBoolean,IsDefined,IsEnum,IsNotEmpty,@@ -64,4 +65,8 @@ export class AuthMethodLDAPConfig {@IsOptional()@IsString()netbiosName?: string++ @IsOptional()+ @IsBoolean()+ enablePasswordAuthFallback? = true}diff --git a/backend/src/authentication/providers/ldap/auth-provider-ldap.service.ts b/backend/src/authentication/providers/ldap/auth-provider-ldap.service.tsindex 3623fd85..c2715996 100644--- a/backend/src/authentication/providers/ldap/auth-provider-ldap.service.ts+++ b/backend/src/authentication/providers/ldap/auth-provider-ldap.service.ts@@ -16,6 +16,7 @@ import { comparePassword, splitFullName } from '../../../common/functions'import { configuration } from '../../../configuration/config.environment'import type { AUTH_SCOPE } from '../../constants/scope'import { AuthProvider } from '../auth-providers.models'+import type { AuthMethodLDAPConfig } from './auth-ldap.config'import { ALL_LDAP_ATTRIBUTES, LDAP_COMMON_ATTR, LDAP_LOGIN_ATTR } from './auth-ldap.constants'type LdapUserEntry = Entry & Record<LDAP_LOGIN_ATTR | (typeof LDAP_COMMON_ATTR)[keyof typeof LDAP_COMMON_ATTR], string>@@ -23,7 +24,7 @@ type LdapUserEntry = Entry & Record<LDAP_LOGIN_ATTR | (typeof LDAP_COMMON_ATTR)[@Injectable()export class AuthProviderLDAP implements AuthProvider {private readonly logger = new Logger(AuthProviderLDAP.name)- private readonly ldapConfig = configuration.auth.ldap+ private readonly ldapConfig: AuthMethodLDAPConfig = configuration.auth.ldapprivate 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: '' }@@ -36,38 +37,49 @@ export class AuthProviderLDAP implements AuthProvider {// Find user from his login or emaillet 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- return this.usersManager.logUser(user, password, ip)+ if (user.isGuest || scope) {+ // Allow local password authentication for guest users and application scopes (app passwords)+ return this.usersManager.logUser(user, password, ip, scope)}if (!user.isActive) {this.logger.error(`${this.validateUser.name} - user *${user.login}* is locked`)throw new HttpException('Account locked', HttpStatus.FORBIDDEN)}}- // If a user was found, use the stored login. This allows logging in with an email.- const entry: false | LdapUserEntry = await this.checkAuth(user?.login || login, password)+ let ldapErrorMessage: string+ let entry: false | LdapUserEntry = false+ try {+ // If a user was found, use the stored login. This allows logging in with an email.+ entry = await this.checkAuth(user?.login || login, password)+ } catch (e) {+ ldapErrorMessage = e.message+ }++ // LDAP auth failed or exception raisedif (entry === false) {- // LDAP auth failed- if (user) {- let authSuccess = false- if (scope) {- // Try user app password- authSuccess = 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- return user- }+ // If LDAP is unavailable (connectivity/service error), allow local password fallback.+ // Allow local password authentication for:+ // - admin users (break-glass access)+ // - regular users when password authentication fallback is enabled+ if (user && Boolean(ldapErrorMessage) && (user.isAdmin || this.ldapConfig.enablePasswordAuthFallback)) {+ const localUser = await this.usersManager.logUser(user, password, ip)+ if (localUser) return localUser}++ if (ldapErrorMessage) {+ throw new HttpException(ldapErrorMessage, HttpStatus.SERVICE_UNAVAILABLE)+ }+return null- } else if (!entry[this.ldapConfig.attributes.login] || !entry[this.ldapConfig.attributes.email]) {+ }++ if (!entry[this.ldapConfig.attributes.login] || !entry[this.ldapConfig.attributes.email]) {this.logger.error(`${this.validateUser.name} - required ldap fields are missing :[${this.ldapConfig.attributes.login}, ${this.ldapConfig.attributes.email}] =>(${JSON.stringify(entry)})`)return null}+const identity = this.createIdentity(entry, password)user = await this.updateOrCreateUser(identity, user)this.usersManager.updateAccesses(user, ip, true).catch((e: Error) => this.logger.error(`${this.validateUser.name} : ${e}`))@@ -103,8 +115,11 @@ export class AuthProviderLDAP implements AuthProvider {await client.unbind()}}- if (error && CONNECT_ERROR_CODE.has(error.code)) {- throw new HttpException('Authentication service error', HttpStatus.INTERNAL_SERVER_ERROR)+ if (error) {+ this.logger.error(`${this.checkAuth.name} - ${error}`)+ if (CONNECT_ERROR_CODE.has(error.code)) {+ throw new Error('Authentication service error')+ }}return false}diff --git a/backend/src/authentication/providers/oidc/auth-oidc.config.ts b/backend/src/authentication/providers/oidc/auth-oidc.config.tsindex 0b228042..981e6076 100644--- a/backend/src/authentication/providers/oidc/auth-oidc.config.ts+++ b/backend/src/authentication/providers/oidc/auth-oidc.config.ts@@ -38,6 +38,10 @@ export class AuthMethodOIDCConfig {@IsBoolean()autoCreateUser? = true+ @IsOptional()+ @IsBoolean()+ enablePasswordAuth? = true+@IsOptional()@IsBoolean()skipSubjectCheck? = falsediff --git a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.tsindex 1c28b7be..39f70d12 100644--- a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts+++ b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts@@ -35,7 +35,7 @@ import type { AUTH_SCOPE } from '../../constants/scope'import { TOKEN_TYPE } from '../../interfaces/token.interface'import { AUTH_PROVIDER } from '../auth-providers.constants'import { AuthProvider } from '../auth-providers.models'-import { AuthMethodOIDCConfig } from './auth-oidc.config'+import type { AuthMethodOIDCConfig } from './auth-oidc.config'import { OAuthCookie, OAuthCookieSettings, OAuthTokenEndpoint } from './auth-oidc.constants'@Injectable()@@ -160,7 +160,6 @@ export class AuthProviderOIDC implements AuthProvider {}async validateUser(login: string, password: string, ip?: string, scope?: AUTH_SCOPE): Promise<UserModel> {- // TODO: should not validate password for OIDC users// Find user from login or emailconst user: UserModel = await this.usersManager.findUser(login, false)@@ -168,26 +167,13 @@ export class AuthProviderOIDC implements AuthProvider {return null}- if (user.isGuest) {- // Allow guests to be authenticated from db- return this.usersManager.logUser(user, password, ip)- }-- if (!user.isActive) {- this.logger.error(`${this.validateUser.name} - user *${user.login}* is locked`)- throw new HttpException('Account locked', HttpStatus.FORBIDDEN)- }-- // For OIDC users, only allow app password authentication- let authSuccess = false- if (scope) {- authSuccess = 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) {- return user+ if (user.isGuest || user.isAdmin || scope || this.oidcConfig.enablePasswordAuth) {+ // Allow local password authentication for:+ // - guest users+ // - admin users (break-glass access)+ // - application scopes (app passwords)+ // - regular users when password authentication is enabled+ return this.usersManager.logUser(user, password, ip, scope)}return null@@ -199,7 +185,7 @@ export class AuthProviderOIDC implements AuthProvider {const apiIndex = url.pathname.indexOf(AUTH_ROUTE.BASE)this.frontendBaseUrl = apiIndex >= 0 ? `${url.origin}${url.pathname.slice(0, apiIndex)}` : url.origin}- const url = new URL('/#/', this.frontendBaseUrl)+ const url = new URL(this.frontendBaseUrl)const params = new URLSearchParams({[AUTH_PROVIDER.OIDC]: 'true',[`${TOKEN_TYPE.ACCESS}_expiration`]: `${accessExpiration}`,diff --git a/environment/environment.dist.yaml b/environment/environment.dist.yamlindex aeff6bac..c7ed5e32 100755--- a/environment/environment.dist.yaml+++ b/environment/environment.dist.yaml@@ -135,6 +135,11 @@ auth:upnSuffix:# netbiosName: NetBIOS domain name used with `sAMAccountName` to build legacy logins (e.g., `SYNC_IN`\user)netbiosName:+ # enablePasswordAuthFallback: Enable local password authentication as a fallback when the LDAP authentication service is unavailable.+ # This fallback applies only to existing users and is NOT triggered when credentials are invalid.+ # Users must have signed in at least once.+ # default: true+ enablePasswordAuthFallback: trueoidc:# issuerUrl: The URL of the OIDC provider's discovery endpoint (e.g., https://accounts.google.com, https://login.microsoftonline.com/tenant-id/v2.0)# The server will automatically discover the authorization, token, userinfo, and logout endpoints@@ -167,6 +172,11 @@ auth:# When disabled, only existing users are allowed to authenticate via OIDC.# default: trueautoCreateUser: true+ # enablePasswordAuthentication: Enable local password-based authentication (login or email/password).+ # When enabled, users can sign in with their Sync-in password in addition to OIDC authentication.+ # Users must have signed in at least once and have a local password set.+ # default: true+ enablePasswordAuth: true# skipSubjectCheck: Disable verification that the `sub` claim returned by the UserInfo endpoint# matches the `sub` claim from the ID token.# Set to true only for non-compliant or legacy OIDC providers.