Xác thực bằng Apple

Bạn có thể cho phép người dùng xác thực bằng Firebase bằng ID Apple của họ bằng cách sử dụng Firebase SDK để thực hiện quy trình đăng nhập OAuth 2.0 toàn diện.

Trước khi bắt đầu

Để đăng nhập người dùng bằng Apple, trước tiên hãy định cấu hình tính năng Đăng nhập bằng Apple trên trang web dành cho nhà phát triển của Apple, sau đó dùng Apple làm nhà cung cấp dịch vụ đăng nhập cho dự án Firebase của bạn.

Tham gia Chương trình nhà phát triển của Apple

Chỉ các thành viên của Chương trình dành cho nhà phát triển của Apple mới có thể định cấu hình cho tính năng Đăng nhập bằng Apple.

Định cấu hình tính năng Đăng nhập bằng Apple

  1. Bật tính năng Đăng nhập bằng Apple cho ứng dụng của bạn trên trang Chứng chỉ, Giá trị nhận dạng và Hồ sơ trên trang web dành cho nhà phát triển của Apple.
  2. Liên kết trang web với ứng dụng của bạn như mô tả trong phần đầu tiên của bài viết Định cấu hình đăng nhập bằng Apple cho web. Khi được nhắc, hãy đăng ký URL sau làm URL trả lại:
    https://YOUR_FIREBASE_PROJECT_ID.firebaseapp.com/__/auth/handler
    Bạn có thể lấy mã dự án Firebase trên trang cài đặt bảng điều khiển của Firebase. Khi bạn hoàn tất, hãy ghi lại Mã dịch vụ mới mà bạn cần trong phần tiếp theo.
  3. Tạo quy trình Đăng nhập bằng khoá riêng tư của Apple. Bạn sẽ cần khoá riêng tư và mã khoá mới trong phần tiếp theo.
  4. Nếu bạn sử dụng bất kỳ tính năng nào của Xác thực Firebase gửi email cho người dùng, bao gồm đăng nhập bằng đường liên kết email, xác minh địa chỉ email, thu hồi thay đổi tài khoản và các tính năng khác, hãy định cấu hình dịch vụ chuyển tiếp email riêng tư của Apple và đăng ký noreply@YOUR_FIREBASE_PROJECT_ID.firebaseapp.com (hoặc miền mẫu email tuỳ chỉnh của bạn) để Apple có thể chuyển tiếp email do tính năng Xác thực Firebase gửi đến các địa chỉ email ẩn danh của Apple.

Cho phép Apple làm nhà cung cấp dịch vụ đăng nhập

  1. Thêm Firebase vào dự án Apple của bạn. Hãy nhớ đăng ký mã gói ứng dụng của bạn khi thiết lập ứng dụng trong bảng điều khiển của Firebase.
  2. Trong bảng điều khiển của Firebase, hãy mở phần Xác thực. Trên thẻ Sign in method (Phương thức đăng nhập), hãy bật nhà cung cấp Apple. Chỉ định Mã dịch vụ mà bạn đã tạo ở phần trước. Ngoài ra, trong phần cấu hình luồng mã OAuth, hãy chỉ định ID nhóm Apple của bạn cũng như khoá riêng tư và mã khoá mà bạn đã tạo ở phần trước.

Tuân thủ các yêu cầu của Apple về dữ liệu ẩn danh

Tính năng Đăng nhập bằng Apple cho phép người dùng ẩn danh dữ liệu của họ (bao gồm cả địa chỉ email) khi đăng nhập. Người dùng chọn tuỳ chọn này sẽ có địa chỉ email với miền privaterelay.appleid.com. Khi sử dụng tính năng Đăng nhập bằng Apple trong ứng dụng, bạn phải tuân thủ mọi chính sách hoặc điều khoản hiện hành của Apple dành cho nhà phát triển liên quan đến các mã nhận dạng Apple ẩn danh này.

