您可以使用 Firebase SDK 執行端對端 OAuth 2.0 登入流程,讓使用者透過 Apple ID 進行 Firebase 驗證。
事前準備
如要讓使用者透過 Apple 登入,請先在 Apple 開發人員網站上設定「使用 Apple 帳戶登入」,然後在 Firebase 專案中啟用 Apple 做為登入服務供應商。
加入 Apple Developer Program
只有 Apple Developer Program 成員可以設定「使用 Apple 帳戶登入」。
設定「使用 Apple 帳戶登入」
在 Apple Developer 網站上,執行下列操作:
-
按照「設定網頁版使用 Apple 帳戶登入」第一節的說明,將網站與應用程式建立關聯。在系統提示時,將下列網址註冊為返回網址:
https://YOUR_FIREBASE_PROJECT_ID.firebaseapp.com/__/auth/handler
如要查看 Firebase 專案 ID,請前往 Firebase 控制台,依序點選
「設定」 > 「一般分頁標籤」 。完成後,請記下新的服務 ID,下一節會用到。
- 建立「使用 Apple 帳戶登入」私密金鑰。在下一節中,您會需要新的私密金鑰和金鑰 ID。
-
如果您使用 Firebase Authentication 的任何功能向使用者傳送電子郵件,包括電子郵件連結登入、電子郵件地址驗證、帳戶變更撤銷等,請設定 Apple 私人電子郵件轉送服務並註冊
noreply@YOUR_FIREBASE_PROJECT_ID.firebaseapp.com(或自訂電子郵件範本網域),讓 Apple 將 Firebase Authentication 傳送的電子郵件轉送至匿名 Apple 電子郵件地址。
啟用 Apple 做為登入資訊提供者
- 將 Firebase 新增至 Android 專案。
-
指定應用程式的 SHA-1 指紋 (如果尚未指定)。
-
在 Firebase 控制台中,依序前往「
Settings 」(設定) >「General tab」(一般分頁)。 - 向下捲動至「您的應用程式」資訊卡,選取 Android 應用程式,然後在「SHA 憑證指紋」欄位中新增 SHA-1 指紋。
-
在 Firebase 控制台中,依序前往「
- 在 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 的規定。
使用 Firebase SDK 處理登入流程
在 Android 上,如要使用 Apple 帳戶透過 Firebase 驗證使用者,最簡單的方法是使用 Firebase Android SDK 處理整個登入流程。
如要使用 Firebase Android SDK 處理登入流程,請按照下列步驟操作:
使用建構工具和供應商 ID
apple.com建構OAuthProvider的例項:Kotlin
val provider = OAuthProvider.newBuilder("apple.com")Java
OAuthProvider.Builder provider = OAuthProvider.newBuilder("apple.com");選用:指定要向驗證提供者要求的其他 OAuth 2.0 範圍 (預設範圍以外)。
Kotlin
provider.setScopes(arrayOf("email", "name"))Java
List<String> scopes = new ArrayList<String>() { { add("email"); add("name"); } }; provider.setScopes(scopes);根據預設,啟用「每個電子郵件地址只能建立一個帳戶」時,Firebase 會要求電子郵件和名稱範圍。如果將這項設定變更為「Multiple accounts per email address」(每個電子郵件地址有多個帳戶),除非您指定範圍,否則 Firebase 不會向 Apple 要求任何範圍。
選用:如要以英文以外的語言顯示 Apple 登入畫面,請設定
locale參數。如要瞭解支援的語言代碼,請參閱「使用 Apple 帳戶登入」說明文件。Kotlin
// Localize the Apple authentication screen in French. provider.addCustomParameter("locale", "fr")Java
// Localize the Apple authentication screen in French. provider.addCustomParameter("locale", "fr");使用 OAuth 供應商物件向 Firebase 驗證。請注意,與其他
FirebaseAuth作業不同,這項作業會開啟 Chrome 自訂分頁,接管使用者介面。因此,請勿在附加的OnSuccessListener和OnFailureListener中參照 Activity,因為作業啟動 UI 時,這些物件會立即卸除。請先確認是否已收到回覆。使用這個方法登入會將 Activity 移至背景,這表示系統可能會在登入流程中回收 Activity。為確保在這種情況下,您不會讓使用者再次嘗試,請檢查是否已有結果。
如要檢查是否有待處理的結果,請呼叫
getPendingAuthResult():Kotlin
val pending = auth.pendingAuthResult if (pending != null) { pending.addOnSuccessListener { authResult -> Log.d(TAG, "checkPending:onSuccess:$authResult") // Get the user profile with authResult.getUser() and // authResult.getAdditionalUserInfo(), and the ID // token from Apple with authResult.getCredential(). }.addOnFailureListener { e -> Log.w(TAG, "checkPending:onFailure", e) } } else { Log.d(TAG, "pending: null") }Java
mAuth = FirebaseAuth.getInstance(); Task<AuthResult> pending = mAuth.getPendingAuthResult(); if (pending != null) { pending.addOnSuccessListener(new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult authResult) { Log.d(TAG, "checkPending:onSuccess:" + authResult); // Get the user profile with authResult.getUser() and // authResult.getAdditionalUserInfo(), and the ID // token from Apple with authResult.getCredential(). } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "checkPending:onFailure", e); } }); } else { Log.d(TAG, "pending: null"); }如果沒有待處理結果,請呼叫
startActivityForSignInWithProvider()啟動登入流程:Kotlin
auth.startActivityForSignInWithProvider(this, provider.build()) .addOnSuccessListener { authResult -> // Sign-in successful! Log.d(TAG, "activitySignIn:onSuccess:${authResult.user}") val user = authResult.user // ... } .addOnFailureListener { e -> Log.w(TAG, "activitySignIn:onFailure", e) }Java
mAuth.startActivityForSignInWithProvider(this, provider.build()) .addOnSuccessListener( new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult authResult) { // Sign-in successful! Log.d(TAG, "activitySignIn:onSuccess:" + authResult.getUser()); FirebaseUser user = authResult.getUser(); // ... } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "activitySignIn:onFailure", e); } });與 Firebase Auth 支援的其他供應商不同,Apple 不提供相片網址。
此外,如果使用者選擇不與應用程式分享電子郵件,Apple 會為該使用者提供一組不重複的電子郵件地址 (格式為
xyz@privaterelay.appleid.com),並與您的應用程式分享。如果您設定了私人電子郵件轉發服務,Apple 會將傳送至匿名地址的電子郵件轉發至使用者的真實電子郵件地址。Apple 只會在使用者首次登入時,與應用程式分享顯示名稱等使用者資訊。通常,Firebase 會在使用者首次使用 Apple 登入時儲存顯示名稱,您可以使用
getCurrentUser().getDisplayName()取得該名稱。不過,如果您先前曾使用 Apple 讓使用者登入應用程式,但未使用 Firebase,Apple 就不會向 Firebase 提供使用者的顯示名稱。
重新驗證和帳戶連結
startActivityForReauthenticateWithProvider() 也可以使用相同的模式,用於擷取新的憑證,以便執行需要近期登入的機密作業:
Kotlin
// The user is already signed-in.
val firebaseUser = auth.getCurrentUser()
firebaseUser
.startActivityForReauthenticateWithProvider(/* activity= */ this, provider.build())
.addOnSuccessListener( authResult -> {
// User is re-authenticated with fresh tokens and
// should be able to perform sensitive operations
// like account deletion and email or password
// update.
})
.addOnFailureListener( e -> {
// Handle failure.
})
Java
// The user is already signed-in.
FirebaseUser firebaseUser = mAuth.getCurrentUser();
firebaseUser
.startActivityForReauthenticateWithProvider(/* activity= */ this, provider.build())
.addOnSuccessListener(
new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// User is re-authenticated with fresh tokens and
// should be able to perform sensitive operations
// like account deletion and email or password
// update.
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Handle failure.
}
});
此外,您可以使用 linkWithCredential() 將不同的識別資訊提供者連結至現有帳戶。
請注意,Apple 規定您必須先取得使用者明確同意,才能將他們的 Apple 帳戶連結至其他資料。
舉例來說,如要將 Facebook 帳戶連結至目前的 Firebase 帳戶,請使用使用者登入 Facebook 時取得的存取權杖:
Kotlin
// Initialize a Facebook credential with a Facebook access token.
val credential = FacebookAuthProvider.getCredential(token.getToken())
// Assuming the current user is an Apple user linking a Facebook provider.
mAuth.getCurrentUser().linkWithCredential(credential)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
// Facebook credential is linked to the current Apple user.
// The user can now sign in to the same account
// with either Apple or Facebook.
}
});
Java
// Initialize a Facebook credential with a Facebook access token.
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
// Assuming the current user is an Apple user linking a Facebook provider.
mAuth.getCurrentUser().linkWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Facebook credential is linked to the current Apple user.
// The user can now sign in to the same account
// with either Apple or Facebook.
}
}
});
進階:手動處理登入流程
您也可以使用 Apple 帳戶向 Firebase 驗證,方法是使用 Apple 登入 JS SDK、手動建構 OAuth 流程,或使用 AppAuth 等 OAuth 程式庫,處理登入流程。
針對每個登入要求,產生隨機字串 (即「nonce」),用來確保您取得的 ID 權杖是專門針對應用程式的驗證要求而授予。這個步驟對於防範重送攻擊至關重要。
您可以使用
SecureRandom在 Android 上產生加密安全的 Nonce,如以下範例所示:Kotlin
private fun generateNonce(length: Int): String { val generator = SecureRandom() val charsetDecoder = StandardCharsets.US_ASCII.newDecoder() charsetDecoder.onUnmappableCharacter(CodingErrorAction.IGNORE) charsetDecoder.onMalformedInput(CodingErrorAction.IGNORE) val bytes = ByteArray(length) val inBuffer = ByteBuffer.wrap(bytes) val outBuffer = CharBuffer.allocate(length) while (outBuffer.hasRemaining()) { generator.nextBytes(bytes) inBuffer.rewind() charsetDecoder.reset() charsetDecoder.decode(inBuffer, outBuffer, false) } outBuffer.flip() return outBuffer.toString() }Java
private String generateNonce(int length) { SecureRandom generator = new SecureRandom(); CharsetDecoder charsetDecoder = StandardCharsets.US_ASCII.newDecoder(); charsetDecoder.onUnmappableCharacter(CodingErrorAction.IGNORE); charsetDecoder.onMalformedInput(CodingErrorAction.IGNORE); byte[] bytes = new byte[length]; ByteBuffer inBuffer = ByteBuffer.wrap(bytes); CharBuffer outBuffer = CharBuffer.allocate(length); while (outBuffer.hasRemaining()) { generator.nextBytes(bytes); inBuffer.rewind(); charsetDecoder.reset(); charsetDecoder.decode(inBuffer, outBuffer, false); } outBuffer.flip(); return outBuffer.toString(); }接著,以十六進位字串的形式取得 Nonce 的 SHA246 雜湊:
Kotlin
private fun sha256(s: String): String { val md = MessageDigest.getInstance("SHA-256") val digest = md.digest(s.toByteArray()) val hash = StringBuilder() for (c in digest) { hash.append(String.format("%02x", c)) } return hash.toString() }Java
private String sha256(String s) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[] digest = md.digest(s.getBytes()); StringBuilder hash = new StringBuilder(); for (byte c: digest) { hash.append(String.format("%02x", c)); } return hash.toString(); }您會將 Nonce 的 SHA256 雜湊與登入要求一併傳送,Apple 會在回應中原封不動地傳遞該雜湊。Firebase 會雜湊原始 Nonce,並與 Apple 傳遞的值進行比較,藉此驗證回應。
使用 OAuth 程式庫或其他方法,啟動 Apple 的登入流程。請務必在要求中加入雜湊 Nonce 做為參數。
收到 Apple 的回應後,請從回應中取得 ID 權杖,並使用該權杖和未經雜湊處理的 Nonce 建立
AuthCredential:Kotlin
val credential = OAuthProvider.newCredentialBuilder("apple.com") .setIdTokenWithRawNonce(appleIdToken, rawUnhashedNonce) .build()Java
AuthCredential credential = OAuthProvider.newCredentialBuilder("apple.com") .setIdTokenWithRawNonce(appleIdToken, rawUnhashedNonce) .build();使用 Firebase 憑證向 Firebase 進行驗證:
Kotlin
auth.signInWithCredential(credential) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // User successfully signed in with Apple ID token. // ... } }Java
mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // User successfully signed in with Apple ID token. // ... } } });
如果對 signInWithCredential 的呼叫成功,您可以使用 getCurrentUser 方法取得使用者的帳戶資料。
撤銷權杖
根據 App Store 審查指南,Apple 規定支援帳戶建立功能的應用程式,必須允許使用者在應用程式內刪除帳戶。
此外,支援「使用 Apple 帳戶登入」的應用程式,應使用「使用 Apple 帳戶登入」REST API 撤銷使用者權杖。
如要滿足這項要求,請按照下列步驟操作:
使用
startActivityForSignInWithProvider()方法透過 Apple 登入,並取得AuthResult。取得 Apple 供應商的存取權杖。
Kotlin
val oauthCredential: OAuthCredential = authResult.credential val accessToken = oauthCredential.accessTokenJava
OAuthCredential oauthCredential = (OAuthCredential) authResult.getCredential(); String accessToken = oauthCredential.getAccessToken();使用
revokeAccessTokenAPI 撤銷權杖。Kotlin
mAuth.revokeAccessToken(accessToken) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Access token successfully revoked // for the user ... } }Java
mAuth.revokeAccessToken(accessToken) .addOnCompleteListener(this, new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { // Access token successfully revoked // for the user ... } } });
- 最後,刪除使用者帳戶 (和所有相關資料)
後續步驟
使用者首次登入後,系統會建立新的使用者帳戶,並連結至使用者登入時使用的憑證,也就是使用者名稱和密碼、電話號碼或驗證提供者資訊。這個新帳戶會儲存為 Firebase 專案的一部分,可用於識別專案中所有應用程式的使用者,無論使用者如何登入。
-
在應用程式中,您可以從
FirebaseUser物件取得使用者的基本個人資料。請參閱「管理使用者」。 在 Firebase Realtime Database 和 Cloud Storage安全性規則中,您可以從
auth變數取得已登入使用者的專屬使用者 ID,並用來控管使用者可存取的資料。
您可以將驗證供應商憑證連結至現有使用者帳戶,允許使用者透過多個驗證供應商登入應用程式。
如要登出使用者,請呼叫
signOut:Kotlin
Firebase.auth.signOut()
Java
FirebaseAuth.getInstance().signOut();
-