【发布时间】:2021-05-24 17:30:15
【问题描述】:
这是在使用 VUEX 的 VUE 项目中。
目的是能够控制经过身份验证的用户和未经身份验证的用户对 API 网关的访问。
在我的身份池中,我可以看到经过身份验证和未经身份验证的用户都被选中,但我无法让授权者接受 SDK (aws-api-gateway-client) 生成的令牌。
首先我得到 accessKeyId 等
let AWS = require("aws-sdk");
AWS.config.region = env.aws.aws_cognito_region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId:
env.aws.aws_cognito_region +
":" +
env.aws.aws_cognito_identity_pool_id,
Logins: {
["cognito-idp." +
env.aws.aws_cognito_region +
".amazonaws.com/" +
env.aws
.aws_user_pools_id]: rootState.UserStore.authSession
.getIdToken()
.getJwtToken(),
},
});
AWS.config.credentials.get(function() {
commit("SET_ACCESS_CREDS", {
updatedAt: new Date(),
accessKeyId: AWS.config.credentials.accessKeyId,
secretAccessKey: AWS.config.credentials.secretAccessKey,
sessionToken: AWS.config.credentials.sessionToken,
});
});
这些凭据看起来不错。我可以输出它们并且很高兴它们看起来没有损坏。
然后我尝试使用这些通过 apigClientFactory.newClient 访问 api 网关
var apigClientFactory = require("aws-api-gateway-client").default;
var client = apigClientFactory.newClient({
accessKey: state.AccessCredentials.accessKeyId,
secretKey: state.AccessCredentials.secretAccessKey,
sessionToken: state.AccessCredentials.sessionToken,
region: env.aws.aws_cognito_region,
invokeUrl: process.env.VUE_APP_APIBASEURL,
});
console.log(client);
client.invokeApi({}, "/testauth", "GET").then(function(data) {
console.log(data);
});
我的理解是,SDK 采用这些值并生成它需要发送到 API 的内容。我已经阅读了它的算法,我并不完全理解它,但这意味着它会创建访问令牌并将其包装到请求中。
路由testauth已分配授权者。
授权人的代码来自 AWS 文档
// A simple token-based authorizer example to demonstrate how to use an authorization token
// to allow or deny a request. In this example, the caller named 'user' is allowed to invoke
// a request if the client-supplied token value is 'allow'. The caller is not allowed to invoke
// the request if the token value is 'deny'. If the token value is 'unauthorized' or an empty
// string, the authorizer function returns an HTTP 401 status code. For any other token value,
// the authorizer returns an HTTP 500 status code.
// Note that token values are case-sensitive.
exports.handler = function(event, context, callback) {
var token = event.authorizationToken;
switch (token) {
case 'allow':
callback(null, generatePolicy('user', 'Allow', event.methodArn));
break;
case 'deny':
callback(null, generatePolicy('user', 'Deny', event.methodArn));
break;
case 'unauthorized':
callback("Unauthorized"); // Return a 401 Unauthorized response
break;
default:
callback("Error: Invalid token"); // Return a 500 Invalid token response
}
};
// Help function to generate an IAM policy
var generatePolicy = function(principalId, effect, resource) {
var authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
var policyDocument = {};
policyDocument.Version = '2012-10-17';
policyDocument.Statement = [];
var statementOne = {};
statementOne.Action = 'execute-api:Invoke';
statementOne.Effect = effect;
statementOne.Resource = resource;
policyDocument.Statement[0] = statementOne;
authResponse.policyDocument = policyDocument;
}
// Optional output with custom properties of the String, Number or Boolean type.
authResponse.context = {
"stringKey": "stringval",
"numberKey": 123,
"booleanKey": true
};
return authResponse;
}
授权方的 cloudwatch 日志显示“无效令牌”。
我不明白它怎么可能只是因为它所做的一切似乎只是在询问令牌是否说“允许”,这似乎有点奇怪。
在这种情况下,我是不是偶然使用了错误的函数来生成令牌?
令人惊讶的是,没有关于此类要求的全面文档,因为它看起来很标准。
【问题讨论】:
-
PS 愿意接受我需要一种全新的方法——这一切都是通过在不同的网站上进行搜索而组合在一起的
-
我强烈认为我需要找到另一个授权人,但也忍不住注意到我一直在网上看到标题中的格式需要为
Authorisation: bearer XXX...,因为上面跳过而不是使用@ 987654326@
标签: amazon-web-services aws-lambda aws-api-gateway amazon-cognito