diff --git a/backend/src/services/auth.service.ts b/backend/src/services/auth.service.ts index c151813..59480d8 100644 --- a/backend/src/services/auth.service.ts +++ b/backend/src/services/auth.service.ts @@ -1,9 +1,10 @@ import { MEET_ADMIN_SECRET, MEET_ADMIN_USER } from '../environment.js'; import { inject, injectable } from '../config/dependency-injector.config.js'; -import { AuthMode, AuthType, SingleUserAuth, User, ValidAuthMethod } from '@typings-ce'; +import { User } from '@typings-ce'; import { UserService } from './user.service.js'; import { GlobalPreferencesService } from './preferences/global-preferences.service.js'; import { LoggerService } from './logger.service.js'; +import { PasswordHelper } from '../helpers/password.helper.js'; @injectable() export class AuthService { @@ -29,29 +30,13 @@ export class AuthService { } private async authenticateUser(username: string, password: string): Promise { - let requireAuthForRoomCreation: boolean; - let authMode: AuthMode; - let authMethod: ValidAuthMethod; + const userCredentials = await this.userService.getStoredUserCredentials(); - try { - const { securityPreferences } = await this.globalPrefService.getGlobalPreferences(); - requireAuthForRoomCreation = securityPreferences.roomCreationPolicy.requireAuthentication; - ({ authMode, method: authMethod } = securityPreferences.authentication); - } catch (error) { - this.logger.error('Error checking authentication preferences:' + error); + if (!userCredentials) { return false; } - if (requireAuthForRoomCreation || authMode !== AuthMode.NONE) { - if (authMethod.type !== AuthType.SINGLE_USER) { - return false; - } - - const { username: configuredUsername, passwordHash: configurePassword } = (authMethod as SingleUserAuth) - .credentials; - return username === configuredUsername && password === configurePassword; - } - - return false; + const isPasswordValid = await PasswordHelper.verifyPassword(password, userCredentials.passwordHash); + return username === userCredentials.username && isPasswordValid; } } diff --git a/backend/src/services/user.service.ts b/backend/src/services/user.service.ts index 43cf6c7..00c53f2 100644 --- a/backend/src/services/user.service.ts +++ b/backend/src/services/user.service.ts @@ -1,6 +1,6 @@ import { MEET_ADMIN_USER } from '../environment.js'; import { inject, injectable } from '../config/dependency-injector.config.js'; -import { AuthType, UserRole, SingleUserAuth, User } from '@typings-ce'; +import { UserRole, SingleUserAuth, User, SingleUserCredentials } from '@typings-ce'; import { LoggerService } from './logger.service.js'; import { GlobalPreferencesService } from './preferences/global-preferences.service.js'; @@ -19,27 +19,26 @@ export class UserService { }; } - let configuredUsername: string | undefined; + const userCredentials = await this.getStoredUserCredentials(); - try { - const { securityPreferences } = await this.globalPrefService.getGlobalPreferences(); - const method = securityPreferences.authentication.method; - - if (method.type === AuthType.SINGLE_USER) { - configuredUsername = (method as SingleUserAuth).credentials.username; - } - } catch (error) { - this.logger.error('Error checking room creation policy:' + error); - return null; - } - - if (username === configuredUsername) { + if (userCredentials && username === userCredentials.username) { return { - username: configuredUsername, + username, role: UserRole.USER }; } return null; } + + async getStoredUserCredentials(): Promise { + try { + const { securityPreferences } = await this.globalPrefService.getGlobalPreferences(); + const { method: authMethod } = securityPreferences.authentication; + return (authMethod as SingleUserAuth).credentials; + } catch (error) { + this.logger.error('Error getting stored user credentials:' + error); + return null; + } + } }