fix: return correct codes
This commit is contained in:
parent
691e472a2e
commit
9a9c7260e2
|
|
@ -6,11 +6,10 @@ import { Request, Response, NextFunction } from 'express';
|
||||||
*/
|
*/
|
||||||
export function requireMasterKey(req: Request, res: Response, next: NextFunction): void {
|
export function requireMasterKey(req: Request, res: Response, next: NextFunction): void {
|
||||||
if (!req.apiKey) {
|
if (!req.apiKey) {
|
||||||
res.status(401).json({
|
return res.status(401).json({
|
||||||
error: 'Authentication required',
|
error: 'Authentication required',
|
||||||
message: 'This endpoint requires authentication',
|
message: 'This endpoint requires authentication',
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.apiKey.keyType !== 'master') {
|
if (req.apiKey.keyType !== 'master') {
|
||||||
|
|
@ -18,11 +17,10 @@ export function requireMasterKey(req: Request, res: Response, next: NextFunction
|
||||||
`[${new Date().toISOString()}] Non-master key attempted admin action: ${req.apiKey.id} (${req.apiKey.keyType}) - ${req.path}`,
|
`[${new Date().toISOString()}] Non-master key attempted admin action: ${req.apiKey.id} (${req.apiKey.keyType}) - ${req.path}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
res.status(403).json({
|
return res.status(403).json({
|
||||||
error: 'Master key required',
|
error: 'Master key required',
|
||||||
message: 'This endpoint requires a master API key',
|
message: 'This endpoint requires a master API key',
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
|
|
||||||
|
|
@ -7,30 +7,27 @@ import { Request, Response, NextFunction } from 'express';
|
||||||
export function requireProjectKey(req: Request, res: Response, next: NextFunction): void {
|
export function requireProjectKey(req: Request, res: Response, next: NextFunction): void {
|
||||||
// This middleware assumes validateApiKey has already run and attached req.apiKey
|
// This middleware assumes validateApiKey has already run and attached req.apiKey
|
||||||
if (!req.apiKey) {
|
if (!req.apiKey) {
|
||||||
res.status(401).json({
|
return res.status(401).json({
|
||||||
error: 'Authentication required',
|
error: 'Authentication required',
|
||||||
message: 'API key validation must be performed first',
|
message: 'API key validation must be performed first',
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block master keys from generation endpoints
|
// Block master keys from generation endpoints
|
||||||
if (req.apiKey.keyType === 'master') {
|
if (req.apiKey.keyType === 'master') {
|
||||||
res.status(403).json({
|
return res.status(403).json({
|
||||||
error: 'Forbidden',
|
error: 'Forbidden',
|
||||||
message:
|
message:
|
||||||
'Master keys cannot be used for image generation. Please use a project-specific API key.',
|
'Master keys cannot be used for image generation. Please use a project-specific API key.',
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure project key has required IDs
|
// Ensure project key has required IDs
|
||||||
if (!req.apiKey.projectId) {
|
if (!req.apiKey.projectId) {
|
||||||
res.status(400).json({
|
return res.status(400).json({
|
||||||
error: 'Invalid API key',
|
error: 'Invalid API key',
|
||||||
message: 'Project key must be associated with a project',
|
message: 'Project key must be associated with a project',
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
|
|
|
||||||
|
|
@ -23,22 +23,20 @@ export async function validateApiKey(
|
||||||
const providedKey = req.headers['x-api-key'] as string;
|
const providedKey = req.headers['x-api-key'] as string;
|
||||||
|
|
||||||
if (!providedKey) {
|
if (!providedKey) {
|
||||||
res.status(401).json({
|
return res.status(401).json({
|
||||||
error: 'Missing API key',
|
error: 'Missing API key',
|
||||||
message: 'Provide your API key via X-API-Key header',
|
message: 'Provide your API key via X-API-Key header',
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const apiKey = await apiKeyService.validateKey(providedKey);
|
const apiKey = await apiKeyService.validateKey(providedKey);
|
||||||
|
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
res.status(401).json({
|
return res.status(401).json({
|
||||||
error: 'Invalid API key',
|
error: 'Invalid API key',
|
||||||
message: 'The provided API key is invalid, expired, or revoked',
|
message: 'The provided API key is invalid, expired, or revoked',
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attach to request for use in routes
|
// Attach to request for use in routes
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue