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 { Request, Response } from 'express';
import { LoggerService } from '../services/logger.service.js';
import { TokenOptions } from '@typings-ce';
import { ParticipantOptions } from '@typings-ce';
import { OpenViduMeetError } from '../models/index.js';
import { ParticipantService } from '../services/participant.service.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 participantService = container.get(ParticipantService);
const roomService = container.get(RoomService);
const tokenOptions: TokenOptions = req.body;
const { roomId } = tokenOptions;
const participantOptions: ParticipantOptions = req.body;
const { roomId } = participantOptions;
try {
logger.verbose(`Generating participant token for room ${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 });
} catch (error) {
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 { roomId } = tokenOptions;
const participantOptions: ParticipantOptions = req.body;
const { roomId } = participantOptions;
const participantService = container.get(ParticipantService);
try {
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}`);
return res.status(200).json({ token });
} catch (error) {

View File

@ -1,5 +1,5 @@
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 { MeetStorageService, LoggerService, RoomService } from '../services/index.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;
try {
const { roomId, secret } = req.body as TokenOptions;
const { roomId, secret } = req.body as ParticipantOptions;
role = await roomService.getRoomRoleBySecret(roomId, secret);
} catch (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 { z } from 'zod';
import { nonEmptySanitizedRoomId } from './room-validator.middleware.js';
const ParticipantTokenRequestSchema: z.ZodType<TokenOptions> = z.object({
const ParticipantTokenRequestSchema: z.ZodType<ParticipantOptions> = z.object({
roomId: nonEmptySanitizedRoomId('roomId'),
participantName: z.string().nonempty('Participant name 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 {
AccessToken,
CreateOptions,
DataPacket_Kind,
EgressClient,
@ -15,13 +14,7 @@ import {
SendDataOptions,
StreamOutput
} from 'livekit-server-sdk';
import {
LIVEKIT_API_KEY,
LIVEKIT_API_SECRET,
LIVEKIT_URL,
LIVEKIT_URL_PRIVATE,
MEET_PARTICIPANT_TOKEN_EXPIRATION
} from '../environment.js';
import { LIVEKIT_API_KEY, LIVEKIT_API_SECRET, LIVEKIT_URL_PRIVATE } from '../environment.js';
import { LoggerService } from './logger.service.js';
import {
errorLivekitIsNotAvailable,
@ -30,7 +23,6 @@ import {
internalError,
OpenViduMeetError
} from '../models/error.model.js';
import { ParticipantPermissions, ParticipantRole, TokenOptions } from '@typings-ce';
import { RecordingHelper } from '../helpers/recording.helper.js';
@injectable()

View File

@ -1,5 +1,5 @@
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 { errorParticipantAlreadyExists, errorParticipantNotFound } from '../models/index.js';
import { LiveKitService, LoggerService, RoomService, TokenService } from './index.js';
@ -13,8 +13,8 @@ export class ParticipantService {
@inject(TokenService) protected tokenService: TokenService
) {}
async generateOrRefreshParticipantToken(options: TokenOptions, refresh = false): Promise<string> {
const { roomId, participantName, secret } = options;
async generateOrRefreshParticipantToken(participantOptions: ParticipantOptions, refresh = false): Promise<string> {
const { roomId, participantName, secret } = participantOptions;
// Check if participant with same participantName exists in the room
const participantExists = await this.participantExists(roomId, participantName);
@ -30,14 +30,17 @@ export class ParticipantService {
}
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}`);
return token;
}
protected async generateParticipantToken(role: ParticipantRole, options: TokenOptions): Promise<string> {
const permissions = this.getParticipantPermissions(role, options.roomId);
return this.tokenService.generateParticipantToken(options, permissions, role);
protected async generateParticipantToken(
role: ParticipantRole,
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> {

View File

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

View File

@ -8,7 +8,7 @@ import {
} from '../environment.js';
import { inject, injectable } from '../config/dependency-injector.config.js';
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';
@injectable()
@ -38,7 +38,7 @@ export class TokenService {
}
async generateParticipantToken(
participantOptions: TokenOptions,
participantOptions: ParticipantOptions,
permissions: ParticipantPermissions,
role: ParticipantRole
): Promise<string> {

View File

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