Điều này bao gồm cả việc có được sự đồng ý cần thiết của người dùng trước khi bạn liên kết mọi thông tin cá nhân nhận dạng trực tiếp với ID Apple ẩn danh. Khi sử dụng tính năng Xác thực Firebase, bạn có thể thực hiện các thao tác sau:

  • Liên kết một địa chỉ email với một ID Apple ẩn danh hoặc ngược lại.
  • Liên kết số điện thoại với ID Apple ẩn danh hoặc ngược lại
  • Liên kết thông tin đăng nhập mạng xã hội không ẩn danh (Facebook, Google, v.v.) với ID Apple ẩn danh hoặc ngược lại.

Danh sách bên trên chưa đầy đủ. Hãy tham khảo Thoả thuận cấp phép của Chương trình dành cho nhà phát triển của Apple trong phần Gói thành viên trong tài khoản nhà phát triển của bạn để đảm bảo ứng dụng của bạn đáp ứng các yêu cầu của Apple.

Đăng nhập bằng Apple và xác thực bằng Firebase

Để xác thực bằng tài khoản Apple, trước tiên, hãy đăng nhập người dùng vào tài khoản Apple của họ bằng khung AuthenticationServices của Apple, sau đó sử dụng mã thông báo mã nhận dạng trong phản hồi của Apple để tạo đối tượng AuthCredential của Firebase:

  1. Đối với mỗi yêu cầu đăng nhập, hãy tạo một chuỗi ngẫu nhiên – "số chỉ dùng một lần" – mà bạn sẽ sử dụng để đảm bảo mã thông báo mã nhận dạng mà bạn nhận được đã được cấp riêng theo yêu cầu xác thực của ứng dụng. Đây là bước quan trọng để ngăn chặn các cuộc tấn công phát lại.

    Bạn có thể tạo một số chỉ dùng một lần được bảo mật được mã hoá bằng SecRandomCopyBytes(_:_:_), như trong ví dụ sau:

    Swift

    private func randomNonceString(length: Int = 32) -> String {
      precondition(length > 0)
      var randomBytes = [UInt8](repeating: 0, count: length)
      let errorCode = SecRandomCopyBytes(kSecRandomDefault, randomBytes.count, &randomBytes)
      if errorCode != errSecSuccess {
        fatalError(
          "Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)"
        )
      }
    
      let charset: [Character] =
        Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
    
      let nonce = randomBytes.map { byte in
        // Pick a random character from the set, wrapping around if needed.
        charset[Int(byte) % charset.count]
      }
    
      return String(nonce)
    }
    
        

    Objective-C

    // Adapted from https://auth0.com/docs/api-auth/tutorials/nonce#generate-a-cryptographically-random-nonce
    - (NSString *)randomNonce:(NSInteger)length {
      NSAssert(length > 0, @"Expected nonce to have positive length");
      NSString *characterSet = @"0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
      NSMutableString *result = [NSMutableString string];
      NSInteger remainingLength = length;
    
      while (remainingLength > 0) {
        NSMutableArray *randoms = [NSMutableArray arrayWithCapacity:16];
        for (NSInteger i = 0; i < 16; i++) {
          uint8_t random = 0;
          int errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random);
          NSAssert(errorCode == errSecSuccess, @"Unable to generate nonce: OSStatus %i", errorCode);
    
          [randoms addObject:@(random)];
        }
    
        for (NSNumber *random in randoms) {
          if (remainingLength == 0) {
            break;
          }
    
          if (random.unsignedIntValue < characterSet.length) {
            unichar character = [characterSet characterAtIndex:random.unsignedIntValue];
            [result appendFormat:@"%C", character];
            remainingLength--;
          }
        }
      }
    
      return [result copy];
    }
        

    Bạn sẽ gửi hàm băm SHA256 của số chỉ dùng một lần cùng với yêu cầu đăng nhập. Apple sẽ truyền dữ liệu này không thay đổi trong phản hồi. Firebase xác thực phản hồi bằng cách băm số chỉ dùng một lần ban đầu rồi so sánh với giá trị mà Apple chuyển.

    Swift

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

    Objective-C

    - (NSString *)stringBySha256HashingString:(NSString *)input {
      const char *string = [input UTF8String];
      unsigned char result[CC_SHA256_DIGEST_LENGTH];
      CC_SHA256(string, (CC_LONG)strlen(string), result);
    
      NSMutableString *hashed = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
      for (NSInteger i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
        [hashed appendFormat:@"%02x", result[i]];
      }
      return hashed;
    }
        
  2. Bắt đầu quy trình đăng nhập của Apple, bao gồm cả trong yêu cầu của bạn, hàm băm SHA256 của số chỉ dùng một lần và lớp uỷ quyền sẽ xử lý phản hồi của Apple (xem bước tiếp theo):

    Swift

    import CryptoKit
    
    // Unhashed nonce.
    fileprivate var currentNonce: String?
    
    @available(iOS 13, *)
    func startSignInWithAppleFlow() {
      let nonce = randomNonceString()
      currentNonce = nonce
      let appleIDProvider = ASAuthorizationAppleIDProvider()
      let request = appleIDProvider.createRequest()
      request.requestedScopes = [.fullName, .email]
      request.nonce = sha256(nonce)
    
      let authorizationController = ASAuthorizationController(authorizationRequests: [request])
      authorizationController.delegate = self
      authorizationController.presentationContextProvider = self
      authorizationController.performRequests()
    }
    

    Objective-C

    @import CommonCrypto;
    
    - (void)startSignInWithAppleFlow {
      NSString *nonce = [self randomNonce:32];
      self.currentNonce = nonce;
      ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
      ASAuthorizationAppleIDRequest *request = [appleIDProvider createRequest];
      request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
      request.nonce = [self stringBySha256HashingString:nonce];
    
      ASAuthorizationController *authorizationController =
          [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
      authorizationController.delegate = self;
      authorizationController.presentationContextProvider = self;
      [authorizationController performRequests];
    }
    
  3. Xử lý phản hồi của Apple trong quá trình triển khai ASAuthorizationControllerDelegate. Nếu đăng nhập thành công, hãy sử dụng mã thông báo mã nhận dạng trong phản hồi của Apple với số chỉ dùng một lần chưa được băm để xác thực với Firebase:

    Swift

    @available(iOS 13.0, *)
    extension MainViewController: ASAuthorizationControllerDelegate {
    
      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, including the user's full name.
          let credential = OAuthProvider.appleCredential(withIDToken: idTokenString,
                                                            rawNonce: nonce,
                                                            fullName: appleIDCredential.fullName)
          // Sign in with Firebase.
          Auth.auth().signIn(with: credential) { (authResult, error) in
            if error {
              // Error. If error.code == .MissingOrInvalidNonce, make sure
              // you're sending the SHA256-hashed nonce as a hex string with
              // your request to Apple.
              print(error.localizedDescription)
              return
            }
            // User is signed in to Firebase with Apple.
            // ...
          }
        }
      }
    
      func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        // Handle error.
        print("Sign in with Apple errored: \(error)")
      }
    
    }
    

    Objective-C

    - (void)authorizationController:(ASAuthorizationController *)controller
       didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
      if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
        ASAuthorizationAppleIDCredential *appleIDCredential = authorization.credential;
        NSString *rawNonce = self.currentNonce;
        NSAssert(rawNonce != nil, @"Invalid state: A login callback was received, but no login request was sent.");
    
        if (appleIDCredential.identityToken == nil) {
          NSLog(@"Unable to fetch identity token.");
          return;
        }
    
        NSString *idToken = [[NSString alloc] initWithData:appleIDCredential.identityToken
                                                  encoding:NSUTF8StringEncoding];
        if (idToken == nil) {
          NSLog(@"Unable to serialize id token from data: %@", appleIDCredential.identityToken);
        }
    
        // Initialize a Firebase credential, including the user's full name.
        FIROAuthCredential *credential = [FIROAuthProvider appleCredentialWithIDToken:IDToken
                                                                             rawNonce:self.appleRawNonce
                                                                             fullName:appleIDCredential.fullName];
    
        // Sign in with Firebase.
        [[FIRAuth auth] signInWithCredential:credential
                                  completion:^(FIRAuthDataResult * _Nullable authResult,
                                               NSError * _Nullable error) {
          if (error != nil) {
            // Error. If error.code == FIRAuthErrorCodeMissingOrInvalidNonce,
            // make sure you're sending the SHA256-hashed nonce as a hex string
            // with your request to Apple.
            return;
          }
          // Sign-in succeeded!
        }];
      }
    }
    
    - (void)authorizationController:(ASAuthorizationController *)controller
               didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
      NSLog(@"Sign in with Apple errored: %@", error);
    }
    

