【问题标题】:AWS: How to properly authenticate a user against Cognito Pool and use it for Cognito Federated Identity?AWS:如何针对 Cognito 池正确验证用户身份并将其用于 Cognito 联合身份?
【发布时间】:2017-03-24 06:54:20
【问题描述】:

我正在开发一个将使用两个身份验证提供程序的应用程序:

  • 脸书
  • Cognito 用户池

对于前者,我没有任何问题,一切都按预期进行。但是,在使用 Cognito 用户池设置身份验证时,我遇到了一堵墙。我正在使用 AWS SDK 2.4.9、XCode 8 和 Swift 3。

我知道已经提出了很多问题,并且有很多“指南”。但是,其中很多都是针对过时的文档和 SDK 回答/制作的。甚至 AWS 的官方文档也已经过时了。

我正在经历的身份验证步骤如下:

1.配置初始认知池

///  Set the default service configuration
let serviceConfiguration = AWSServiceConfiguration(region: AWSRegionType.usEast1, credentialsProvider: nil)
AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration

/// Create a pool configuration and register it for a specific key to use later
let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: appClientID, clientSecret: appClientSecret, poolId: poolID)  
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPool(with: poolConfiguration, forKey: poolKey)

/// Create a pool for a specific predefined key
pool = AWSCognitoIdentityUserPool(forKey: poolKey)

2。针对 Cognito 用户池对用户进行身份验证

  user.getSession(username, password: password, validationData: nil).continue({ (task) -> AnyObject? in

        if let error = task.error as? NSError {
            completionHandler(error)
            return nil
        }

        let session = task.result! as AWSCognitoIdentityUserSession
        let token = session.idToken!.tokenString

        let tokens : [NSString:NSString] = ["cognito-idp.us-east-1.amazonaws.com/\(self.poolID!)" as NSString : token as NSString]
        let identityProvider = CognitoPoolIdentityProvider(tokens: tokens)

        let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .usEast1, identityPoolId: self.identityPoolID, identityProviderManager: identityProvider)

        ///  Set the default service configuration
        let serviceConfiguration = AWSServiceConfiguration(region: AWSRegionType.usEast1, credentialsProvider: credentialsProvider)
        AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration

        credentialsProvider.getIdentityId().continue({ (task) -> AnyObject? in
            completionHandler(task.error as NSError?)
            return nil
        })

        return nil
    })

3. CognitoPoolIdentityProvider 类

    class CognitoPoolIdentityProvider : NSObject, AWSIdentityProviderManager {

      var tokens : NSDictionary = [:]

      init(tokens: [NSString : NSString]) {
           self.tokens = tokens as NSDictionary
      }

      @objc func logins() -> AWSTask<NSDictionary> {
           return AWSTask(result: tokens)
      }

    }

4.将数据存储到 Cognito 联合身份

所有这些都顺利通过,没有任何错误。但是,现在我想将从 Cognito 池中提取的数据存储到特定的 Cognito 联合身份数据集,所以我打电话给:userProfile.synchronize().continue,我得到以下结果:

getCredentialsWithCognito:authenticated:customRoleArn:]_block_invoke | GetCredentialsForIdentity 失败。错误是[错误 域=com.amazonaws.AWSCognitoIdentityErrorDomain 代码=8“(空)” UserInfo={__type=NotAuthorizedException, message=访问身份 'us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' 被禁止。}]

2016-11-10 10:27:16.947365 xxxxxxxx[19867:5614838] AWSiOSSDK v2.4.11 [错误] AWSIdentityProvider.m 行:304 | __52-[AWSCognitoCredentialsProviderHelper getIdentityId]_block_invoke.255 |获取 ID 失败。错误是[错误 域=com.amazonaws.AWSCognitoIdentityErrorDomain 代码=8“(空)” UserInfo={__type=NotAuthorizedException,消息=未验证 此身份池不支持访问。}] 2016-11-10 10:27:16.947726 xxxxxxxx[19867:5614838] AWSiOSSDK v2.4.11 [错误]

AWSCredentialsProvider.m 行:577 | __44-[AWSCognitoCredentialsProvider 凭证]_block_invoke.352 |无法刷新。错误是[错误 域=com.amazonaws.AWSCognitoIdentityErrorDomain 代码=8“(空)” UserInfo={__type=NotAuthorizedException,消息=未验证 此身份池不支持访问。}] 2016-11-10 10:27:16.948452 xxxxxxxx[19867:5614838] AWSiOSSDK v2.4.11 [错误]

AWSCognitoDataset.m 行:352 | __30-[AWSCognitoDataset 同步拉动:]_block_invoke |无法列出记录:错误 域=com.amazonaws.AWSCognitoIdentityErrorDomain 代码=8“(空)” UserInfo={__type=NotAuthorizedException,消息=未验证 此身份池不支持访问。} [10:27:16]: saveSettings AWS 任务错误:无法完成操作。 (com.amazonaws.AWSCognitoIdentityErrorDomain 错误 8。)

更改日志级别后,我可以看到以下内容:

//请求

2016-11-10 10:33:08.095735 xxxxxxxx[19874:5616142] AWSiOSSDK v2.4.11 [调试] AWSURLSessionManager.m 行:543 | -[AWSURLSessionManager printHTTPHeadersAndBodyForRequest:] |请求正文:{"IdentityId":"us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"}

//响应

2016-11-10 10:33:08.714268 xxxxxxxx[19874:5616154] AWSiOSSDK v2.4.11 [调试] AWSURLSessionManager.m 行:553 | -[AWSURLSessionManager printHTTPHeadersForResponse:] |响应标头:{ 连接=“保持活动”; “内容长度”= 129; “内容类型”=“应用程序/x-amz-json-1.1”; 日期 =“2016 年 11 月 10 日星期四 09:33:08 GMT”; "x-amzn-ErrorMessage" = "禁止访问身份 'us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx'。"; "x-amzn-ErrorType" = "NotAuthorizedException:"; “x-amzn-RequestId”=“b0ac6fb0-a728-11e6-8413-1fdb846185bb”; }

上述请求是 GetID API 调用。显然,它与 AWS Docs 中的请求格式不匹配:http://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetId.html

根据 AWSServiceManager 类,我们有这个:

/**
 The default service configuration object. This property can be set only once, and any subsequent setters are ignored.
 */
@property (nonatomic, copy) AWSServiceConfiguration *defaultServiceConfiguration;

这意味着设置新的服务配置是没有意义的,但我看不到其他方法可以刷新通过 Cognito 用户池身份验证获得的凭据。

差不多就是这样。有什么想法吗?

谢谢

【问题讨论】:

  • 这个用户来自哪里。你能提到用户类型

标签: ios swift amazon-web-services federated-identity


【解决方案1】:

看来你得到的错误

  Access to Identity 'us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' is forbidden

您在第一部分中获得的凭据无法访问您进行同步调用的身份,因此您的身份可能已更改。

【讨论】:

  • 你能提供更多的粒度吗?我收到类似的错误
猜你喜欢
  • 2018-04-05
  • 2016-08-18
  • 2018-02-11
  • 2018-11-07
  • 2017-02-28
  • 2017-01-02
  • 2023-04-03
  • 2020-07-31
  • 2016-12-29
相关资源
最近更新 更多