Swift 4 - повторная аутентификация FaceID - PullRequest
2 голосов
/ 30 апреля 2019

Я включил проверку подлинности Face ID в своем приложении, и пользователь получает проверку подлинности при нажатии кнопки. Я также реализовал метод выхода из системы, который выводит пользователя из системы:

dismiss(animated: true, completion: {

UserDefaults.standard.set(false, forKey: "hasLoginKey")

})

Однако, когда я выхожу, а затем пытаюсь снова войти в систему, мне не предлагается FaceID, вместо этого он пропускается, и я полностью авторизируюсь. У меня вопрос, как я могу предотвратить это и предлагать пользователю входить в систему каждый раз, когда он нажимает кнопку?

Вот код кнопки:

@IBAction func loginButtonPressed(_ sender: Any) {

        //Define Button variable from the button that has been tapped.
        let button = sender as! UIButton

        //If the button tag is Touch ID, authenticate the user

        if(button.tag == loginWithTouchID)
        {
            //Check if device is compatible with Touch ID
            if(touchMe.canEvaluatePolicy())
            {
                //Get Response from Touch ID popup
                touchMe.authenticateUser() { responsCode in

                    if let responsCode = responsCode {

                        if(responsCode == 0)
                        {
                            //If Touch ID is not available
                            self.customAlert(title: "Error", message: "Touch ID not available")
                        }
                        else if(responsCode == 1)
                        {
                            //If Touch ID has not been setup
                            self.customAlert(title: "Error", message: "Touch ID may not be configured")
                        }
                        else if(responsCode == 2)
                        {
                            //If Touch ID authentication failed
                            self.customAlert(title: "Error", message: "There was a problem verifying your identity")
                        }

                    } else {

                        //If there is no response code, that means Touch ID was successful in authenticating user and we can now call the login method
                        Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(Login.login), userInfo: nil, repeats: false)
                    }
                }
            }
        }
        else
        {
            Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(Login.login), userInfo: nil, repeats: false)
        }


    }

и мой класс TouchIDAuth

class TouchIDAuth {

    let context = LAContext()

    func canEvaluatePolicy() -> Bool {
        return context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
    }

    func authenticateUser(completion: @escaping (NSNumber?) -> Void) {

        guard canEvaluatePolicy() else {
            completion(0)
            return
        }

        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Logging in with Touch ID") { (success, evaluateError) in
            if success {
                DispatchQueue.main.async {
                    completion(nil)
                }
            } else {

                let response: NSNumber

                switch evaluateError?._code {
                case Int(kLAErrorAuthenticationFailed):
                    response = 2
                case Int(kLAErrorUserCancel):
                    response = 3
                case Int(kLAErrorUserFallback):
                    response = 4
                default:
                    response = 1
                }

                completion(response)

            }
        }
    }

}

Вот метод входа в систему, который вызывается при нажатии кнопки

