iOS AWSCognito 2.4.X AWSIdentityProviderManager数字示例

时间:2016-08-07 00:56:21

标签: ios swift aws-sdk amazon-cognito twitter-digits

我使用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."

2 个答案:

答案 0 :(得分:0)

我设法找到的主要信息来源是GitHub上的未解决问题。在阅读并查找J.Toomey在AWS Discussion Forum上撰写的帖子后,我发现" 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)
    }
}

答案 1 :(得分: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
    })
}