Fix duplicate entry for same code during offline to online transition (#229)
This commit is contained in:
commit
50ebcdd1f0
3 changed files with 23 additions and 15 deletions
|
@ -161,11 +161,11 @@ class AuthenticatorService {
|
|||
}
|
||||
}
|
||||
|
||||
Future<void> onlineSync() async {
|
||||
Future<bool> onlineSync() async {
|
||||
try {
|
||||
if(getAccountMode().isOffline) {
|
||||
debugPrint("Skipping sync since account mode is offline");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
_logger.info("Sync");
|
||||
await _remoteToLocalSync();
|
||||
|
@ -173,6 +173,7 @@ class AuthenticatorService {
|
|||
await _localToRemoteSync();
|
||||
_logger.info("local push completed");
|
||||
Bus.instance.fire(CodesUpdatedEvent());
|
||||
return true;
|
||||
} on UnauthorizedError {
|
||||
if ((await _db.removeSyncedData()) > 0) {
|
||||
Bus.instance.fire(CodesUpdatedEvent());
|
||||
|
@ -180,8 +181,10 @@ class AuthenticatorService {
|
|||
debugPrint("Firing logout event");
|
||||
|
||||
Bus.instance.fire(TriggerLogoutEvent());
|
||||
return false;
|
||||
} catch (e) {
|
||||
_logger.severe("Failed to sync with remote", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import 'package:ente_auth/models/authenticator/entity_result.dart';
|
|||
import 'package:ente_auth/models/code.dart';
|
||||
import 'package:ente_auth/services/authenticator_service.dart';
|
||||
import 'package:ente_auth/store/offline_authenticator_db.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
class CodeStore {
|
||||
|
@ -90,29 +91,33 @@ class CodeStore {
|
|||
Future<void> importOfflineCodes() async {
|
||||
try {
|
||||
Configuration config = Configuration.instance;
|
||||
// Account isn't configured yet, so we can't import offline codes
|
||||
if (!config.hasConfiguredAccount()) {
|
||||
return;
|
||||
}
|
||||
// Never opted for offline mode, so we can't import offline codes
|
||||
if (!config.hasOptedForOfflineMode()) {
|
||||
return;
|
||||
}
|
||||
Uint8List? hasOfflineKey = config.getOfflineSecretKey();
|
||||
if (hasOfflineKey == null) {
|
||||
// No offline key, so we can't import offline codes
|
||||
if (!config.hasConfiguredAccount() ||
|
||||
!config.hasOptedForOfflineMode() ||
|
||||
config.getOfflineSecretKey() == null) {
|
||||
return;
|
||||
}
|
||||
_logger.info('starting offline imports');
|
||||
|
||||
List<Code> offlineCodes =
|
||||
await CodeStore.instance.getAllCodes(accountMode: AccountMode.offline);
|
||||
if(offlineCodes.isEmpty) {
|
||||
return;
|
||||
}
|
||||
bool isOnlineSyncDone = await AuthenticatorService.instance.onlineSync();
|
||||
if(!isOnlineSyncDone) {
|
||||
debugPrint("Skipping offline import since online sync failed");
|
||||
return;
|
||||
}
|
||||
for (Code eachCode in offlineCodes) {
|
||||
await CodeStore.instance.addCode(
|
||||
eachCode,
|
||||
accountMode: AccountMode.online,
|
||||
shouldSync: false,
|
||||
);
|
||||
await OfflineAuthenticatorDB.instance.deleteByIDs(
|
||||
generatedIDs: [eachCode.generatedID!],
|
||||
);
|
||||
}
|
||||
OfflineAuthenticatorDB.instance.clearTable();
|
||||
AuthenticatorService.instance.onlineSync().ignore();
|
||||
} catch (e, s) {
|
||||
_logger.severe("error while importing offline codes", e, s);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name: ente_auth
|
||||
description: ente two-factor authenticator
|
||||
version: 1.0.56+56
|
||||
version: 1.0.57+57
|
||||
publish_to: none
|
||||
|
||||
environment:
|
||||
|
|
Loading…
Add table
Reference in a new issue