0

我使用 Digits 和 AWSCognito 2.3.6 对我的用户进行身份验证,我的代码如下所示:

let cognito = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USWest1, identityPoolId: cognitoIdentityPoolId)

let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: cognito)

AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

let credentials = authToken! + ";" + authSecret! 
cognito.logins = [ "www.digits.com" : credentials ]
cognito.getIdentityId().continueWithBlock { (task) -> AnyObject! in

    ....

}

在更新到 2.4.6 后,“登录”被弃用,我们中的许多人发现缺少有关如何使用推荐的替代 AWSIdentityProviderManager 或如何解决错误的文档:

"NotAuthorizedException","message":"Access to Identity 'us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' is forbidden."
4

2 回答 2

0

我设法找到的主要信息来源是GitHub 上的 open issue 。在阅读并找到 J.Toomey 在AWS 论坛上写的帖子后,我发现“AWSCognitoCredentialsProvider 是延迟加载的”。并且能够使用以下代码解决我的问题:

1) 实现了一个自定义 AWSIdentityProviderManager

import Foundation 
import AWSCognito

class CustomAWSProvider:NSObject, AWSIdentityProviderManager{
    var tokens : [NSString : NSString]?
    init(tokens: [NSString : NSString]) {
        self.tokens = tokens
        print("tokens : ", self.tokens);
    }
    @objc func logins() -> AWSTask {
        return AWSTask(result: tokens)
    } 
}

2) 使用此自定义提供程序如下:

let digitsConfiguration = DGTAuthenticationConfiguration(accountFields: .DefaultOptionMask)

Digits.sharedInstance().authenticateWithViewController(nil, configuration: digitsConfiguration) { session, error in


    if session != nil {

        let credentials = session.authToken + ";" + session.authTokenSecret
        let customProvider = CustomAWSProvider.init(tokens: [AWSIdentityProviderDigits : credentials])
        let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USWest1, identityPoolId: cognitoIdentityPoolId , identityProviderManager: customProvider)

        let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
        AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

        credentialsProvider.credentials().continueWithBlock { (task: AWSTask!) -> AnyObject! in

                return nil

                }.continueWithBlock { (task: AWSTask ) -> AnyObject in

                    return credentialsProvider.getIdentityId().continueWithBlock { (task: AWSTask!) -> AnyObject! in

                    if (task.result != nil) {

                        print("You have been successfully stored in Cognito")
                    }


                    if (task.error != nil) {
                        print(task.error!.localizedDescription)
                    }

                    if (task.exception != nil) {
                        print(task.exception!.description)
                    }

                    return nil
                }
        }

    } else {

        NSLog("Authentication error: %@", error!.localizedDescription)
    }
}
于 2016-08-07T00:56:21.463 回答
0

这是我在 2.4.10 和 Swift 3 上的工作示例。

一个 IdentityProviderManager:

class DigitsIdentityProviderManager:NSObject, AWSIdentityProviderManager {
    public func logins() -> AWSTask<NSDictionary> {
        let completion = AWSTaskCompletionSource<NSDictionary>()
        if let configuration = DGTAuthenticationConfiguration(accountFields: .defaultOptionMask) {
            configuration.appearance = DGTAppearance()
            configuration.appearance.backgroundColor = UIColor.white
            configuration.appearance.accentColor = UIColor.tintColor()

            Digits.sharedInstance().authenticate(with: nil, configuration:configuration) {(session, error) in
                if session != nil {
                    let value = session!.authToken + ";" + session!.authTokenSecret
                    print("digits: \(value)")
                    completion.setResult(["www.digits.com" : value as NSString])
                } else {
                    completion.setError(error!)
                }
            }
        }
        return completion.task
    }
}

调用

func handleDigitLogin() {
    let digitsIdentityProviderManager = DigitsIdentityProviderManager()
    let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.usEast1,
                                                            identityPoolId:Constants.Aws.CognitoPoolId,
                                                            identityProviderManager:digitsIdentityProviderManager)

    let serviceConfiguration = AWSServiceConfiguration(region: .usEast1, credentialsProvider: credentialsProvider)
    AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration

    credentialsProvider.clearKeychain()
    credentialsProvider.clearCredentials()

    let task = credentialsProvider.getIdentityId()
    task.continue(successBlock: { (task:AWSTask) -> Any? in
        if (task.error != nil ) {
            print("\(task.error)")
        } else {
            print("Task result: \(task.result)")
        }
        return nil
    })
}
于 2016-11-17T08:42:07.003 回答