войдите с помощью Apple, чтобы Firebase не удалось зарегистрироваться на FireBase - PullRequest
0 голосов
/ 05 марта 2020
  • Я пытаюсь создать регистрацию в Apple для создания базы. Я успешно зарегистрировался в Apple, но это дает мне следующую ошибку

    Необязательно (__ C .FIRAuthErrorCode) Необязательно (Ошибка домена = FIR AuthErrorDomain Code = 17094 «В запросе отсутствует одноразовый номер." UserInfo = { NSLocalizedDescription = Nonce отсутствует в запросе., FIRAuthErrorUserInfoNameKey = ERROR_MISSING_OR_INVALID_NONCE})

    Это мой код

*

import UIKit
import Foundation
import AuthenticationServices
import Firebase
import CryptoKit

class firstCV: UIViewController {

    fileprivate var currentNonce: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpView()



        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)


    }

    static func randomNonceString(length: Int = 32) -> String {
        precondition(length > 0)
        let charset: Array<Character> =
            Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
        var result = ""
        var remainingLength = length

        while remainingLength > 0 {
            let randoms: [UInt8] = (0 ..< 16).map { _ in
                var random: UInt8 = 0
                let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random)
                if errorCode != errSecSuccess {
                    fatalError("Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)")
                }
                return random
            }

            randoms.forEach { random in
                if length == 0 {
                    return
                }

                if random < charset.count {
                    result.append(charset[Int(random)])
                    remainingLength -= 1
                }
            }
        }

        return result
    }

    @available(iOS 13.0, *)
    override func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
      if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
        guard let nonce = currentNonce else {

          fatalError("Invalid state: A login callback was received, but no login request was sent.")
        }
        guard let appleIDToken = appleIDCredential.identityToken else {
          print("Unable to fetch identity token")
          return
        }
        guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
          print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
          return
        }

        // Initialize a Firebase credential.

        let credential = OAuthProvider.credential(withProviderID: "apple.com",
                                                  idToken: idTokenString,
                                                  accessToken: nonce)
        // Sign in with Firebase.
        Auth.auth().signIn(with: credential) { (authResult, error) in
            if (error != nil) {



                let castedError = error! as NSError
                let firebaseError = AuthErrorCode(rawValue: castedError.code)


            // Error. If error.code == .MissingOrInvalidNonce, make sure
            // you're sending the SHA256-hashed nonce as a hex string with
            // your request to Apple.

            return
          }
          let storyboard = UIStoryboard(name: "Main", bundle: nil)
          let controller = storyboard.instantiateViewController(withIdentifier: "ageVC")
          self.present(controller, animated: true, completion: nil)
          if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ageVC") as? ageVC
          {
            self.present(vc, animated: true, completion: nil)
          }
        }
      }
    }

    @available(iOS 13.0, *)
    override func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
      // Handle error.
      print("Sign in with Apple errored: \(error)")
    }


    @available(iOS 13, *)
    static func sha256(_ input: String) -> String {
      let inputData = Data(input.utf8)
      let hashedData = SHA256.hash(data: inputData)
      let hashString = hashedData.compactMap {
        return String(format: "%02x", $0)
      }.joined()

      return hashString
    }

    func setUpView(){

        if #available(iOS 13.0, *) {
            let appleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)
             appleButton.translatesAutoresizingMaskIntoConstraints = false

            appleButton.addTarget(self, action: #selector(didTapAppleButton), for: .touchUpInside)

            view.addSubview(appleButton)
            NSLayoutConstraint.activate([appleButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -30),
                                         appleButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 45),
                                         appleButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -45),
                                         appleButton.heightAnchor.constraint(equalToConstant: 40)])

            view.bringSubviewToFront(appleButton)

        } else {
            // Fallback on earlier versions
        }
    }



    @available(iOS 13.0, *)
    @objc
    func didTapAppleButton(){
        let nonce = firstCV.randomNonceString()
        currentNonce = nonce
        let appleIDProvider = ASAuthorizationAppleIDProvider()
        let request = appleIDProvider.createRequest()
        request.requestedScopes = [.email]
        request.nonce = firstCV.sha256(nonce)

        let controller = ASAuthorizationController(authorizationRequests: [request])

        controller.delegate = self
        controller.presentationContextProvider = self
        controller.performRequests()
    }

}


extension UIViewController: ASAuthorizationControllerDelegate{

    @available(iOS 13.0, *)
    public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authrezation: ASAuthorization){

    }

    @available(iOS 13.0, *)
    public func authorizationController(controller: ASAuthorizationController, didCompleteWithError: Error){

    }

}
extension UIViewController: ASAuthorizationControllerPresentationContextProviding{
    @available(iOS 13.0, *)
    public func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
         return view.window!
    }
}

1 Ответ

0 голосов
/ 09 марта 2020

Вы передаете токен доступа вместо необработанного одноразового номера во время инициализации OAuthCredential:

let credential = OAuthProvider.credential(withProviderID: "apple.com",
                                              idToken: idTokenString,
                                              rawNonce: nonce)
...