import json import boto3
def handler(event, context): user_pool_id = 'your-user-pool-id' region_name = 'your-region-name'
auth_token = event['authorizationToken']
method_arn = event['methodArn']
cognito = boto3.client('cognito-idp', region_name=region_name)
try:
response = cognito.get_user(
AccessToken=auth_token,
)
user_id = response['Username']
policy = generate_policy(user_id, 'Allow', method_arn)
except cognito.exceptions.TokenValidityException as tve:
policy = generate_policy('*', 'Deny', method_arn)
return policy
def generate_policy(principal_id, effect, method_arn): policy = { 'principalId': principal_id, 'policyDocument': { 'Version': '2012-10-17', 'Statement': [ { 'Action': 'execute-api:Invoke', 'Effect': effect, 'Resource': method_arn } ] } } return policy
如果仍然遇到问题,可以尝试检查API网关Lambda授权器和Cognito-IDP客户端的权限设置。确保该Lambda函数有权访问Cognito-IDP客户端,并且API网关在其执行角色中具有所需的IAM权限。
如果问题仍未解决,可以尝试检查Cognito池的配置。确保公钥在池的验证配置中正确配置,并检查Cognito池的用户池域是否正确设置。
最后,您可以在AWS支持中心寻求帮助,他们可以提供一些日志并确定问题原因。