Không giống như các nhà cung cấp khác được tính năng Xác thực Firebase hỗ trợ, Apple không cung cấp URL ảnh.

Ngoài ra, khi người dùng chọn không chia sẻ email của họ với ứng dụng, Apple sẽ cấp cho người dùng đó một địa chỉ email duy nhất (ở dạng xyz@privaterelay.appleid.com). Nếu bạn đã định cấu hình dịch vụ chuyển tiếp email riêng tư, Apple sẽ chuyển tiếp các email gửi đến địa chỉ ẩn danh đến địa chỉ email thực của người dùng.

Xác thực lại và liên kết tài khoản

Bạn có thể dùng cùng một mẫu với reauthenticateWithCredential() để truy xuất thông tin đăng nhập mới cho những thao tác nhạy cảm yêu cầu đăng nhập gần đây:

Swift

// Initialize a fresh Apple credential with Firebase.
let credential = OAuthProvider.credential(
  withProviderID: "apple.com",
  IDToken: appleIdToken,
  rawNonce: rawNonce
)
// Reauthenticate current Apple user with fresh Apple credential.
Auth.auth().currentUser.reauthenticate(with: credential) { (authResult, error) in
  guard error != nil else { return }
  // Apple user successfully re-authenticated.
  // ...
}

Objective-C

FIRAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"apple.com",
                                                                   IDToken:appleIdToken,
                                                                  rawNonce:rawNonce];
[[FIRAuth auth].currentUser
    reauthenticateWithCredential:credential
                      completion:^(FIRAuthDataResult * _Nullable authResult,
                                   NSError * _Nullable error) {
  if (error) {
    // Handle error.
  }
  // Apple user successfully re-authenticated.
  // ...
}];

Ngoài ra, bạn có thể sử dụng linkWithCredential() để liên kết nhiều nhà cung cấp danh tính với các tài khoản hiện có.

Xin lưu ý rằng Apple yêu cầu bạn phải được người dùng đồng ý rõ ràng trước khi liên kết tài khoản Apple của họ với dữ liệu khác.

Tính năng Đăng nhập bằng Apple sẽ không cho phép bạn sử dụng lại thông tin xác thực để liên kết với một tài khoản hiện có. Nếu muốn liên kết thông tin đăng nhập "Đăng nhập bằng Apple" với một tài khoản khác, trước tiên, bạn phải thử liên kết các tài khoản bằng thông tin đăng nhập cũ của Apple, sau đó kiểm tra lỗi được trả về để tìm thông tin đăng nhập mới. Thông tin xác thực mới sẽ nằm trong từ điển userInfo của lỗi và có thể truy cập được qua khoá AuthErrorUserInfoUpdatedCredentialKey.

Ví dụ: để liên kết tài khoản Facebook với tài khoản Firebase hiện tại, hãy sử dụng mã truy cập bạn nhận được từ việc đăng nhập người dùng vào Facebook:

Swift

// Initialize a Facebook credential with Firebase.
let credential = FacebookAuthProvider.credential(
  withAccessToken: AccessToken.current!.tokenString
)
// Assuming the current user is an Apple user linking a Facebook provider.
Auth.auth().currentUser.link(with: credential) { (authResult, error) in
  // Facebook credential is linked to the current Apple user.
  // The user can now sign in with Facebook or Apple to the same Firebase
  // account.
  // ...
}

Objective-C

// Initialize a Facebook credential with Firebase.
FacebookAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:accessToken];
// Assuming the current user is an Apple user linking a Facebook provider.
[FIRAuth.auth linkWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
  // Facebook credential is linked to the current Apple user.
  // The user can now sign in with Facebook or Apple to the same Firebase
  // account.
  // ...
}];

Thu hồi mã thông báo

Apple yêu cầu các ứng dụng hỗ trợ tạo tài khoản phải cho phép người dùng bắt đầu xoá tài khoản của họ trong ứng dụng, như mô tả trong Nguyên tắc đánh giá trên App Store

