From a9274005efc0cdafde21d199dbcd97fd5303bfbf Mon Sep 17 00:00:00 2001 From: juancarmore Date: Fri, 21 Mar 2025 00:54:16 +0100 Subject: [PATCH] backend: Refactor AuthService and TokenService to use User object for authentication and token generation --- backend/src/services/auth.service.ts | 23 +++++++++++++++++++---- backend/src/services/token.service.ts | 15 ++++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/backend/src/services/auth.service.ts b/backend/src/services/auth.service.ts index dd0e003..f245af7 100644 --- a/backend/src/services/auth.service.ts +++ b/backend/src/services/auth.service.ts @@ -1,17 +1,32 @@ import { MEET_ADMIN_SECRET, MEET_ADMIN_USER, MEET_PRIVATE_ACCESS, MEET_SECRET, MEET_USER } from '../environment.js'; -import { injectable } from '../config/dependency-injector.config.js'; +import { inject, injectable } from '../config/dependency-injector.config.js'; +import { User } from '@typings-ce'; +import { UserService } from './user.service.js'; @injectable() export class AuthService { - authenticateAdmin(username: string, password: string): boolean { + constructor(@inject(UserService) protected userService: UserService) {} + + authenticate(username: string, password: string): User | null { + const isAdmin = this.authenticateAdmin(username, password); + const isUser = this.authenticateUser(username, password); + + if (isAdmin || isUser) { + return this.userService.getUser(username); + } + + return null; + } + + private authenticateAdmin(username: string, password: string): boolean { return username === MEET_ADMIN_USER && password === MEET_ADMIN_SECRET; } - authenticateUser(username: string, password: string): boolean { + private authenticateUser(username: string, password: string): boolean { if (MEET_PRIVATE_ACCESS === 'true') { return username === MEET_USER && password === MEET_SECRET; } - return true; + return false; } } diff --git a/backend/src/services/token.service.ts b/backend/src/services/token.service.ts index f29f94c..38c0f02 100644 --- a/backend/src/services/token.service.ts +++ b/backend/src/services/token.service.ts @@ -6,23 +6,24 @@ import { } from '../environment.js'; import { injectable } from '../config/dependency-injector.config.js'; import { AccessToken, AccessTokenOptions, ClaimGrants, TokenVerifier } from 'livekit-server-sdk'; +import { User } from '@typings-ce'; @injectable() export class TokenService { - async generateAccessToken(username: string): Promise { - return await this.generateJwtToken(username, MEET_ACCESS_TOKEN_EXPIRATION); + async generateAccessToken(user: User): Promise { + return await this.generateJwtToken(user, MEET_ACCESS_TOKEN_EXPIRATION); } - async generateRefreshToken(username: string): Promise { - return await this.generateJwtToken(username, MEET_REFRESH_TOKEN_EXPIRATION); + async generateRefreshToken(user: User): Promise { + return await this.generateJwtToken(user, MEET_REFRESH_TOKEN_EXPIRATION); } - private async generateJwtToken(username: string, expiration: string): Promise { + private async generateJwtToken(user: User, expiration: string): Promise { const options: AccessTokenOptions = { - identity: username, + identity: user.username, ttl: expiration, metadata: JSON.stringify({ - role: 'admin' + role: user.role }) }; const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, options);