Spaces:
Sleeping
Sleeping
File size: 1,357 Bytes
525d39f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import { Controller, Post, Headers, HttpCode, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiHeader } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { createLogger } from '../../common/services/logger.service';
@ApiTags('auth')
@Controller('auth')
export class AuthValidateController {
private readonly logger = createLogger('AuthValidateController');
constructor(private readonly authService: AuthService) {}
@Post('validate')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Validate an API key' })
@ApiHeader({ name: 'X-API-Key', description: 'API key to validate' })
@ApiResponse({ status: 200, description: 'API key is valid' })
@ApiResponse({ status: 401, description: 'Invalid or missing API key' })
async validate(@Headers('x-api-key') apiKey?: string): Promise<{ valid: boolean; role?: string }> {
if (!apiKey) {
return { valid: false };
}
try {
const keyEntity = await this.authService.validateApiKey(apiKey);
if (keyEntity && keyEntity.isActive) {
return { valid: true, role: keyEntity.role };
}
return { valid: false };
} catch (error) {
this.logger.warn('API key validation error', { error: error instanceof Error ? error.message : String(error) });
return { valid: false };
}
}
}
|