2020-10-01 01:29:24 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-09-12 21:53:41 +00:00
|
|
|
import Container from 'components/Container';
|
|
|
|
import styled from 'styled-components';
|
|
|
|
import Card from 'react-bootstrap/Card';
|
|
|
|
import Form from 'react-bootstrap/Form';
|
|
|
|
import Button from 'react-bootstrap/Button';
|
|
|
|
import constants from 'utils/strings/constants';
|
|
|
|
import { Formik, FormikHelpers } from 'formik';
|
2020-09-13 06:30:07 +00:00
|
|
|
import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
|
2020-09-12 21:53:41 +00:00
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import * as Yup from 'yup';
|
2021-04-02 04:03:00 +00:00
|
|
|
import { KeyAttributes } from 'types';
|
2020-09-13 06:30:07 +00:00
|
|
|
import { setKey, SESSION_KEYS, getKey } from 'utils/storage/sessionStorage';
|
2021-04-05 05:44:56 +00:00
|
|
|
import CryptoWorker, {
|
2021-04-05 05:50:40 +00:00
|
|
|
generateAndSaveIntermediateKeyAttributes,
|
2021-04-05 05:44:56 +00:00
|
|
|
setSessionKeys,
|
|
|
|
} from 'utils/crypto';
|
2021-03-13 05:35:38 +00:00
|
|
|
import { logoutUser } from 'services/userService';
|
2021-04-03 04:49:37 +00:00
|
|
|
import { isFirstLogin } from 'utils/storage';
|
2021-04-03 05:00:37 +00:00
|
|
|
import { Spinner } from 'react-bootstrap';
|
2020-09-12 21:53:41 +00:00
|
|
|
|
|
|
|
const Image = styled.img`
|
|
|
|
width: 200px;
|
|
|
|
margin-bottom: 20px;
|
|
|
|
max-width: 100%;
|
|
|
|
`;
|
|
|
|
|
|
|
|
interface formValues {
|
|
|
|
passphrase: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Credentials() {
|
|
|
|
const router = useRouter();
|
2021-04-02 04:03:00 +00:00
|
|
|
const [keyAttributes, setKeyAttributes] = useState<KeyAttributes>();
|
2020-09-12 21:53:41 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
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-02-09 09:33:54 +00:00
|
|
|
const verifyPassphrase = async (
|
|
|
|
values: formValues,
|
|
|
|
{ setFieldError }: FormikHelpers<formValues>
|
|
|
|
) => {
|
2020-09-12 21:53:41 +00:00
|
|
|
setLoading(true);
|
|
|
|
try {
|
2020-10-01 11:47:13 +00:00
|
|
|
const cryptoWorker = await new CryptoWorker();
|
2020-09-12 21:53:41 +00:00
|
|
|
const { passphrase } = values;
|
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-04-02 04:03:00 +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,
|
|
|
|
kek
|
|
|
|
);
|
2021-04-02 04:49:57 +00:00
|
|
|
if (isFirstLogin()) {
|
2021-04-05 05:50:40 +00:00
|
|
|
generateAndSaveIntermediateKeyAttributes(
|
2021-04-02 04:26:25 +00:00
|
|
|
passphrase,
|
|
|
|
keyAttributes,
|
|
|
|
key
|
|
|
|
);
|
|
|
|
}
|
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',
|
|
|
|
`${constants.UNKNOWN_ERROR} ${e.message}`
|
|
|
|
);
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|
|
|
|
setLoading(false);
|
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 (
|
|
|
|
<Container>
|
2021-03-09 10:47:39 +00:00
|
|
|
{/* <Image alt="vault" src="/vault.png" /> */}
|
2021-03-13 05:35:38 +00:00
|
|
|
<Card
|
|
|
|
style={{ minWidth: '320px', padding: '40px 30px' }}
|
|
|
|
className="text-center"
|
|
|
|
>
|
2021-02-09 09:33:54 +00:00
|
|
|
<Card.Body>
|
2021-03-13 05:35:38 +00:00
|
|
|
<Card.Title style={{ marginBottom: '24px' }}>
|
|
|
|
{constants.ENTER_PASSPHRASE}
|
|
|
|
</Card.Title>
|
2021-02-09 09:33:54 +00:00
|
|
|
<Formik<formValues>
|
|
|
|
initialValues={{ passphrase: '' }}
|
|
|
|
onSubmit={verifyPassphrase}
|
|
|
|
validationSchema={Yup.object().shape({
|
|
|
|
passphrase: Yup.string().required(
|
|
|
|
constants.REQUIRED
|
|
|
|
),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{({
|
|
|
|
values,
|
|
|
|
touched,
|
|
|
|
errors,
|
|
|
|
handleChange,
|
|
|
|
handleBlur,
|
|
|
|
handleSubmit,
|
|
|
|
}) => (
|
|
|
|
<Form noValidate onSubmit={handleSubmit}>
|
|
|
|
<Form.Group>
|
|
|
|
<Form.Control
|
|
|
|
type="password"
|
|
|
|
placeholder={
|
|
|
|
constants.RETURN_PASSPHRASE_HINT
|
|
|
|
}
|
|
|
|
value={values.passphrase}
|
|
|
|
onChange={handleChange('passphrase')}
|
|
|
|
onBlur={handleBlur('passphrase')}
|
|
|
|
isInvalid={Boolean(
|
|
|
|
touched.passphrase &&
|
2021-04-02 04:03:00 +00:00
|
|
|
errors.passphrase
|
2021-02-09 09:33:54 +00:00
|
|
|
)}
|
|
|
|
disabled={loading}
|
2021-02-17 09:09:52 +00:00
|
|
|
autoFocus={true}
|
2021-02-09 09:33:54 +00:00
|
|
|
/>
|
|
|
|
<Form.Control.Feedback type="invalid">
|
|
|
|
{errors.passphrase}
|
|
|
|
</Form.Control.Feedback>
|
|
|
|
</Form.Group>
|
|
|
|
<Button block type="submit" disabled={loading}>
|
2021-04-03 05:00:37 +00:00
|
|
|
{loading ? (
|
|
|
|
<Spinner animation="border" />
|
|
|
|
) : (
|
|
|
|
constants.VERIFY_PASSPHRASE
|
|
|
|
)}
|
2021-02-09 09:33:54 +00:00
|
|
|
</Button>
|
2021-03-13 05:35:38 +00:00
|
|
|
<br />
|
2021-04-05 10:05:44 +00:00
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Button variant="link">
|
|
|
|
{constants.FORGOT_PASSWORD}
|
|
|
|
</Button>
|
|
|
|
<Button variant="link" onClick={logoutUser}>
|
2021-03-13 05:35:38 +00:00
|
|
|
{constants.LOGOUT}
|
2021-04-05 10:05:44 +00:00
|
|
|
</Button>
|
2021-03-13 05:35:38 +00:00
|
|
|
</div>
|
2021-02-09 09:33:54 +00:00
|
|
|
</Form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
</Card.Body>
|
|
|
|
</Card>
|
|
|
|
</Container>
|
|
|
|
);
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|