ente/src/pages/credentials/index.tsx

118 lines
4.2 KiB
TypeScript
Raw Normal View History

2021-06-04 07:57:48 +00:00
import React, { useContext, useEffect, useState } from 'react';
import constants from 'utils/strings/constants';
2021-06-03 06:05:43 +00:00
import { clearData, getData, LS_KEYS } from 'utils/storage/localStorage';
2021-05-30 16:56:48 +00:00
import { useRouter } from 'next/router';
import { KeyAttributes } from 'types';
import { SESSION_KEYS, getKey } from 'utils/storage/sessionStorage';
2021-04-05 05:44:56 +00:00
import CryptoWorker, {
generateAndSaveIntermediateKeyAttributes,
2021-04-05 05:44:56 +00:00
setSessionKeys,
} from 'utils/crypto';
2021-05-30 16:56:48 +00:00
import { logoutUser } from 'services/userService';
import { isFirstLogin } from 'utils/storage';
import SingleInputForm from 'components/SingleInputForm';
import Container from 'components/Container';
2021-05-30 16:56:48 +00:00
import { Button, Card } from 'react-bootstrap';
2021-06-04 07:57:48 +00:00
import { AppContext } from 'pages/_app';
export default function Credentials() {
const router = useRouter();
2021-04-02 04:03:00 +00:00
const [keyAttributes, setKeyAttributes] = useState<KeyAttributes>();
2021-06-04 07:57:48 +00:00
const appContext = useContext(AppContext);
useEffect(() => {
2020-09-14 09:32:01 +00:00
router.prefetch('/gallery');
const user = getData(LS_KEYS.USER);
const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
2021-06-03 06:05:43 +00:00
if (!user?.token || !keyAttributes?.memLimit) {
clearData();
} else if (!keyAttributes) {
router.push('/generate');
} else if (key) {
router.push('/gallery');
} else {
setKeyAttributes(keyAttributes);
}
2021-06-04 07:57:48 +00:00
appContext.showNavBar(true);
}, []);
const verifyPassphrase = async (passphrase, setFieldError) => {
try {
2020-10-01 11:47:13 +00:00
const cryptoWorker = await new CryptoWorker();
const kek: string = await cryptoWorker.deriveKey(
passphrase,
keyAttributes.kekSalt,
keyAttributes.opsLimit,
2021-05-29 06:27:52 +00:00
keyAttributes.memLimit,
);
try {
const key: string = await cryptoWorker.decryptB64(
keyAttributes.encryptedKey,
keyAttributes.keyDecryptionNonce,
2021-05-29 06:27:52 +00:00
kek,
);
if (isFirstLogin()) {
await generateAndSaveIntermediateKeyAttributes(
passphrase,
keyAttributes,
2021-05-29 06:27:52 +00:00
key,
);
}
2021-04-05 05:44:56 +00:00
setSessionKeys(key);
router.push('/gallery');
} catch (e) {
console.error(e);
setFieldError('passphrase', constants.INCORRECT_PASSPHRASE);
}
} catch (e) {
setFieldError(
'passphrase',
2021-05-29 06:27:52 +00:00
`${constants.UNKNOWN_ERROR} ${e.message}`,
);
}
};
return (
<>
<Container>
<Card
2021-05-30 16:56:48 +00:00
style={{ minWidth: '320px', padding: '40px 30px' }}
className="text-center"
>
<Card.Body>
2021-05-30 16:56:48 +00:00
<Card.Title style={{ marginBottom: '24px' }}>
{constants.ENTER_PASSPHRASE}
</Card.Title>
<SingleInputForm
callback={verifyPassphrase}
placeholder={constants.RETURN_PASSPHRASE_HINT}
buttonText={constants.VERIFY_PASSPHRASE}
fieldType="password"
/>
<div
style={{
display: 'flex',
flexDirection: 'column',
marginTop: '12px',
}}
>
<Button
variant="link"
onClick={() => router.push('/recover')}
>
{constants.FORGOT_PASSWORD}
</Button>
<Button variant="link" onClick={logoutUser}>
{constants.GO_BACK}
</Button>
</div>
</Card.Body>
</Card>
</Container>
</>
);
}