2021-05-30 16:56:48 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-04-05 11:19:15 +00:00
|
|
|
|
2020-09-12 21:53:41 +00:00
|
|
|
import constants from 'utils/strings/constants';
|
2021-05-30 16:56:48 +00:00
|
|
|
import { getData, LS_KEYS } from 'utils/storage/localStorage';
|
|
|
|
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, {
|
2021-05-23 14:53:11 +00:00
|
|
|
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';
|
2021-05-11 05:03:09 +00:00
|
|
|
import SingleInputForm from 'components/SingleInputForm';
|
|
|
|
import Container from 'components/Container';
|
2021-05-30 16:56:48 +00:00
|
|
|
import { Button, Card } from 'react-bootstrap';
|
2020-09-12 21:53:41 +00:00
|
|
|
|
|
|
|
export default function Credentials() {
|
|
|
|
const router = useRouter();
|
2021-04-02 04:03:00 +00:00
|
|
|
const [keyAttributes, setKeyAttributes] = useState<KeyAttributes>();
|
2020-10-01 01:29:24 +00:00
|
|
|
|
2020-09-12 21:53:41 +00:00
|
|
|
useEffect(() => {
|
2020-09-14 09:32:01 +00:00
|
|
|
router.prefetch('/gallery');
|
2020-09-13 06:30:07 +00:00
|
|
|
const user = getData(LS_KEYS.USER);
|
|
|
|
const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
|
|
|
|
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
|
2020-09-12 21:53:41 +00:00
|
|
|
if (!user?.token) {
|
|
|
|
router.push('/');
|
|
|
|
} else if (!keyAttributes) {
|
|
|
|
router.push('/generate');
|
2020-09-13 06:30:07 +00:00
|
|
|
} else if (key) {
|
2021-02-09 09:33:54 +00:00
|
|
|
router.push('/gallery');
|
2020-09-12 21:53:41 +00:00
|
|
|
} else {
|
|
|
|
setKeyAttributes(keyAttributes);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2021-04-05 11:19:15 +00:00
|
|
|
const verifyPassphrase = async (passphrase, setFieldError) => {
|
2020-09-12 21:53:41 +00:00
|
|
|
try {
|
2020-10-01 11:47:13 +00:00
|
|
|
const cryptoWorker = await new CryptoWorker();
|
2021-02-09 09:33:54 +00:00
|
|
|
const kek: string = await cryptoWorker.deriveKey(
|
|
|
|
passphrase,
|
2021-03-18 09:56:40 +00:00
|
|
|
keyAttributes.kekSalt,
|
|
|
|
keyAttributes.opsLimit,
|
2021-05-29 06:27:52 +00:00
|
|
|
keyAttributes.memLimit,
|
2021-02-09 09:33:54 +00:00
|
|
|
);
|
2020-10-01 01:29:24 +00:00
|
|
|
|
2021-03-18 09:56:40 +00:00
|
|
|
try {
|
2021-02-09 09:33:54 +00:00
|
|
|
const key: string = await cryptoWorker.decryptB64(
|
|
|
|
keyAttributes.encryptedKey,
|
|
|
|
keyAttributes.keyDecryptionNonce,
|
2021-05-29 06:27:52 +00:00
|
|
|
kek,
|
2021-02-09 09:33:54 +00:00
|
|
|
);
|
2021-04-02 04:49:57 +00:00
|
|
|
if (isFirstLogin()) {
|
2021-05-23 14:53:11 +00:00
|
|
|
await generateAndSaveIntermediateKeyAttributes(
|
|
|
|
passphrase,
|
|
|
|
keyAttributes,
|
2021-05-29 06:27:52 +00:00
|
|
|
key,
|
2021-05-23 14:53:11 +00:00
|
|
|
);
|
2021-04-02 04:26:25 +00:00
|
|
|
}
|
2021-04-05 05:44:56 +00:00
|
|
|
setSessionKeys(key);
|
2020-09-12 21:53:41 +00:00
|
|
|
router.push('/gallery');
|
2021-03-18 09:56:40 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2020-09-12 21:53:41 +00:00
|
|
|
setFieldError('passphrase', constants.INCORRECT_PASSPHRASE);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-02-09 09:33:54 +00:00
|
|
|
setFieldError(
|
|
|
|
'passphrase',
|
2021-05-29 06:27:52 +00:00
|
|
|
`${constants.UNKNOWN_ERROR} ${e.message}`,
|
2021-02-09 09:33:54 +00:00
|
|
|
);
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|
2021-02-09 09:33:54 +00:00
|
|
|
};
|
2020-09-12 21:53:41 +00:00
|
|
|
|
2021-02-09 09:33:54 +00:00
|
|
|
return (
|
2021-05-11 05:03:09 +00:00
|
|
|
<>
|
|
|
|
<Container>
|
|
|
|
<Card
|
2021-05-30 16:56:48 +00:00
|
|
|
style={{ minWidth: '320px', padding: '40px 30px' }}
|
2021-05-11 05:03:09 +00:00
|
|
|
className="text-center"
|
|
|
|
>
|
|
|
|
<Card.Body>
|
2021-05-30 16:56:48 +00:00
|
|
|
<Card.Title style={{ marginBottom: '24px' }}>
|
2021-05-11 05:03:09 +00:00
|
|
|
{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>
|
|
|
|
</>
|
2021-02-09 09:33:54 +00:00
|
|
|
);
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|