* feat: hide note contents if the protection expires when the protected note is open and wasn't edited for a while * feat: handle session expiration for opened protected note for both plain advanced editors * fix: if after canceling session expiry modal only one unprotected note stays selected, show its contents in the editor * refactor: handle session expiration for opened protected note (move the logic to web client) * feat: handle the case of selecting "Don't remember" option in session expiry dialog * test (WIP): add unit tests for protecting opened note after the session has expired * test: add remaining unit tests * refactor: move the opened note protection logic to "editor_view" * refactor: reviewer comments - don't rely on user signed-in/out status to require authentication for protected note - remove unnecessary async/awaits - better wording on ui * refactor: reviewer's comments: - use snjs method to check if "Don't remember" option is selected in authentication modal - move the constant to snjs - fix eslint error * refactor: avoid `any` type for `appEvent` payload * test: add unit tests * chore: update function name * refactor: use simpler protection session event types * refactor: protected access terminology * refactor: start counting idle timer after every edit (instead of counting by interval in spite of edits) * test: unit tests * style: don't give extra brightness to the "View Note"/"Authenticate" button on hover/focus * chore: bump snjs version * chore: put snjs "beta" version * fix: run protection timeout when the note is marked as protected * chore: snjs version bump * refactor: immediately lock the note if it's marked as "Protected" * refactor: rename component, directive and some props * refactor: remove extra check * refactor: rename the method * chore: update snjs version Co-authored-by: Mo Bitar <me@bitar.io>
119 lines
3.4 KiB
TypeScript
119 lines
3.4 KiB
TypeScript
import { WebApplication } from '@/ui_models/application';
|
|
import { FunctionalComponent } from 'preact';
|
|
import { useCallback, useState } from 'preact/hooks';
|
|
import { useEffect } from 'preact/hooks';
|
|
import { ApplicationEvent } from '@standardnotes/snjs';
|
|
import { isSameDay } from '@/utils';
|
|
import {
|
|
PreferencesGroup,
|
|
PreferencesSegment,
|
|
Title,
|
|
Text,
|
|
} from '@/preferences/components';
|
|
import { Button } from '@/components/Button';
|
|
|
|
type Props = {
|
|
application: WebApplication;
|
|
};
|
|
|
|
export const Protections: FunctionalComponent<Props> = ({ application }) => {
|
|
const enableProtections = () => {
|
|
application.clearProtectionSession();
|
|
};
|
|
|
|
const [hasProtections, setHasProtections] = useState(() =>
|
|
application.hasProtectionSources()
|
|
);
|
|
|
|
const getProtectionsDisabledUntil = useCallback((): string | null => {
|
|
const protectionExpiry = application.getProtectionSessionExpiryDate();
|
|
const now = new Date();
|
|
if (protectionExpiry > now) {
|
|
let f: Intl.DateTimeFormat;
|
|
if (isSameDay(protectionExpiry, now)) {
|
|
f = new Intl.DateTimeFormat(undefined, {
|
|
hour: 'numeric',
|
|
minute: 'numeric',
|
|
});
|
|
} else {
|
|
f = new Intl.DateTimeFormat(undefined, {
|
|
weekday: 'long',
|
|
day: 'numeric',
|
|
month: 'short',
|
|
hour: 'numeric',
|
|
minute: 'numeric',
|
|
});
|
|
}
|
|
|
|
return f.format(protectionExpiry);
|
|
}
|
|
return null;
|
|
}, [application]);
|
|
|
|
const [protectionsDisabledUntil, setProtectionsDisabledUntil] = useState(
|
|
getProtectionsDisabledUntil()
|
|
);
|
|
|
|
useEffect(() => {
|
|
const removeUnprotectedSessionBeginObserver = application.addEventObserver(
|
|
async () => {
|
|
setProtectionsDisabledUntil(getProtectionsDisabledUntil());
|
|
},
|
|
ApplicationEvent.UnprotectedSessionBegan
|
|
);
|
|
|
|
const removeUnprotectedSessionEndObserver = application.addEventObserver(
|
|
async () => {
|
|
setProtectionsDisabledUntil(getProtectionsDisabledUntil());
|
|
},
|
|
ApplicationEvent.UnprotectedSessionExpired
|
|
);
|
|
|
|
const removeKeyStatusChangedObserver = application.addEventObserver(
|
|
async () => {
|
|
setHasProtections(application.hasProtectionSources());
|
|
},
|
|
ApplicationEvent.KeyStatusChanged
|
|
);
|
|
|
|
return () => {
|
|
removeUnprotectedSessionBeginObserver();
|
|
removeUnprotectedSessionEndObserver();
|
|
removeKeyStatusChangedObserver();
|
|
};
|
|
}, [application, getProtectionsDisabledUntil]);
|
|
|
|
if (!hasProtections) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<PreferencesGroup>
|
|
<PreferencesSegment>
|
|
<Title>Protections</Title>
|
|
{protectionsDisabledUntil ? (
|
|
<Text className="info">
|
|
Unprotected access expires at {protectionsDisabledUntil}.
|
|
</Text>
|
|
) : (
|
|
<Text className="info">Protections are enabled.</Text>
|
|
)}
|
|
<Text className="mt-2">
|
|
Actions like viewing or searching protected notes, exporting decrypted
|
|
backups, or revoking an active session require additional
|
|
authentication such as entering your account password or application
|
|
passcode.
|
|
</Text>
|
|
{protectionsDisabledUntil && (
|
|
<Button
|
|
className="mt-3"
|
|
type="primary"
|
|
label="End Unprotected Access"
|
|
onClick={enableProtections}
|
|
/>
|
|
)}
|
|
</PreferencesSegment>
|
|
</PreferencesGroup>
|
|
);
|
|
};
|