diff --git a/AUTHORS b/AUTHORS index da382e6f0055..15e2159471a1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -74,3 +74,4 @@ Artem Shevchenko Adem Ocel Bilal Aksoy Zain Qureshi <43629888+ZainnQureshii@users.noreply.github.com> +Kenichi Tatsuhama diff --git a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt index a59e485660de..dcc0b7435721 100644 --- a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt +++ b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt @@ -273,10 +273,6 @@ class FlutterFirebaseAuthPlugin : override fun signOut(app: AuthPigeonFirebaseApp, callback: (Result) -> Unit) { try { val firebaseAuth = getAuthFromPigeon(app) - if (firebaseAuth.currentUser != null) { - FlutterFirebaseMultiFactor.multiFactorUserMap[app.appName]?.remove( - firebaseAuth.currentUser!!.uid) - } firebaseAuth.signOut() callback(Result.success(Unit)) } catch (e: Exception) { diff --git a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt index da12c0ad6c17..39059b594441 100644 --- a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt +++ b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt @@ -20,8 +20,7 @@ class FlutterFirebaseMultiFactor : MultiFactorUserHostApi, MultiFactoResolverHos val currentUser = FlutterFirebaseAuthUser.getCurrentUserFromPigeon(app) ?: throw FirebaseNoSignedInUserException("No user is signed in") - val appMultiFactorUser = multiFactorUserMap.getOrPut(app.appName) { HashMap() } - return appMultiFactorUser.getOrPut(currentUser.uid) { currentUser.multiFactor } + return currentUser.multiFactor } override fun enrollPhone( @@ -181,7 +180,6 @@ class FlutterFirebaseMultiFactor : MultiFactorUserHostApi, MultiFactoResolverHos } companion object { - val multiFactorUserMap: MutableMap> = HashMap() val multiFactorSessionMap: MutableMap = HashMap() val multiFactorResolverMap: MutableMap = HashMap() val multiFactorAssertionMap: MutableMap = HashMap() diff --git a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_multi_factor_e2e_test.dart b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_multi_factor_e2e_test.dart index c61f0aedc0a7..e1c27b5487bb 100644 --- a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_multi_factor_e2e_test.dart +++ b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_multi_factor_e2e_test.dart @@ -40,6 +40,56 @@ void main() { // Assertions expect((await multiFactor.getEnrolledFactors()).length, 0); }); + + test('should not return stale factors after the session ends without ' + 'signOut and the same user signs in again', () async { + final email = generateRandomEmail(); + final user = + (await FirebaseAuth.instance.createUserWithEmailAndPassword( + email: email, + password: testPassword, + )).user!; + + await emulatorSetPhoneMultiFactors(user.uid, ['+441444555777']); + await user.reload(); + expect( + (await FirebaseAuth.instance.currentUser!.multiFactor + .getEnrolledFactors()) + .length, + 1, + ); + + // The native SDK signs out by itself when the session is no longer + // valid, so FirebaseAuth.signOut() is never called. + await emulatorDisableUser(user.uid); + await expectLater( + user.reload(), + throwsA( + isA().having( + (e) => e.code, + 'code', + 'user-disabled', + ), + ), + ); + expect(FirebaseAuth.instance.currentUser, isNull); + + // The factor is removed elsewhere, then the same user signs in again. + await emulatorEnableUser(user.uid); + await emulatorSetPhoneMultiFactors(user.uid, []); + await FirebaseAuth.instance.signInWithEmailAndPassword( + email: email, + password: testPassword, + ); + + // Assertions + expect( + (await FirebaseAuth.instance.currentUser!.multiFactor + .getEnrolledFactors()) + .length, + 0, + ); + }); }); group('session', () { diff --git a/packages/firebase_auth/firebase_auth/example/integration_test/test_utils.dart b/packages/firebase_auth/firebase_auth/example/integration_test/test_utils.dart index f71af7bfa26b..fb788c43bf1f 100644 --- a/packages/firebase_auth/firebase_auth/example/integration_test/test_utils.dart +++ b/packages/firebase_auth/firebase_auth/example/integration_test/test_utils.dart @@ -61,8 +61,34 @@ Future emulatorClearAllUsers() async { /// Disable a specific user by uid. Future emulatorDisableUser(String uid) async { - String body = jsonEncode({'disableUser': true, 'localId': uid}); - await http.post( + await _emulatorUpdateAccount({'disableUser': true, 'localId': uid}); +} + +/// Enable a specific user by uid. +Future emulatorEnableUser(String uid) async { + await _emulatorUpdateAccount({'disableUser': false, 'localId': uid}); +} + +/// Replace the phone second factors of a specific user by uid on the server, +/// as an administrator would. An empty list removes all of them. +Future emulatorSetPhoneMultiFactors( + String uid, + List phoneNumbers, +) async { + await _emulatorUpdateAccount({ + 'localId': uid, + 'mfa': { + 'enrollments': [ + for (final (index, phoneNumber) in phoneNumbers.indexed) + {'mfaEnrollmentId': 'phone-$index', 'phoneInfo': phoneNumber}, + ], + }, + }); +} + +Future _emulatorUpdateAccount(Map fields) async { + String body = jsonEncode(fields); + final response = await http.post( Uri.parse( 'http://$testEmulatorHost:$testEmulatorPort/identitytoolkit.googleapis.com/v1/accounts:update', ), @@ -73,6 +99,11 @@ Future emulatorDisableUser(String uid) async { }, body: body, ); + if (response.statusCode != 200) { + throw Exception( + 'accounts:update failed: ${response.statusCode} ${response.body}', + ); + } } /// Retrieve a sms phone authentication code that may have been sent for a specific