【问题标题】:AWS API Gateway with Cognito Authorization using multiple user pools使用多个用户池的具有 Cognito 授权的 AWS API 网关
【发布时间】:2020-11-27 16:44:08
【问题描述】:
【问题讨论】:
标签:
amazon-web-services
aws-api-gateway
amazon-cognito
【解决方案1】:
我做过类似的事情,但不完全是你正在做的事情,但也许它会引导你朝着正确的方向前进。
所以我认为你是对的,你需要做一个 Lambda 授权方,然后承担认知用户的角色。然后,如果该用户无权执行某项操作,IAM 就会将其炸毁。
client = boto3.client('sts')
role=event['requestContext']['authorizer']['claims']['cognito:preferred_role']
assumed_role_object = client.assume_role(
RoleArn=role,
RoleSessionName='APIrole'
)
credentials=assumed_role_object['Credentials']
dynamo_resource=boto3.resource(
'dynamodb',
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'],
)
【解决方案2】:
这里发布了一个解决方案:
How to use multiple Cognito user pools for a single endpoint with AWS API Gateway?
我发现 Abhay Nayak 的回答很有用,它帮助我实现了我的场景:
- 允许使用不同 Cognitos 提供的 JWT 从不同的 aws 帐户对单个端点进行授权。使用 cognito 用户池授权器,而不是自定义 lambda 授权器。
这是我的无服务器 .yml 模板中的授权方和端点:
functions:
service:
handler: service.service
events:
- http:
path: service
method: get
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
resources:
Resources:
ApiGatewayAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
AuthorizerResultTtlInSeconds: 300
Name: API_AUTH_cognito_authorizer
IdentitySource: method.request.header.Authorization
RestApiId:
Ref: ApiGatewayRestApi
Type: COGNITO_USER_POOLS
ProviderARNs:
- arn:aws:cognito-idp:us-east-1:account1:userpool/userpool1
- arn:aws:cognito-idp:us-east-1:account1:userpool/userpool2
- arn:aws:cognito-idp:us-east-1:account2:userpool/userpool3
- arn:aws:cognito-idp:us-east-1:account2:userpool/userpool4