amazon-web-services - 自定义授权者的 AWS API 网关 403 错误
问题描述
我正在使用 Claudia-api-builder 创建和部署。 https://github.com/claudiajs/example-projects/tree/master/custom-authorizers
我的 AWS 自定义授权方如下所示:
let jwtDecode = require('jwt-decode');
var generatePolicy = function (authToken, methodArn) {
'use strict';
var tmp = methodArn.split(':'),
apiGatewayArnTmp = tmp[5].split('/'),
awsAccountId = tmp[4],
region = tmp[3],
restApiId = apiGatewayArnTmp[0],
stage = apiGatewayArnTmp[1];
let group = jwtDecode(authToken)["cognito:groups"];
if (group[0] === 'Admin') {
return {
'principalId': authToken.split('-')[0],
'policyDocument': {
'Version': '2012-10-17',
'Statement': [{
'Effect': 'Allow',
'Action': [
'execute-api:Invoke'
],
'Resource': [
'arn:aws:execute-api:' + region + ':' + awsAccountId + ':' + restApiId + '/' + stage + '/GET/citizens',
'arn:aws:execute-api:' + region + ':' + awsAccountId + ':' + restApiId + '/' + stage + '/GET/citizens/{citizenId}/personal-details'
]
}]
}
};
}
exports.auth = function testAuth(event, context, callback) {
'use strict';
console.log('got event', event);
/*
* {
* "type":"TOKEN",
* "authorizationToken":"<Incoming bearer token>",
* "methodArn":"arn:aws:execute-api:<Region id>:<Account id>:<API id>/<Stage>/<Method>/<Resource path>"
* }
*/
if (event && event.authorizationToken && event.methodArn) {
callback(null, generatePolicy(event.authorizationToken, event.methodArn));
} else {
callback('Unauthorized');
}
};
资源中的第一个 API 工作正常,但是当我调用第二个 API i:e 时:
'arn:aws:execute-api:' + region + ':' + awsAccountId + ':' + restApiId + '/' + stage + '/GET/citizens/{citizenId}/personal-details'
它给了我 403 Forbidden :
{
"Message": "User is not authorized to access this resource"
}
就我而言,授权缓存也被禁用
这个问题有什么解决办法吗?
解决方案
资源不应是 API Gateway 方法的路径。
实际上它应该是资源的Arn。您可以通过执行以下操作从 AWS 控制台获取此信息:
- 开放 API 网关
- 选择您的 API 网关
- 点击
Resources
选项 - 找到您的资源(这将是
GET
下面的方法citizens/{citizenId}/personal-details
)。点击它。 - 将有一个
Arn
可供您使用。
当使用基于路径的参数时,任何参数都被替换为一个,*
所以这将变成下面。
'arn:aws:execute-api:' + region + ':' + awsAccountId + ':' + restApiId + '/' + stage + '/GET/citizens/*/personal-details'
推荐阅读
- java - @ManyToOne 跨模式创建错误的 FK
- angular - Angular 7无法在获取带有地图的Web服务中发送标头
- .net - 使用 Simple Injector .NET 进行依赖注入,找不到类型或命名空间
- excel - 将值复制并粘贴到工作簿中每个工作表的数据表末尾
- javascript - 使用 Parceljs 时,为 Prime React 组件包含 css 文件的最佳方法是什么?
- python - Python三元组到元组
- java - 退出VM后如何在其中创建临时文件并删除
- php - 如何在 where() 函数中给出条件以使表中的记录不等于零
- cordova - 是否可以使用“Set-Cookie”响应标头在 Cordova 中设置 cookie?
- python - from urllib3.util.ssl_ import ( ImportError: cannot import name ssl