@objc func login() {

        //Start Activity Indicator

        self.createIndicator()

        //Define Username and Password Variables

        var user: String!
        var pass: String!


        //Check if User is Authenticating with TouchID, we do this so we know to use credentials from Keychain to make the API call with
        if(loginButton.tag == loginWithTouchID)
        {
            //If Yes, Get the username from Keychain
            if let storedUsername = UserDefaults.standard.value(forKey: "username") as? String {

                //Get Password from Keychain

                do {

                    let passwordItem = KeychainPasswordItem(service: KeychainConfiguration.serviceName,
                                                            account: storedUsername,
                                                            accessGroup: KeychainConfiguration.accessGroup)
                    let keychainPassword = try passwordItem.readPassword()

                    //Store Username and Password from Keychain into Username and Password variables

                    user = storedUsername
                    pass = keychainPassword

                }
                catch {

                    //If something went wrong, stop the Activity Indicator and Alert the user something went wrong.

                    self.stopIndicator()

                    self.customAlert(title: "Error", message: "Error reading password from keychain - \(error)")
                }

            }
        }
        else
        {
            //If we are not using Touch ID, store the username and password text field into the username and password variable to use for the API Call

            user = username.text!
            pass = password.text!
        }

        //Finally call the webservice

        WebService().loginUser(user, password: pass)
        {
            (result: Bool) in
            //If API call is successful
            if(result == true)
            {

                //Stop Activity Indicator

                self.stopIndicator()

                //Check if button tag is create, login or touch ID

                if self.loginButton.tag == self.createLoginButtonTag {

                    //If create, check if a user has login

                    let hasLoginKey = UserDefaults.standard.bool(forKey: "hasLoginKey")
                    if !hasLoginKey {

                        //If not, add username to App Default

                        UserDefaults.standard.setValue(user, forKey: "username")
                    }

                    //Try and save the password to Keychain

                    do {

                        //Create a KeychainPasswordItem

                        let passwordItem = KeychainPasswordItem(service: KeychainConfiguration.serviceName, account: user!, accessGroup: KeychainConfiguration.accessGroup)

                        //Save password to the new KeychainPasswordItem

                        try passwordItem.savePassword(pass!)

                        //Add hasLoginKey bool to App Defaults

                        UserDefaults.standard.set(true, forKey: "hasLoginKey")

                        //Change Login button tag to Login as we do not need to create this user again

                        self.loginButton.tag = self.loginButtonTag

                        //Store Credentials to App Delegate to make API calls down the road.

                        self.appDelegate.username = user
                        self.appDelegate.password = pass

                        self.password.text = ""

                        //Everything has been authenticated, proceed to Dashboad

                        self.performSegue(withIdentifier: "toolbarSegue", sender: nil)


                    } catch {

                        //Something went wrong, alert the user with error.

                        self.customAlert(title: "Error", message: "Error updating keychain - \(error)")

                    }

                }
                    //If Login Button tag with Login
                else if self.loginButton.tag == self.loginButtonTag {

                    //Check if user exists in Keychain

                    if self.checkLogin(username: user, password: pass) {

                        //Store Credentials to App Delegate to make API calls down the road.

                        self.appDelegate.username = user
                        self.appDelegate.password = pass

                        self.password.text = ""

                        //Exisiting user has been authenticated, proceed to Dashboad

                        self.performSegue(withIdentifier: "toolbarSegue", sender: nil)

                    } else {

                        //User does not exist in Keychain, alert user there is an error.

                        self.customAlert(title: "Login Problem", message: "Sorry Login Failed, User and/or Passsword Incorrect")
                    }

                }
                    //If Login Button tag with Touch ID
                else if self.loginButton.tag == self.loginWithTouchID {

                    //Store Credentials to App Delegate to make API calls down the road.

                    self.appDelegate.username = user
                    self.appDelegate.password = pass

                    self.password.text = ""

                    //Touch ID has been authenticated, proceed to Dashboad

                    self.performSegue(withIdentifier: "toolbarSegue", sender: nil)

                }

            }
            else
            {

                //Stop Activity Indicator

                self.stopIndicator()

                //API call was unsuccessful, alert user.

                self.customAlert(title: "Login Problem", message: "Sorry Login Failed, User and/or Passsword Incorrect")

            }

        }


    }

Ответы [ 2 ]

2 голосов
/ 02 мая 2019

Я думаю, что вы меняете тег кнопки входа в login, в этой строке:

//Change Login button tag to Login as we do not need to create this user again, as you have specified

self.loginButton.tag = self.loginButtonTag

Именно поэтому, когда пользователь нажимает кнопку входа в следующий раз, он напрямую аутентифицирует пользователя (поскольку условие false):

 if(button.tag == loginWithTouchID) {
      // login with touchID
else {
      // authenticate the user
}

Так что я думаю, что вы не должны менять self.loginButton.tag

0 голосов
/ 07 мая 2019

Примечание. В свете вашей проблемы

Однако, когда я выхожу, а затем пытаюсь снова войти в систему, меня не запрашивают FaceID, вместо этого он пропускается, и яЯ полностью авторизован.

Обычно, когда вы обновляете UserDefaults (т. е. устанавливаете новые значения / объекты для ключа (ей)), вы добавляете следующий фрагмент кода, чтобы изменения вступили в силу.

UserDefaults.standard.syncronize()

Итак, добавьте его после

UserDefaults.standard.set(false, forKey: "hasLoginKey") // 1


UserDefaults.standard.setValue(user, forKey: "username") // 2


UserDefaults.standard.set(true, forKey: "hasLoginKey") // 3

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...