backend: update API key handling to use numeric creation date and improve method signatures

This commit is contained in:
juancarmore 2025-06-18 13:37:53 +02:00
parent ebdd70c42b
commit 419aaecc57
3 changed files with 23 additions and 23 deletions

View File

@ -103,7 +103,7 @@ export const refreshToken = async (req: Request, res: Response) => {
}
};
export const createApiKey = async (req: Request, res: Response) => {
export const createApiKey = async (_req: Request, res: Response) => {
const logger = container.get(LoggerService);
logger.verbose('Create API key request received');
@ -111,26 +111,12 @@ export const createApiKey = async (req: Request, res: Response) => {
try {
const apiKey = await authService.createApiKey();
return res.status(201).json({ apiKey });
return res.status(201).json(apiKey);
} catch (error) {
handleError(res, error, 'creating API key');
}
};
export const deleteApiKeys = async (req: Request, res: Response) => {
const logger = container.get(LoggerService);
logger.verbose('Delete API keys request received');
const authService = container.get(AuthService);
try {
await authService.deleteApiKeys();
return res.status(202).send();
} catch (error) {
handleError(res, error, 'deleting API keys');
}
};
export const getApiKeys = async (_req: Request, res: Response) => {
const logger = container.get(LoggerService);
logger.verbose('Get API keys request received');
@ -139,8 +125,22 @@ export const getApiKeys = async (_req: Request, res: Response) => {
try {
const apiKeys = await authService.getApiKeys();
return res.status(200).json({ apiKeys });
return res.status(200).json(apiKeys);
} catch (error) {
handleError(res, error, 'getting API keys');
}
};
export const deleteApiKeys = async (_req: Request, res: Response) => {
const logger = container.get(LoggerService);
logger.verbose('Delete API keys request received');
const authService = container.get(AuthService);
try {
await authService.deleteApiKeys();
return res.status(204).send();
} catch (error) {
handleError(res, error, 'deleting API keys');
}
};

View File

@ -13,7 +13,7 @@ export class PasswordHelper {
}
// Generate a secure API key using uid with a length of 32 characters
static generateApiKey(): { key: string; creationDate: string } {
return { key: `ovmeet-${uid(32)}`, creationDate: new Date().toISOString() };
static generateApiKey(): { key: string; creationDate: number } {
return { key: `ovmeet-${uid(32)}`, creationDate: new Date().getTime() };
}
}

View File

@ -568,18 +568,18 @@ export class MeetStorageService<
return await this.saveCacheAndStorage(userRedisKey, storageUserKey, user);
}
async saveApiKey(apiKeyData: { key: string; creationDate: string }): Promise<void> {
async saveApiKey(apiKeyData: { key: string; creationDate: number }): Promise<void> {
const redisKey = RedisKeyName.API_KEYS;
const storageKey = this.keyBuilder.buildApiKeysKey();
this.logger.debug(`Saving API key to Redis and storage: ${redisKey}`);
await this.saveCacheAndStorage(redisKey, storageKey, apiKeyData);
await this.saveCacheAndStorage(redisKey, storageKey, [apiKeyData]);
}
async getApiKeys(): Promise<{ key: string; creationDate: string }[]> {
async getApiKeys(): Promise<{ key: string; creationDate: number }[]> {
const redisKey = RedisKeyName.API_KEYS;
const storageKey = this.keyBuilder.buildApiKeysKey();
this.logger.debug(`Retrieving API key from Redis and storage: ${redisKey}`);
const apiKeys = await this.getFromCacheAndStorage<{ key: string; creationDate: string }[]>(
const apiKeys = await this.getFromCacheAndStorage<{ key: string; creationDate: number }[]>(
redisKey,
storageKey
);