使用 Apple 和 C++ 進行身份驗證

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

通過使用 Firebase SDK 執行端到端 OAuth 2.0 登錄流程,您可以讓用戶使用其 Apple ID 向 Firebase 進行身份驗證。

在你開始之前

要使用 Apple 登錄用戶,請首先在 Apple 的開發者網站上配置 Sign In with Apple,然後將 Apple 啟用為您的 Firebase 項目的登錄提供商。

加入 Apple 開發者計劃

Sign In with Apple 只能由Apple Developer Program的成員配置。

配置使用 Apple 登錄

必須在 Firebase 項目中啟用並正確配置 Apple 登錄。配置因 Android 和 Apple 平台而異。在繼續之前,請按照Apple 平台和/或Android指南的“配置 Apple 登錄”部分進行操作。

啟用 Apple 作為登錄提供商

  1. Firebase 控制台中,打開Auth部分。在登錄方法選項卡上,啟用Apple提供程序。
  2. 配置 Apple 登錄提供程序設置:
    1. 如果您僅在 Apple 平台上部署您的應用程序,您可以將服務 ID、Apple 團隊 ID、私鑰和密鑰 ID 字段留空。
    2. 對於 Android 設備的支持:
      1. 將 Firebase 添加到您的 Android 項目中。在 Firebase 控制台中設置應用時,請務必註冊應用的 SHA-1 簽名。
      2. Firebase 控制台中,打開Auth部分。在登錄方法選項卡上,啟用Apple提供程序。指定您在上一節中創建的服務 ID。此外,在 OAuth 代碼流配置部分中,指定您的 Apple Team ID 以及您在上一部分中創建的私鑰和密鑰 ID。

遵守 Apple 匿名數據要求

Sign In with Apple 為用戶提供了在登錄時匿名其數據(包括電子郵件地址)的選項。選擇此選項的用戶擁有域為privaterelay.appleid.com的電子郵件地址。當您在您的應用程序中使用 Sign In with Apple 時,您必須遵守 Apple 有關這些匿名 Apple ID 的任何適用開發者政策或條款。

這包括在您將任何直接識別個人信息與匿名 Apple ID 相關聯之前獲得任何必要的用戶同意。使用 Firebase 身份驗證時,這可能包括以下操作:

  • 將電子郵件地址鏈接到匿名 Apple ID,反之亦然。
  • 將電話號碼鏈接到匿名 Apple ID,反之亦然
  • 將非匿名社交憑證(Facebook、Google 等)鏈接到匿名 Apple ID,反之亦然。

上面的列表並不詳盡。請參閱開發者帳戶成員部分中的 Apple 開發者計劃許可協議,以確保您的應用程序符合 Apple 的要求。

訪問firebase::auth::Auth

Auth類是所有 API 調用的網關。
  1. 添加 Auth 和 App 頭文件:
    #include "firebase/app.h"
    #include "firebase/auth.h"
    
  2. 在您的初始化代碼中,創建一個firebase::App類。
    #if defined(__ANDROID__)
      firebase::App* app =
          firebase::App::Create(firebase::AppOptions(), my_jni_env, my_activity);
    #else
      firebase::App* app = firebase::App::Create(firebase::AppOptions());
    #endif  // defined(__ANDROID__)
    
  3. 為您的firebase::App獲取firebase::auth::Auth類。 AppAuth之間存在一對一的映射關係。
    firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
    

使用 Firebase SDK 處理登錄流程

使用 Apple 登錄的流程因 Apple 和 Android 平台而異。

在 Apple 平台上

通過從您的 C++ 代碼調用的 Apple Sign In Objective-C SDK 使用 Firebase 對您的用戶進行身份驗證。

  1. 對於每個登錄請求,生成一個隨機字符串——一個“nonce”——你將使用它來確保你獲得的 ID 令牌是專門為響應你的應用程序的身份驗證請求而授予的。此步驟對於防止重放攻擊很重要。

      - (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--;
            }
          }
        }
      }
    
    

    您將在您的登錄請求中發送隨機數的 SHA256 哈希,Apple 將在響應中保持不變。 Firebase 通過散列原始隨機數並將其與 Apple 傳遞的值進行比較來驗證響應。

  2. 啟動 Apple 的登錄流程,在您的請求中包括 nonce 的 SHA256 哈希和將處理 Apple 響應的委託類(請參閱下一步):

      - (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];
      }
    
      - (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;
      }
    
  3. 在您的 ASAuthorizationControllerDelegate 實現中處理 Apple 的響應。如果登錄成功,請使用 Apple 響應中的 ID 令牌和未散列的 nonce 向 Firebase 進行身份驗證:

      - (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);
          }
        }
    
  4. 使用生成的令牌字符串和原始隨機數構建 Firebase 憑據並登錄 Firebase。

    firebase::auth::OAuthProvider::GetCredential(
            /*provider_id=*/"apple.com", token, nonce,
            /*access_token=*/nullptr);
    
    firebase::Future<firebase::auth::User*> result =
        auth->SignInWithCredential(credential);
    
  5. 相同的模式可以與Reauthenticate一起使用,它可用於檢索需要最近登錄的敏感操作的新憑據。

    firebase::Future<firebase::auth::SignInResult> result =
        user->Reauthenticate(credential);
    
  6. 相同的模式可用於將帳戶與 Apple Sign In 關聯。但是,如果現有 Firebase 帳戶已與您嘗試關聯的 Apple 帳戶相關聯,您可能會遇到錯誤。發生這種情況時,future 將返回SignInResult狀態,並且kAuthErrorCredentialAlreadyInUse的 UserInfo 對象可能包含有效的updated_credential 。此憑據可用於通過SignInWithCredential登錄 Apple 關聯帳戶,而無需生成另一個 Apple 登錄令牌和隨機數。

    請注意,此操作必須使用LinkAndRetrieveDataWithCredential才能包含憑據,因為updated_credentialSignInResult.UserInfo對象的成員。

    firebase::Future<firebase::auth::SignInResult> link_result =
        auth->current_user()->LinkAndRetrieveDataWithCredential(credential);
    
    // To keep example simple, wait on the current thread until call completes.
    while (link_result.status() == firebase::kFutureStatusPending) {
      Wait(100);
    }
    
    // Determine the result of the link attempt
    if (link_result.error() == firebase::auth::kAuthErrorNone) {
      // user linked correctly.
    } else if (link_result.error() ==
                   firebase::auth::kAuthErrorCredentialAlreadyInUse &&
               link_result.result()->info.updated_credential.is_valid()) {
      // Sign In with the new credential
      firebase::Future<firebase::auth::User*> result = auth->SignInWithCredential(
          link_result.result()->info.updated_credential);
    } else {
      // Another link error occurred.
    }
    

