ios - AWS Cognito iOS 开发人员身份验证

标签 ios amazon-web-services amazon-s3 amazon-cognito

我正在尝试将 amazon cognito 与开发人员身份验证一起使用。我的 API 成功返回了一个 ID 和 token 。但是,当我使用这些 token 将内容上传到 S3 时,我收到以下错误:

Not authorized to perform sts:AssumeRoleWithWebIdentity

下面是我设置凭据提供程序的代码。

ZGAWSIdentityProvider *identityProvider = [ZGAWSIdentityProvider new];
[identityProvider setIdentityPoolId:AWS_IDENTITY_POOL_ID];

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
                                           initWithRegionType:AWSRegionUSEast1
                                           identityProvider:identityProvider
                                           unauthRoleArn:AWS_UNAUTH_ROLE_ARN
                                           authRoleArn:AWS_AUTH_ROLE_ARN];


AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSWest1
                                                                      credentialsProvider:credentialsProvider];

[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

我正在使用 http://docs.aws.amazon.com/mobile/sdkforios/developerguide/cognito-auth.html#create-an-identity-pool-that-supports-developer-authenticated-identities 提供的模板创建身份提供者。

@implementation ZGAWSIdentityProvider
@synthesize identityPoolId=_identityPoolId;
@synthesize identityId=_identityId;
@synthesize token=_token;


- (BFTask *)getIdentityId {
    // Should ensure that identityId property is valid. The below code can probably
    // be used for most use cases.

    if (self.identityId) {
        return [BFTask taskWithResult:nil];
    } else {
        return [[BFTask taskWithResult:nil] continueWithBlock:^id(BFTask *task) {
            if (!self.identityId) {
                return [self refresh];
            }
            return nil;
        }];
    }
}

- (BFTask *)refresh {

    BFTaskCompletionSource *task = [BFTaskCompletionSource taskCompletionSource];
    __weak __typeof(self)weakSelf = self;
    [[ZGAccountController sharedInstance] getAWSCredentialsWithCompletion:^(NSDictionary *credentials) {

        if (credentials && [credentials objectForKey:@"identity_id"] && [credentials objectForKey:@"identity_id"]) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            strongSelf.identityId = [credentials objectForKey:@"identity_id"];
            strongSelf.token = [credentials objectForKey:@"token"];
            [task setResult:nil];
        } else {
            NSError *error = [NSError errorWithDomain:@"com.##.##" code:-1 userInfo:nil];
            [task setError:error];
        }

    }];

    return task.task;
}

@end

这似乎是 Role Trust 的问题。我使用亚马逊网络界面创建了身份池,并仔细检查了身份池 ID 是否正确。我已经能够成功上传未经身份验证的身份,所以我相信这不是角色权限问题​​。

最佳答案

抱歉给您带来了麻烦。

身份提供者和凭据提供者如何交互存在一个小问题,没有正确记录或处理得当。凭据提供程序使用 unauth 或 auth 角色 arn 根据提供程序上是否附加了登录信息进行枢转。如果您没有在提供程序上存储任何其他登录信息,它会将其视为未经身份验证并使用 unauth 角色并导致您看到的 STS 错误。您可以通过在身份提供者的刷新中执行类似以下操作来解决此问题:

// add login to the map to make sure CredentialsProvider treats us as authenticated
NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:self.logins];
[temp setObject:@"temp" forKey:@"myprovider"];
self.logins = temp;

2015-03-10 更新:您可能需要考虑查看我们的 end-to-end example寻找更好的方法来处理这个问题。

在这个示例中,我们包含了用户标识符的实际值,然后将 logins 属性的全部内容传递给后端。

关于ios - AWS Cognito iOS 开发人员身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26290256/

相关文章:

amazon-web-services - 将docker部署到AWS而不进行负载均衡

Amazon S3 上的 SSL 为 "static website"

javascript - 上传S3 knox node js(签名不匹配)

ios - 在类中设置和删除布局

ios - 如何在 Swift 中打印 'catch all' 异常的详细信息?

json - cloudformation 模板中的 opsworks 参数和资源

amazon-web-services - 仅在AWS EC2实例上运行Kubernetes

ios - SCN Material : can't find init:mdlMaterial:

ios - 从 Chrome 打开 iTunes 的链接

amazon-web-services - 如何在dynamodb中使用自动递增主键id