|
|
@ -52,28 +52,16 @@ export function createMcpAuthorizationMiddleware({ |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (!impersonation.isActive) { |
|
|
if (!impersonation.isActive) { |
|
|
const parameters = [`realm="${MCP_REALM}"`]; |
|
|
return sendUnauthorizedResponse({ accessId, response }); |
|
|
|
|
|
|
|
|
if (accessId) { |
|
|
|
|
|
parameters.push( |
|
|
|
|
|
'error="invalid_token"', |
|
|
|
|
|
'error_description="The access cannot be resolved"' |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
response.setHeader('WWW-Authenticate', `Bearer ${parameters.join(', ')}`); |
|
|
|
|
|
|
|
|
|
|
|
return response.status(StatusCodes.UNAUTHORIZED).json({ |
|
|
|
|
|
error: getReasonPhrase(StatusCodes.UNAUTHORIZED), |
|
|
|
|
|
message: 'A valid access is required as a bearer token' |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) { |
|
|
if (configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) { |
|
|
const user = await userService.user({ id: impersonation.userId }); |
|
|
const user = await userService.user({ id: impersonation.userId }); |
|
|
|
|
|
|
|
|
// The daily requests of an inactive user are not counted, as in the
|
|
|
if (!user) { |
|
|
// strategies
|
|
|
return sendUnauthorizedResponse({ accessId, response }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const isRequestLimitExceeded = |
|
|
const isRequestLimitExceeded = |
|
|
hasRole(user, 'INACTIVE') || |
|
|
hasRole(user, 'INACTIVE') || |
|
|
(await userService.isDailyRequestLimitExceeded({ user })); |
|
|
(await userService.isDailyRequestLimitExceeded({ user })); |
|
|
@ -99,3 +87,27 @@ export function createMcpAuthorizationMiddleware({ |
|
|
return next(); |
|
|
return next(); |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function sendUnauthorizedResponse({ |
|
|
|
|
|
accessId, |
|
|
|
|
|
response |
|
|
|
|
|
}: { |
|
|
|
|
|
accessId: string; |
|
|
|
|
|
response: Response; |
|
|
|
|
|
}) { |
|
|
|
|
|
const parameters = [`realm="${MCP_REALM}"`]; |
|
|
|
|
|
|
|
|
|
|
|
if (accessId) { |
|
|
|
|
|
parameters.push( |
|
|
|
|
|
'error="invalid_token"', |
|
|
|
|
|
'error_description="The access cannot be resolved"' |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
response.setHeader('WWW-Authenticate', `Bearer ${parameters.join(', ')}`); |
|
|
|
|
|
|
|
|
|
|
|
return response.status(StatusCodes.UNAUTHORIZED).json({ |
|
|
|
|
|
error: getReasonPhrase(StatusCodes.UNAUTHORIZED), |
|
|
|
|
|
message: 'A valid access is required as a bearer token' |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|