Refactor code to use ParticipantOptions instead of TokenOptions

This commit is contained in:
juancarmore 2025-04-24 12:26:07 +02:00
parent 7ad0328dcf
commit 7135270b8a
8 changed files with 39 additions and 39 deletions

View File

@ -1,7 +1,7 @@
import { container } from '../config/dependency-injector.config.js'; import { container } from '../config/dependency-injector.config.js';
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { LoggerService } from '../services/logger.service.js'; import { LoggerService } from '../services/logger.service.js';
import { TokenOptions } from '@typings-ce'; import { ParticipantOptions } from '@typings-ce';
import { OpenViduMeetError } from '../models/index.js'; import { OpenViduMeetError } from '../models/index.js';
import { ParticipantService } from '../services/participant.service.js'; import { ParticipantService } from '../services/participant.service.js';
import { MEET_PARTICIPANT_TOKEN_EXPIRATION } from '../environment.js'; import { MEET_PARTICIPANT_TOKEN_EXPIRATION } from '../environment.js';
@ -14,15 +14,19 @@ export const generateParticipantToken = async (req: Request, res: Response) => {
const logger = container.get(LoggerService); const logger = container.get(LoggerService);
const participantService = container.get(ParticipantService); const participantService = container.get(ParticipantService);
const roomService = container.get(RoomService); const roomService = container.get(RoomService);
const tokenOptions: TokenOptions = req.body; const participantOptions: ParticipantOptions = req.body;
const { roomId } = tokenOptions; const { roomId } = participantOptions;
try { try {
logger.verbose(`Generating participant token for room ${roomId}`); logger.verbose(`Generating participant token for room ${roomId}`);
await roomService.createLivekitRoom(roomId); await roomService.createLivekitRoom(roomId);
const token = await participantService.generateOrRefreshParticipantToken(tokenOptions); const token = await participantService.generateOrRefreshParticipantToken(participantOptions);
res.cookie(INTERNAL_CONFIG.PARTICIPANT_TOKEN_COOKIE_NAME, token, getCookieOptions('/', MEET_PARTICIPANT_TOKEN_EXPIRATION)); res.cookie(
INTERNAL_CONFIG.PARTICIPANT_TOKEN_COOKIE_NAME,
token,
getCookieOptions('/', MEET_PARTICIPANT_TOKEN_EXPIRATION)
);
return res.status(200).json({ token }); return res.status(200).json({ token });
} catch (error) { } catch (error) {
logger.error(`Error generating participant token for room: ${roomId}`); logger.error(`Error generating participant token for room: ${roomId}`);
@ -49,15 +53,19 @@ export const refreshParticipantToken = async (req: Request, res: Response) => {
} }
} }
const tokenOptions: TokenOptions = req.body; const participantOptions: ParticipantOptions = req.body;
const { roomId } = tokenOptions; const { roomId } = participantOptions;
const participantService = container.get(ParticipantService); const participantService = container.get(ParticipantService);
try { try {
logger.verbose(`Refreshing participant token for room ${roomId}`); logger.verbose(`Refreshing participant token for room ${roomId}`);
const token = await participantService.generateOrRefreshParticipantToken(tokenOptions, true); const token = await participantService.generateOrRefreshParticipantToken(participantOptions, true);
res.cookie(INTERNAL_CONFIG.PARTICIPANT_TOKEN_COOKIE_NAME, token, getCookieOptions('/', MEET_PARTICIPANT_TOKEN_EXPIRATION)); res.cookie(
INTERNAL_CONFIG.PARTICIPANT_TOKEN_COOKIE_NAME,
token,
getCookieOptions('/', MEET_PARTICIPANT_TOKEN_EXPIRATION)
);
logger.verbose(`Participant token refreshed for room ${roomId}`); logger.verbose(`Participant token refreshed for room ${roomId}`);
return res.status(200).json({ token }); return res.status(200).json({ token });
} catch (error) { } catch (error) {

View File

@ -1,5 +1,5 @@
import { Request, Response, NextFunction } from 'express'; import { Request, Response, NextFunction } from 'express';
import { AuthMode, ParticipantRole, UserRole, TokenOptions } from '@typings-ce'; import { AuthMode, ParticipantRole, UserRole, ParticipantOptions } from '@typings-ce';
import { container } from '../config/dependency-injector.config.js'; import { container } from '../config/dependency-injector.config.js';
import { MeetStorageService, LoggerService, RoomService } from '../services/index.js'; import { MeetStorageService, LoggerService, RoomService } from '../services/index.js';
import { allowAnonymous, tokenAndRoleValidator, withAuth } from './auth.middleware.js'; import { allowAnonymous, tokenAndRoleValidator, withAuth } from './auth.middleware.js';
@ -19,7 +19,7 @@ export const configureTokenAuth = async (req: Request, res: Response, next: Next
let role: ParticipantRole; let role: ParticipantRole;
try { try {
const { roomId, secret } = req.body as TokenOptions; const { roomId, secret } = req.body as ParticipantOptions;
role = await roomService.getRoomRoleBySecret(roomId, secret); role = await roomService.getRoomRoleBySecret(roomId, secret);
} catch (error) { } catch (error) {
logger.error('Error getting room secret role', error); logger.error('Error getting room secret role', error);

View File

@ -1,9 +1,9 @@
import { TokenOptions } from '@typings-ce'; import { ParticipantOptions } from '@typings-ce';
import { Request, Response, NextFunction } from 'express'; import { Request, Response, NextFunction } from 'express';
import { z } from 'zod'; import { z } from 'zod';
import { nonEmptySanitizedRoomId } from './room-validator.middleware.js'; import { nonEmptySanitizedRoomId } from './room-validator.middleware.js';
const ParticipantTokenRequestSchema: z.ZodType<TokenOptions> = z.object({ const ParticipantTokenRequestSchema: z.ZodType<ParticipantOptions> = z.object({
roomId: nonEmptySanitizedRoomId('roomId'), roomId: nonEmptySanitizedRoomId('roomId'),
participantName: z.string().nonempty('Participant name is required'), participantName: z.string().nonempty('Participant name is required'),
secret: z.string().nonempty('Secret is required') secret: z.string().nonempty('Secret is required')

View File

@ -1,6 +1,5 @@
import { inject, injectable } from '../config/dependency-injector.config.js'; import { inject, injectable } from '../config/dependency-injector.config.js';
import { import {
AccessToken,
CreateOptions, CreateOptions,
DataPacket_Kind, DataPacket_Kind,
EgressClient, EgressClient,
@ -15,13 +14,7 @@ import {
SendDataOptions, SendDataOptions,
StreamOutput StreamOutput
} from 'livekit-server-sdk'; } from 'livekit-server-sdk';
import { import { LIVEKIT_API_KEY, LIVEKIT_API_SECRET, LIVEKIT_URL_PRIVATE } from '../environment.js';
LIVEKIT_API_KEY,
LIVEKIT_API_SECRET,
LIVEKIT_URL,
LIVEKIT_URL_PRIVATE,
MEET_PARTICIPANT_TOKEN_EXPIRATION
} from '../environment.js';
import { LoggerService } from './logger.service.js'; import { LoggerService } from './logger.service.js';
import { import {
errorLivekitIsNotAvailable, errorLivekitIsNotAvailable,
@ -30,7 +23,6 @@ import {
internalError, internalError,
OpenViduMeetError OpenViduMeetError
} from '../models/error.model.js'; } from '../models/error.model.js';
import { ParticipantPermissions, ParticipantRole, TokenOptions } from '@typings-ce';
import { RecordingHelper } from '../helpers/recording.helper.js'; import { RecordingHelper } from '../helpers/recording.helper.js';
@injectable() @injectable()

View File

@ -1,5 +1,5 @@
import { injectable, inject } from 'inversify'; import { injectable, inject } from 'inversify';
import { ParticipantPermissions, ParticipantRole, TokenOptions } from '@typings-ce'; import { ParticipantPermissions, ParticipantRole, ParticipantOptions } from '@typings-ce';
import { ParticipantInfo } from 'livekit-server-sdk'; import { ParticipantInfo } from 'livekit-server-sdk';
import { errorParticipantAlreadyExists, errorParticipantNotFound } from '../models/index.js'; import { errorParticipantAlreadyExists, errorParticipantNotFound } from '../models/index.js';
import { LiveKitService, LoggerService, RoomService, TokenService } from './index.js'; import { LiveKitService, LoggerService, RoomService, TokenService } from './index.js';
@ -13,8 +13,8 @@ export class ParticipantService {
@inject(TokenService) protected tokenService: TokenService @inject(TokenService) protected tokenService: TokenService
) {} ) {}
async generateOrRefreshParticipantToken(options: TokenOptions, refresh = false): Promise<string> { async generateOrRefreshParticipantToken(participantOptions: ParticipantOptions, refresh = false): Promise<string> {
const { roomId, participantName, secret } = options; const { roomId, participantName, secret } = participantOptions;
// Check if participant with same participantName exists in the room // Check if participant with same participantName exists in the room
const participantExists = await this.participantExists(roomId, participantName); const participantExists = await this.participantExists(roomId, participantName);
@ -30,14 +30,17 @@ export class ParticipantService {
} }
const role = await this.roomService.getRoomRoleBySecret(roomId, secret); const role = await this.roomService.getRoomRoleBySecret(roomId, secret);
const token = await this.generateParticipantToken(role, options); const token = await this.generateParticipantToken(role, participantOptions);
this.logger.verbose(`Participant token generated for room ${roomId}`); this.logger.verbose(`Participant token generated for room ${roomId}`);
return token; return token;
} }
protected async generateParticipantToken(role: ParticipantRole, options: TokenOptions): Promise<string> { protected async generateParticipantToken(
const permissions = this.getParticipantPermissions(role, options.roomId); role: ParticipantRole,
return this.tokenService.generateParticipantToken(options, permissions, role); participantOptions: ParticipantOptions
): Promise<string> {
const permissions = this.getParticipantPermissions(role, participantOptions.roomId);
return this.tokenService.generateParticipantToken(participantOptions, permissions, role);
} }
async getParticipant(roomId: string, participantName: string): Promise<ParticipantInfo | null> { async getParticipant(roomId: string, participantName: string): Promise<ParticipantInfo | null> {

View File

@ -1,7 +1,4 @@
import { import {
_Object,
DeleteObjectCommand,
DeleteObjectCommandOutput,
DeleteObjectsCommand, DeleteObjectsCommand,
DeleteObjectsCommandOutput, DeleteObjectsCommandOutput,
GetObjectCommand, GetObjectCommand,

View File

@ -8,7 +8,7 @@ import {
} from '../environment.js'; } from '../environment.js';
import { inject, injectable } from '../config/dependency-injector.config.js'; import { inject, injectable } from '../config/dependency-injector.config.js';
import { AccessToken, AccessTokenOptions, ClaimGrants, TokenVerifier, VideoGrant } from 'livekit-server-sdk'; import { AccessToken, AccessTokenOptions, ClaimGrants, TokenVerifier, VideoGrant } from 'livekit-server-sdk';
import { ParticipantPermissions, ParticipantRole, TokenOptions, User } from '@typings-ce'; import { ParticipantPermissions, ParticipantRole, ParticipantOptions, User } from '@typings-ce';
import { LoggerService } from './index.js'; import { LoggerService } from './index.js';
@injectable() @injectable()
@ -38,7 +38,7 @@ export class TokenService {
} }
async generateParticipantToken( async generateParticipantToken(
participantOptions: TokenOptions, participantOptions: ParticipantOptions,
permissions: ParticipantPermissions, permissions: ParticipantPermissions,
role: ParticipantRole role: ParticipantRole
): Promise<string> { ): Promise<string> {

View File

@ -6,7 +6,7 @@ import {
MeetRoomRoleAndPermissions, MeetRoomRoleAndPermissions,
MeetRoomPreferences, MeetRoomPreferences,
SecurityPreferencesDTO, SecurityPreferencesDTO,
TokenOptions, ParticipantOptions,
User User
} from '@lib/typings/ce'; } from '@lib/typings/ce';
import { RecordingInfo } from 'openvidu-components-angular'; import { RecordingInfo } from 'openvidu-components-angular';
@ -51,12 +51,12 @@ export class HttpService {
return this.deleteRequest(`${this.INTERNAL_API_PATH_PREFIX}/meetings/${roomId}`); return this.deleteRequest(`${this.INTERNAL_API_PATH_PREFIX}/meetings/${roomId}`);
} }
generateParticipantToken(tokenOptions: TokenOptions): Promise<{ token: string }> { generateParticipantToken(participantOptions: ParticipantOptions): Promise<{ token: string }> {
return this.postRequest(`${this.INTERNAL_API_PATH_PREFIX}/participants/token`, tokenOptions); return this.postRequest(`${this.INTERNAL_API_PATH_PREFIX}/participants/token`, participantOptions);
} }
refreshParticipantToken(tokenOptions: TokenOptions): Promise<{ token: string }> { refreshParticipantToken(participantOptions: ParticipantOptions): Promise<{ token: string }> {
return this.postRequest(`${this.INTERNAL_API_PATH_PREFIX}/participants/token/refresh`, tokenOptions); return this.postRequest(`${this.INTERNAL_API_PATH_PREFIX}/participants/token/refresh`, participantOptions);
} }
/** /**