【发布时间】:2021-06-27 06:29:47
【问题描述】:
我正在开发自定义身份验证解决方案,我的后端位于 NODE (lambda) 上,并通过 API 网关调用该 lambda(这是一个后调用)。 如果我使用无代理 API,那么对于每个瘦身都可以正常工作,但在我的情况下,需要传递自定义的附加标头。当我尝试使用代理时,它没有响应。 (看起来 Cognito 异步调用需要时间或响应无法解决) 我不确定我是否遗漏了一些配置或代码有问题(代码在单个 lambda 和没有代理的 API 上运行良好)。 这是我的 Lambda 代码。
// const AWS = require("aws-sdk");
// var crypto = require("crypto-js");
var CryptoJS = require("crypto-js");
var Crypto = require("crypto");
const { CognitoIdentityServiceProvider } = require("aws-sdk");
const hashSecret = (clientSecret, username, clientId) =>
Crypto.createHmac("SHA256", clientSecret)
.update(username + clientId)
.digest("base64");
async function isUserValid() {
const USER_POOL_ID = "poolid";
const CLIENT_ID = "clidntID";
const CLIENT_SECRET = "secreteCode";
const Password = "userName";
const Username = "password";
const cognito = new CognitoIdentityServiceProvider({
region: "ap-southeast-2",
});
try {
const payload = {
UserPoolId: USER_POOL_ID,
AuthFlow: "ADMIN_NO_SRP_AUTH",
ClientId: CLIENT_ID,
AuthParameters: {
USERNAME: "username",
PASSWORD: "password",
SECRET_HASH: hashSecret(CLIENT_SECRET, Username, CLIENT_ID),
},
};
const response = await cognito.adminInitiateAuth(payload).promise();
// // console.log("respone :", response)
// console.log("before response node js lambda ::: ")
return response;
} catch (e) {
console.log("error : ", e.message);
}
}
async function test() {
return "test ";
}
exports.handler = async (event) => {
// TODO implement
console.log("event : ", event);
'
const response = {
statusCode: 200,
headers: {
"Content-Type" : "application/json",
"Access-Control-Allow-Origin" : "*",
"X-Content-Type-Option" : "nosniff",
"Content-Security-Policy" : "default-src self",
"X-frame-options" : "DENY",
"Cache-Control" : "max-age=86400",
"X-XSS-protection" : 0,
"X-rate-limit": 600,
"Strict-Transport-Security": "max-age=31536000; includeSubDomains",
},
// body: await test() // (this response is working fine
body: await isUserValid(),
};
return response;
};
使用代理设置从 API 网关进行测试时的错误: { “消息”:“内部服务器错误” } 响应标头 {"x-amzn-ErrorType":"InternalServerErrorException"}
我尝试了多个选项,但没有一个对我有用。
【问题讨论】:
标签: amazon-web-services aws-api-gateway