Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ Artem Shevchenko <art.shevchen@gmail.com>
Adem Ocel <moosyroot@gmail.com>
Bilal Aksoy <sloeyeleet@gmail.com>
Zain Qureshi <43629888+ZainnQureshii@users.noreply.github.com>
Kenichi Tatsuhama <kenichi.tatsuhama@gmail.com>
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,6 @@ class FlutterFirebaseAuthPlugin :
override fun signOut(app: AuthPigeonFirebaseApp, callback: (Result<Unit>) -> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -181,7 +180,6 @@ class FlutterFirebaseMultiFactor : MultiFactorUserHostApi, MultiFactoResolverHos
}

companion object {
val multiFactorUserMap: MutableMap<String, MutableMap<String, MultiFactor>> = HashMap()
val multiFactorSessionMap: MutableMap<String, MultiFactorSession> = HashMap()
val multiFactorResolverMap: MutableMap<String, MultiFactorResolver> = HashMap()
val multiFactorAssertionMap: MutableMap<String, MultiFactorAssertion> = HashMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FirebaseAuthException>().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', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,34 @@ Future<void> emulatorClearAllUsers() async {

/// Disable a specific user by uid.
Future<void> 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<void> 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<void> emulatorSetPhoneMultiFactors(
String uid,
List<String> phoneNumbers,
) async {
await _emulatorUpdateAccount({
'localId': uid,
'mfa': {
'enrollments': [
for (final (index, phoneNumber) in phoneNumbers.indexed)
{'mfaEnrollmentId': 'phone-$index', 'phoneInfo': phoneNumber},
],
},
});
}

Future<void> _emulatorUpdateAccount(Map<String, Object> fields) async {
String body = jsonEncode(fields);
final response = await http.post(
Uri.parse(
'http://$testEmulatorHost:$testEmulatorPort/identitytoolkit.googleapis.com/v1/accounts:update',
),
Expand All @@ -73,6 +99,11 @@ Future<void> 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
Expand Down
Loading