@@ -55,28 +55,45 @@ class MasterPasswordAppUnlockMethod extends AppUnlockMethod<String> {
5555 }
5656
5757 @override
58- Future <CannotUnlockException ?> canUnlock () async {
59- Salt ? salt = await Salt .readFromLocalStorage ();
60- if (salt == null ) {
61- try {
62- List <Totp > totps = await _ref.read (totpRepositoryProvider.future);
63- salt = totps.firstOrNull? .encryptedData.encryptionSalt;
64- if (salt == null ) {
65- return MasterPasswordNoSalt ();
66- }
67- await salt.saveToLocalStorage ();
68- } catch (ex, stackTrace) {
69- handleException (ex, stackTrace);
70- return MasterPasswordNoSalt ();
58+ Future <CannotUnlockException ?> canUnlock (UnlockReason reason) async {
59+ if (reason != .openApp && reason != .sensibleAction) {
60+ List <Totp > totps = await _ref.read (totpRepositoryProvider.future);
61+ if (totps.isEmpty) {
62+ return null ;
7163 }
7264 }
65+ if (! (await _ensureSaltAvailable ())) {
66+ return MasterPasswordNoSalt ();
67+ }
7368 List <PasswordVerificationMethod > passwordVerificationMethods = await _ref.read (passwordVerificationProvider.future);
7469 if (passwordVerificationMethods.isEmpty) {
7570 return MasterPasswordNoPasswordVerificationMethodAvailable ();
7671 }
7772 return null ;
7873 }
7974
75+ /// Ensures that the local salt exists, restoring it from the TOTP list when possible.
76+ Future <bool > _ensureSaltAvailable () async {
77+ if (await Salt .readFromLocalStorage () != null ) {
78+ return true ;
79+ }
80+ try {
81+ List <Totp > totps = await _ref.read (totpRepositoryProvider.future);
82+ Salt ? salt = totps.firstOrNull? .encryptedData.encryptionSalt;
83+ if (salt == null ) {
84+ return false ;
85+ }
86+ await salt.saveToLocalStorage ();
87+ _ref.invalidate (cryptoStoreProvider);
88+ _ref.invalidate (cryptoStoreVerificationMethodProvider);
89+ _ref.invalidate (passwordVerificationProvider);
90+ return true ;
91+ } catch (ex, stackTrace) {
92+ handleException (ex, stackTrace);
93+ return false ;
94+ }
95+ }
96+
8097 /// Prompts master password for unlock.
8198 Future <Result <String >> _promptMasterPasswordForUnlock (BuildContext context, String ? message) async {
8299 String ? password = await MasterPasswordInputDialog .prompt (
0 commit comments