我正在使用serverless-framework,node.js和typescript作为我的服务。我有一个自定义的授权器,我从外部服务获取一些数据,根据该服务的响应,我想在我的lambda中返回这样的错误代码。
问题是,每次来自授权器的响应都是(不管我如何返回错误以及它通过的错误和状态代码):
{
"statusCode": 403,
"error": "Forbidden",
"message": "No principalId set on the Response"
}这是自定义授权器的具体信息,其他内容都不能返回吗,或者是否可以以某种方式修改响应?
serverless.yml
...
functions:
users:
handler: lambdas/users/handler.main
events:
- http:
path: /users
method: GET
private: true
authorizer:
name: customAuthorizer
resultTtlInSeconds: 0
customAuthorizer:
handler: lambdas/custom-authorizer/handler.handler
environment:
APP_NAME: ${env:APP_NAME}
...这是我的处理程序中的一个简单代码
handler.ts
export async function handler(event: APIGatewayTokenAuthorizerEvent, context: Context): Promise<any> {
context.callbackWaitsForEmptyEventLoop = false;
try {
if (!event.authorizationToken) {
throw new HttpError("Unauthorized", 401);
}
const hasAccess = await fetchingPolicyFromExternalService(event.authorizationToken);
if (!hasAccess) {
throw new HttpError("Forbidden", 403);
}
const policyDocument = generatePolicy(event, "Allow"); //generate policy for Allow user to invoke api
return policyDocument;
} catch (error) {
if (error instanceof HttpError) {
return {
statusCode: error.status,
body: error.message,
};
}
return {
statusCode: 500,
body: "UnknownError",
};
}
}发布于 2021-08-07 23:01:29
关于私有: true
然后,连接到此Rest API的
客户端需要在其请求的x- API -key头中设置这些api密钥值中的任何一个。这仅对于private属性设置为true的函数是必需的。
https://www.serverless.com/framework/docs/providers/aws/events/apigateway/
不确定您是如何发送请求的,但请考虑这一部分。这可能是你遗漏了头信息,这就是为什么它总是这样回答(即不是你的授权者,但你的函数是受保护的)。因此,此响应可能来自您的函数,而不是授权者。
https://stackoverflow.com/questions/64771296
复制相似问题