Skip to content

Commit

Permalink
Desktop: Improved master password state handling in Encryption screen
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Oct 17, 2021
1 parent 8d09ed3 commit 7d62df8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { _ } from '@joplin/lib/locale';
import time from '@joplin/lib/time';
import shim from '@joplin/lib/shim';
import dialogs from '../dialogs';
import { decryptedStatText, dontReencryptData, enableEncryptionConfirmationMessages, onSavePasswordClick, onToggleEnabledClick, reencryptData, upgradeMasterKey, useInputPasswords, usePasswordChecker, useStats, useToggleShowDisabledMasterKeys } from '@joplin/lib/components/EncryptionConfigScreen/utils';
import { decryptedStatText, dontReencryptData, enableEncryptionConfirmationMessages, onSavePasswordClick, onToggleEnabledClick, reencryptData, upgradeMasterKey, useInputPasswords, useNeedMasterPassword, usePasswordChecker, useStats, useToggleShowDisabledMasterKeys } from '@joplin/lib/components/EncryptionConfigScreen/utils';
import { MasterKeyEntity } from '@joplin/lib/services/e2ee/types';
import { getEncryptionEnabled, masterKeyEnabled, SyncInfo } from '@joplin/lib/services/synchronizer/syncInfoUtils';
import { getDefaultMasterKey, getMasterPasswordStatusMessage, masterPasswordIsValid, toggleAndSetupEncryption } from '@joplin/lib/services/e2ee/utils';
Expand Down Expand Up @@ -39,6 +39,7 @@ const EncryptionConfigScreen = (props: Props) => {
const stats = useStats();
const { passwordChecks, masterPasswordKeys, masterPasswordStatus } = usePasswordChecker(props.masterKeys, props.activeMasterKeyId, props.masterPassword, props.passwords);
const { showDisabledMasterKeys, toggleShowDisabledMasterKeys } = useToggleShowDisabledMasterKeys();
const needMasterPassword = useNeedMasterPassword(passwordChecks, props.masterKeys);

const onUpgradeMasterKey = useCallback((mk: MasterKeyEntity) => {
void upgradeMasterKey(mk, passwordChecks, props.passwords);
Expand Down Expand Up @@ -263,9 +264,8 @@ const EncryptionConfigScreen = (props: Props) => {
};

const buttonTitle = CommandService.instance().label('openMasterPasswordDialog');
const needPassword = Object.values(passwordChecks).includes(false);

const needPasswordMessage = !needPassword ? null : (
const needPasswordMessage = !needMasterPassword ? null : (
<p className="needpassword">{_('Your master password is needed to decrypt some of your data.')}<br/>{_('Please click on "%s" to proceed', buttonTitle)}</p>
);

Expand All @@ -275,7 +275,7 @@ const EncryptionConfigScreen = (props: Props) => {
<h2>{_('Master password')}</h2>
<p className="status"><span>{_('Master password:')}</span>&nbsp;<span className="bold">{getMasterPasswordStatusMessage(masterPasswordStatus)}</span></p>
{needPasswordMessage}
<Button className="managebutton" level={needPassword ? ButtonLevel.Primary : ButtonLevel.Secondary} onClick={onManageMasterPassword} title={buttonTitle} />
<Button className="managebutton" level={needMasterPassword ? ButtonLevel.Primary : ButtonLevel.Secondary} onClick={onManageMasterPassword} title={buttonTitle} />
</div>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions packages/app-desktop/gui/MasterPasswordDialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export default function(props: Props) {

const renderResetMasterPasswordLink = () => {
if (mode === Mode.Reset) return null;
if (status === MasterPasswordStatus.Valid) return null;
return <p><a href="#" onClick={onToggleMode}>Reset master password</a></p>;
};

Expand Down
10 changes: 10 additions & 0 deletions packages/lib/components/EncryptionConfigScreen/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ export const usePasswordChecker = (masterKeys: MasterKeyEntity[], activeMasterKe
return { passwordChecks, masterPasswordKeys, masterPasswordStatus };
};

export const useNeedMasterPassword = (passwordChecks: PasswordChecks, masterKeys: MasterKeyEntity[]) => {
for (const [mkId, valid] of Object.entries(passwordChecks)) {
const mk = masterKeys.find(mk => mk.id === mkId);
if (!mk) continue;
if (!masterKeyEnabled(mk)) continue;
if (!valid) return true;
}
return false;
};

export const upgradeMasterKey = async (masterKey: MasterKeyEntity, passwordChecks: PasswordChecks, passwords: Record<string, string>): Promise<string> => {
const passwordCheck = passwordChecks[masterKey.id];
if (!passwordCheck) {
Expand Down

0 comments on commit 7d62df8

Please sign in to comment.