在安卓上

在 Android 上,通過使用 Firebase SDK 將基於 Web 的通用 OAuth 登錄集成到您的應用程序中,使用 Firebase 對您的用戶進行身份驗證,以執行端到端的登錄流程。

要使用 Firebase SDK 處理登錄流程,請按以下步驟操作:

  1. 構造一個FederatedOAuthProviderData實例,該實例配置了適用於 Apple 的提供程序 ID。

    firebase::auth::FederatedOAuthProviderData provider_data("apple.com");
    
  2. 可選:指定您希望從身份驗證提供程序請求的默認值之外的其他 OAuth 2.0 範圍。

    provider_data.scopes.push_back("email");
    provider_data.scopes.push_back("name");
    
  3. 可選:如果要以英語以外的語言顯示 Apple 的登錄屏幕,請設置locale參數。有關支持的語言環境,請參閱Sign In with Apple 文檔

    // Localize to French.
    provider_data.custom_parameters["language"] = "fr";
    ```
    
  4. 配置提供程序數據後,使用它來創建 FederatedOAuthProvider。

    // Construct a FederatedOAuthProvider for use in Auth methods.
    firebase::auth::FederatedOAuthProvider provider(provider_data);
    
  5. 使用 Auth 提供程序對象向 Firebase 進行身份驗證。請注意,與其他 FirebaseAuth 操作不同,這將通過彈出一個用戶可以在其中輸入其憑據的 Web 視圖來控制您的 UI。

    要啟動登錄流程,請調用signInWithProvider

    firebase::Future<firebase::auth::SignInResult> result =
      auth->SignInWithProvider(provider_data);
    

    然後,您的應用程序可能會在 Future 上等待或註冊回調

  6. ReauthenticateWithProvider可以使用相同的模式,它可用於檢索需要最近登錄的敏感操作的新憑據。

    firebase::Future<firebase::auth::SignInResult> result =
      user->ReauthenticateWithProvider(provider_data);
    

    然後,您的應用程序可能會在 Future 上等待或註冊回調

  7. 而且,您可以使用linkWithCredential()將不同的身份提供者鏈接到現有帳戶。

    請注意,Apple 要求您在將用戶的 Apple 帳戶鏈接到其他數據之前獲得用戶的明確同意。

    例如,要將 Facebook 帳戶鏈接到當前 Firebase 帳戶,請使用從用戶登錄 Facebook 時獲得的訪問令牌:

    // Initialize a Facebook credential with a Facebook access token.
    AuthCredential credential =
        firebase::auth::FacebookAuthProvider.getCredential(token);
    
    // Assuming the current user is an Apple user linking a Facebook provider.
    firebase::Future<firebase::auth::SignInResult> result =
        auth.getCurrentUser().linkWithCredential(credential);
    

使用 Apple Notes 登錄

與 Firebase Auth 支持的其他提供商不同,Apple 不提供照片 URL。

此外,當用戶選擇不與應用程序共享電子郵件時,Apple 會為該用戶提供一個唯一的電子郵件地址(格式xyz@privaterelay.appleid.com ),並與您的應用程序共享。如果您配置了私人電子郵件中繼服務,Apple 會將發送到匿名地址的電子郵件轉發到用戶的真實電子郵件地址。

Apple 僅在用戶首次登錄時與應用共享用戶信息,例如顯示名稱。通常,Firebase 會在用戶首次使用 Apple 登錄時存儲顯示名稱,您可以通過getCurrentUser().getDisplayName()獲取。但是,如果您之前使用 Apple 將用戶登錄到應用程序而不使用 Firebase,Apple 將不會向 Firebase 提供用戶的顯示名稱。

下一步

用戶首次登錄後,會創建一個新用戶帳戶並將其鏈接到憑據(即用戶名和密碼、電話號碼或身份驗證提供商信息),即用戶登錄時使用的憑據。這個新帳戶作為 Firebase 項目的一部分存儲,可用於在項目中的每個應用中識別用戶,無論用戶如何登錄。

在您的應用程序中,您可以從 firebase::auth::user 對象獲取用戶的基本個人資料信息。請參閱管理用戶

在您的 Firebase 實時數據庫和雲存儲安全規則中,您可以從 auth 變量中獲取登錄用戶的唯一用戶 ID,並使用它來控制用戶可以訪問哪些數據。