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

jam*_*ame 3 amazon-s3 amazon-web-services ios amazon-cognito

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

Not authorized to perform sts:AssumeRoleWithWebIdentity
Run Code Online (Sandbox Code Playgroud)

以下是我设置凭据提供程序的代码.

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;
Run Code Online (Sandbox Code Playgroud)

而且我在使用所提供的模板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
Run Code Online (Sandbox Code Playgroud)

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

Bob*_*ney 6

对不起,所有的烦恼.

身份提供者和凭证提供者如何进行交互并没有正确记录或处理得很好.凭证提供程序根据是否在提供程序上附加登录名来使用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;
Run Code Online (Sandbox Code Playgroud)

更新2015-03-10:您可能需要考虑查看我们的端到端示例,以获得更好的处理方法.

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