您可以使用 Firebase SDK 執行端對端 OAuth 2.0 登入流程,讓使用者透過 Apple ID 進行 Firebase 驗證。
事前準備
如要讓使用者透過 Apple 登入,請先在 Apple 開發人員網站上設定「使用 Apple 帳戶登入」,然後在 Firebase 專案中啟用 Apple 做為登入服務供應商。
加入 Apple Developer Program
只有 Apple Developer Program 成員可以設定「使用 Apple 帳戶登入」。
設定「使用 Apple 帳戶登入」功能
- 在 Apple 開發人員網站的「Certificates, Identifiers & Profiles」(憑證、ID 與設定檔) 頁面,為應用程式啟用「使用 Apple 帳戶登入」功能。
- 按照「設定網頁版使用 Apple 登入」第一節的說明,將網站與應用程式建立關聯。系統提示時,請將下列網址註冊為返回網址:
您可以在 Firebase 控制台設定頁面取得 Firebase 專案 ID。 完成後,請記下新的服務 ID,下一節會用到。https://YOUR_FIREBASE_PROJECT_ID.firebaseapp.com/__/auth/handler
- 建立「使用 Apple 帳戶登入」私密金鑰。下一節會用到新的私密金鑰和金鑰 ID。
- 如果您使用 Firebase Authentication 的任何功能向使用者傳送電子郵件,包括電子郵件連結登入、電子郵件地址驗證、帳戶變更撤銷等,請設定 Apple 私人電子郵件轉送服務並註冊
noreply@YOUR_FIREBASE_PROJECT_ID.firebaseapp.com(或自訂電子郵件範本網域),讓 Apple 將 Firebase Authentication 傳送的電子郵件轉送至匿名 Apple 電子郵件地址。
啟用 Apple 做為登入資訊提供者
- 將 Firebase 新增至 Apple 專案。在 Firebase 控制台中設定應用程式時,請務必註冊應用程式的軟體包 ID。
- 在 Firebase 控制台中,依序前往 「Security」(安全性) >「Authentication」(驗證)。
- 在「登入方式」分頁中,啟用「Apple」登入供應商。指定您在前一節建立的服務 ID。此外,在「OAuth code flow configuration」(OAuth 程式碼流程設定) 部分,指定 Apple 團隊 ID,以及您在上一節建立的私密金鑰和金鑰 ID。
遵守 Apple 去識別化資料規定
使用者透過「使用 Apple 登入」功能登入時,可以選擇將資料匿名化,包括電子郵件地址。選擇這個選項的使用者,電子郵件地址會使用 privaterelay.appleid.com 網域。在應用程式中使用「使用 Apple 登入」功能時,您必須遵守 Apple 針對這些匿名 Apple ID 訂定的任何適用開發人員政策或條款。
包括在將任何可直接識別的個人資訊與匿名 Apple ID 建立關聯前,取得任何必要的使用者同意聲明。使用 Firebase 驗證時,可能包括下列動作:
- 將電子郵件地址連結至匿名 Apple ID,或反向操作。
- 將電話號碼連結至匿名 Apple ID,或將匿名 Apple ID 連結至電話號碼
- 將非匿名社群憑證 (Facebook、Google 等) 連結至匿名 Apple ID,反之亦然。
以上僅列出部分示例。請參閱開發人員帳戶「Membership」部分的《Apple Developer Program 授權協議》,確保應用程式符合 Apple 的規定。
使用 Apple 帳戶登入,並透過 Firebase 進行驗證
如要使用 Apple 帳戶進行驗證,請先使用 Apple 的 AuthenticationServices 架構將使用者登入其 Apple 帳戶,然後使用 Apple 回應中的 ID 權杖建立 Firebase AuthCredential 物件:
針對每個登入要求,產生隨機字串 (即「nonce」),用來確保您取得的 ID 權杖是專門針對應用程式的驗證要求而授予。這個步驟對於防範重送攻擊至關重要。
您可以使用
SecRandomCopyBytes(_:_:_)產生加密安全的 Nonce,如以下範例所示: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]; }
您會將 Nonce 的 SHA256 雜湊與登入要求一併傳送,Apple 會在回應中原封不動地傳遞該雜湊。Firebase 會雜湊原始 Nonce,並與 Apple 傳遞的值進行比較,藉此驗證回應。
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; }
啟動 Apple 的登入流程,在要求中加入 Nonce 的 SHA256 雜湊,以及處理 Apple 回應的委派類別 (請參閱下一個步驟):
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]; }在
ASAuthorizationControllerDelegate實作中處理 Apple 的回應。如果登入成功,請使用 Apple 回應中的 ID 權杖和未經雜湊處理的 Nonce,向 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); }
與 Firebase Auth 支援的其他供應商不同,Apple 不提供相片網址。
此外,如果使用者選擇不與應用程式分享電子郵件,Apple 會為該使用者提供一組不重複的電子郵件地址 (格式為 xyz@privaterelay.appleid.com),並與您的應用程式分享。如果您設定了私人電子郵件轉送服務,Apple 會將傳送至匿名地址的電子郵件轉送至使用者的實際電子郵件地址。
重新驗證和帳戶連結
reauthenticateWithCredential() 也可以使用相同的模式,用於擷取需要近期登入的機密作業的新憑證:
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.
// ...
}];
此外,您可以使用 linkWithCredential() 將不同的識別資訊提供者連結至現有帳戶。
請注意,Apple 規定您必須先取得使用者明確同意,才能將他們的 Apple 帳戶連結至其他資料。
「使用 Apple 帳戶登入」不允許重複使用驗證憑證連結現有帳戶。如要將「使用 Apple 帳戶登入」憑證連結至其他帳戶,請先嘗試使用舊的「使用 Apple 帳戶登入」憑證連結帳戶,然後檢查傳回的錯誤,找出新的憑證。新憑證會位於錯誤的 userInfo 字典中,可透過 AuthErrorUserInfoUpdatedCredentialKey 鍵存取。
舉例來說,如要將 Facebook 帳戶連結至目前的 Firebase 帳戶,請使用使用者登入 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.
// ...
}];
撤銷權杖
根據 App Store 審查指南,Apple 規定支援帳戶建立功能的應用程式,必須允許使用者在應用程式內刪除帳戶。
如要滿足這項要求,請按照下列步驟操作:
請務必填寫「使用 Apple 帳戶登入」供應商設定的「Services ID」和「OAuth code flow configuration」部分,詳情請參閱「設定使用 Apple 帳戶登入」一節。
由於 Firebase 不會在使用者透過「使用 Apple 登入」建立帳戶時儲存使用者權杖,因此您必須要求使用者再次登入,才能撤銷權杖並刪除帳戶。
Swift
private var currentNonce: String? 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) } }
從
ASAuthorizationAppleIDCredential取得授權碼,並使用該授權碼呼叫Auth.auth().revokeToken(withAuthorizationCode:),撤銷使用者的權杖。Swift
private var user: User? 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) } } }
最後,刪除使用者帳戶 (和所有相關聯資料)
後續步驟
使用者首次登入後,系統會建立新的使用者帳戶,並連結至使用者登入時使用的憑證,也就是使用者名稱和密碼、電話號碼或驗證提供者資訊。這個新帳戶會儲存為 Firebase 專案的一部分,可用於識別專案中所有應用程式的使用者,無論使用者如何登入。
在 Firebase Realtime Database 和 Cloud Storage安全性規則中,您可以從
auth變數取得已登入使用者的專屬使用者 ID,並用來控管使用者可存取的資料。
您可以將驗證供應商憑證連結至現有使用者帳戶,允許使用者透過多個驗證供應商登入應用程式。
如要將使用者登出,請呼叫 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; }
您也可以為所有驗證錯誤新增錯誤處理程式碼。請參閱「處理錯誤」。