Để đáp ứng yêu cầu này, hãy triển khai các bước sau:

  1. Hãy nhớ điền vào phần Mã dịch vụCấu hình luồng mã OAuth trong phần cấu hình nhà cung cấp tính năng Đăng nhập bằng Apple, như nêu trong phần Định cấu hình đăng nhập bằng Apple.

  2. Vì Firebase không lưu trữ mã thông báo của người dùng khi người dùng được tạo bằng tính năng Đăng nhập bằng Apple, nên bạn phải yêu cầu người dùng đăng nhập lại trước khi thu hồi mã thông báo của họ và xoá tài khoản.

    Swift

    private func deleteCurrentUser() {
      do {
        let nonce = try CryptoUtils.randomNonceString()
        currentNonce = nonce
        let appleIDProvider = ASAuthorizationAppleIDProvider()
        let request = appleIDProvider.createRequest()
        request.requestedScopes = [.fullName, .email]
        request.nonce = CryptoUtils.sha256(nonce)
    
        let authorizationController = ASAuthorizationController(authorizationRequests: [request])
        authorizationController.delegate = self
        authorizationController.presentationContextProvider = self
        authorizationController.performRequests()
      } catch {
        // In the unlikely case that nonce generation fails, show error view.
        displayError(error)
      }
    }
    
    
  3. Lấy mã uỷ quyền từ ASAuthorizationAppleIDCredential và sử dụng mã này để gọi Auth.auth().revokeToken(withAuthorizationCode:) nhằm thu hồi mã thông báo của người dùng.

    Swift

    func authorizationController(controller: ASAuthorizationController,
                                 didCompleteWithAuthorization authorization: ASAuthorization) {
      guard let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential
      else {
        print("Unable to retrieve AppleIDCredential")
        return
      }
    
      guard let _ = currentNonce else {
        fatalError("Invalid state: A login callback was received, but no login request was sent.")
      }
    
      guard let appleAuthCode = appleIDCredential.authorizationCode else {
        print("Unable to fetch authorization code")
        return
      }
    
      guard let authCodeString = String(data: appleAuthCode, encoding: .utf8) else {
        print("Unable to serialize auth code string from data: \(appleAuthCode.debugDescription)")
        return
      }
    
      Task {
        do {
          try await Auth.auth().revokeToken(withAuthorizationCode: authCodeString)
          try await user?.delete()
          self.updateUI()
        } catch {
          self.displayError(error)
        }
      }
    }
    
    
  4. Cuối cùng, hãy xoá tài khoản người dùng (và tất cả dữ liệu liên quan)

Các bước tiếp theo

Sau khi người dùng đăng nhập lần đầu tiên, một tài khoản người dùng mới sẽ được tạo và liên kết với thông tin đăng nhập (chẳng hạn như tên người dùng và mật khẩu, số điện thoại hoặc thông tin của nhà cung cấp dịch vụ xác thực) mà người dùng đã đăng nhập. Tài khoản mới này được lưu trữ như một phần trong dự án Firebase và có thể được dùng để xác định người dùng trên mọi ứng dụng trong dự án của bạn, bất kể người dùng đăng nhập bằng cách nào.

  • Trong các ứng dụng, bạn có thể lấy thông tin hồ sơ cơ bản của người dùng từ đối tượng User . Xem phần Quản lý người dùng.

  • Trong Quy tắc bảo mật của Cloud Storage và Cơ sở dữ liệu theo thời gian thực của Firebase, bạn có thể lấy mã nhận dạng người dùng riêng biệt của người dùng đã đăng nhập từ biến auth rồi sử dụng mã này để kiểm soát những dữ liệu mà người dùng có thể truy cập.

Bạn có thể cho phép người dùng đăng nhập vào ứng dụng của mình thông qua nhiều nhà cung cấp dịch vụ xác thực bằng cách liên kết thông tin đăng nhập của nhà cung cấp dịch vụ xác thực với một tài khoản người dùng hiện có.

Để đăng xuất một người dùng, hãy gọi signOut:.

Swift

let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print("Error signing out: %@", signOutError)
}

Objective-C

NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
  NSLog(@"Error signing out: %@", signOutError);
  return;
}

Bạn cũng nên thêm mã xử lý lỗi cho toàn bộ các lỗi xác thực. Hãy xem bài viết Xử lý lỗi.