2014-10-10 93 views
3

我正在嘗試使用開發人員身份驗證的amazon cognito。我的API已成功返回一個id和令牌。然而,當我使用這些標記將內容上傳到S3我收到以下錯誤:AWS Cognito iOS開發人員身份驗證

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 

這似乎是角色信任的問題。我使用亞馬遜網頁界面創建了身份池,並且雙重檢查身份池ID是否正確。我已經能夠成功上傳w未經身份驗證的身份,所以我認爲這不是角色權限問題​​。

+0

可能有點偏離主題,但我想問一下爲什麼在'-getIdentityID'方法的'else'塊中返回[[BFTask ...];'而不是簡單的'return [self refresh ];'? – slava 2016-01-04 18:58:39

回答

3

如果您在未經身份驗證的情況下成功完成此操作,這裏有幾種可能性。

首先,確保您的未經身份驗證的角色arn與您的經過身份驗證的角色arn不同。 此外,請確保在信任策略中(可通過this link的相應角色訪問),amr指向「已驗證」。

如果您有任何其他問題,this blog post會在較高級別上完成此過程。

+0

我已經關注了上面鏈接的博客帖子,確保未經身份驗證的角色arn與我的身份驗證角色arn不同,但仍然沒有任何成功。但是,當我將unauthRoleArn設置爲authRoleArn作爲authRoleArn時,我能夠成功發佈到AWS,但我不明白爲什麼這會起作用/不習慣部署此功能。 – jame 2014-10-14 17:22:36

+0

您能否確認您的信任政策amr指向已通過身份驗證? – 2014-10-15 17:23:53

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; 

更新2015年3月10日:你可能要考慮看​​看我們end-to-end example爲處理這更好的方法。

這個樣本,我們包含用戶標識符的實際值,然後將logins屬性的全部內容傳遞給後端。

+0

謝謝!這正是問題......現在一切都按預期運作。 – jame 2014-11-06 22:43:57

+1

也救了我,這是不是在文檔中的任何理由? – Pierre 2015-01-20 18:03:56

+1

我們正在研究文檔的更新以及說明性的端到端示例。 – 2015-01-20 20:18:10

相關問題