AWS Cognito iOS开发者身份验证



我正在尝试使用经过开发人员身份验证的amazon cognito。我的API成功地返回了一个id和令牌。然而,当我使用这些令牌将内容上传到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-一个身份池,支持开发人员身份验证来创建身份提供程序。

@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

这似乎是角色信任的问题。我使用amazon web界面创建了身份池,并仔细检查了身份池id是否正确。我已经能够成功上传w个未经身份验证的身份,所以我相信这不是角色权限问题。

很抱歉给您带来这么多麻烦。

身份提供程序和凭据提供程序如何交互存在一个小问题,该问题没有正确记录或处理好。凭据提供程序根据提供程序上是否附加了登录名,使用unauth或auth角色arn进行转移。如果您没有在提供程序上存储任何额外的登录信息,它会将其视为未经身份验证,并使用未授权的角色,从而导致您看到的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:您可能需要考虑查看我们的端到端示例,以获得更好的处理方法。

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

如果您能够在未经身份验证的情况下成功做到这一点,那么这里有一些可能性。

首先,请确保您的未经身份验证的角色arn与您的已验证角色arn不同。此外,请确保在信任策略(可通过此链接中的相应角色访问)中,amr指向"已验证"。

如果你还有任何其他问题,这篇博客文章将在高水平上介绍这个过程。

最新更新