在AWS iOS SDK中,如何处理FORCE_CHANGE_PASSWORD用户状态 [英] In AWS iOS SDK, how do I handle FORCE_CHANGE_PASSWORD User Status

查看:87
本文介绍了在AWS iOS SDK中,如何处理FORCE_CHANGE_PASSWORD用户状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遵循了示例

https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample

要将交互式认知登录集成到我的iOS应用中。一切正常,但是在池中创建新用户时,他们最初的状态为FORCE_CHANGE_PASSWORD。

To integrate interactive cognito login to my iOS app. This is all working well, but when a new user is created in the pool, they initially have a FORCE_CHANGE_PASSWORD status.

对于android,您可以按照以下步骤操作

For android you can follow the procedure below

http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-android- sdk-authenticate-admin-created-user.html

但是对于iOS,我无法确定如何执行此操作。使用该示例,如果我尝试以FORCE_CHANGE_PASSWORD状态登录用户,则会在控制台日志中看到以下输出(为简洁起见,删除了一些值):

But for iOS I can't find out how to do this. Using the sample, if I attempt to login with a user in FORCE_CHANGE_PASSWORD status, I see the following output in the console logs (with some values removed for brevity):


{ ChallengeName: NEW_PASSWORD_REQUIRED, ChallengeParameters:{ requiredAttributes: [], userAttributes: {\ email_verified\:\ true\ ,\ custom:autoconfirm\:\ Y\, Session: xyz}

{"ChallengeName":"NEW_PASSWORD_REQUIRED","ChallengeParameters":{"requiredAttributes":"[]","userAttributes":"{\"email_verified\":\"true\",\"custom:autoconfirm\":\"Y\","Session":"xyz"}

以下是上述示例中SignInViewController的代码。

The following is the code from the SignInViewController from the sample detailed above.

import Foundation
import AWSCognitoIdentityProvider

class SignInViewController: UIViewController {
    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var password: UITextField!
    var passwordAuthenticationCompletion: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>?
    var usernameText: String?

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.password.text = nil
        self.username.text = usernameText
        self.navigationController?.setNavigationBarHidden(true,   animated: false)
    }

    @IBAction func signInPressed(_ sender: AnyObject) {
        if (self.username.text != nil && self.password.text != nil) {
            let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails(username: self.username.text!, password: self.password.text! )
            self.passwordAuthenticationCompletion?.set(result: authDetails)
        } else {
            let alertController = UIAlertController(title: "Missing information",
                                                message: "Please enter a valid user name and password",
                                                preferredStyle: .alert)
            let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
            alertController.addAction(retryAction)
        }
    }
}

extension SignInViewController: AWSCognitoIdentityPasswordAuthentication {

    public func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>)     {
        self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
        DispatchQueue.main.async {
            if (self.usernameText == nil) {
                self.usernameText = authenticationInput.lastKnownUsername
            }
        }
    }

    public func didCompleteStepWithError(_ error: Error?) {
        DispatchQueue.main.async {
        if let error = error as? NSError {
            let alertController = UIAlertController(title: error.userInfo["__type"] as? String,
                                                    message: error.userInfo["message"] as? String,
                                                    preferredStyle: .alert)
            let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
            alertController.addAction(retryAction)

            self.present(alertController, animated: true, completion:  nil)
            } else {
                self.username.text = nil
                self.dismiss(animated: true, completion: nil)
            }
        }
    }
}

didCompleteStepWithError 执行,错误为null,我希望它指示某些内容告诉我们用户需要更改密码。

When didCompleteStepWithError executes, error is null where I would expect it to indicate something to tell us that the user is required to change password.

我的问题确实是如何捕获输出到控制台的 ChallengeName: NEW_PASSWORD_REQUIRED json ?

My question is really how to catch the "ChallengeName":"NEW_PASSWORD_REQUIRED" json that is output to the console?

推荐答案

现在对它进行排序。

首先,您需要创建一个视图控制器,该控制器将允许用户指定新密码。 ViewController应该具有 AWSTaskCompletionSource< AWSCognitoIdentityNewPasswordRequiredDetails> 作为属性:

First, you need to create a view controller that will allow the user to specify the new password. The ViewController should have a AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails> as a property:

var newPasswordCompletion: AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>?

按照问题中提到的样本中的模式,然后实施 AWSCognitoIdentityNewPasswordRequired 作为视图控制器的扩展,如下所示:

Following the pattern in the sample referred to in the question, I then implemented AWSCognitoIdentityNewPasswordRequired as an extension to the view controller as follows:

extension NewPasswordRequiredViewController: AWSCognitoIdentityNewPasswordRequired {
    func getNewPasswordDetails(_ newPasswordRequiredInput: AWSCognitoIdentityNewPasswordRequiredInput, newPasswordRequiredCompletionSource:
    AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>) {                    
        self.newPasswordCompletion = newPasswordRequiredCompletionSource
    }

    func didCompleteNewPasswordStepWithError(_ error: Error?) {
        if let error = error as? NSError {
            // Handle error
        } else {
            // Handle success, in my case simply dismiss the view controller
            self.dismiss(animated: true, completion: nil)
        }
    }

}

再次关注问题中详细说明的样本的设计,向AppDelegate的 AWSCognitoIdentityInteractiveAuthenticationDelegate 扩展添加函数:

Again following the design of the sample detailed in the question, add a function to the AWSCognitoIdentityInteractiveAuthenticationDelegate extension to AppDelegate:

extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate {
    func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication {
        //Existing implementation
    }
    func startNewPasswordRequired() -> AWSCognitoIdentityNewPasswordRequired {
        // Your code to handle how the NewPasswordRequiredViewController is created / presented, the view controller should be returned
        return self.newPasswordRequiredViewController!
    }
}

这篇关于在AWS iOS SDK中,如何处理FORCE_CHANGE_PASSWORD用户状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