2021-04-05 06:17:31 +00:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-09-12 21:53:41 +00:00
|
|
|
import constants from 'utils/strings/constants';
|
2021-03-22 14:06:06 +00:00
|
|
|
import { logoutUser, putAttributes } from 'services/userService';
|
2021-05-23 13:55:56 +00:00
|
|
|
import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
|
2020-09-12 21:53:41 +00:00
|
|
|
import { useRouter } from 'next/router';
|
2021-04-05 09:56:13 +00:00
|
|
|
import { getKey, SESSION_KEYS } from 'utils/storage/sessionStorage';
|
2021-05-23 13:55:56 +00:00
|
|
|
import {
|
2021-04-05 05:44:56 +00:00
|
|
|
setSessionKeys,
|
2021-05-23 13:55:56 +00:00
|
|
|
generateIntermediateKeyAttributes,
|
|
|
|
generateKeyAttributes,
|
2021-04-05 05:44:56 +00:00
|
|
|
} from 'utils/crypto';
|
2021-05-11 04:42:06 +00:00
|
|
|
import SetPasswordForm from 'components/SetPasswordForm';
|
2021-04-05 15:37:56 +00:00
|
|
|
import { setJustSignedUp } from 'utils/storage';
|
2021-04-22 14:30:07 +00:00
|
|
|
import RecoveryKeyModal from 'components/RecoveryKeyModal';
|
2021-05-23 14:48:27 +00:00
|
|
|
import { KeyAttributes } from 'types';
|
2020-09-12 21:53:41 +00:00
|
|
|
|
2021-04-02 03:56:21 +00:00
|
|
|
export interface KEK {
|
2021-03-18 09:56:40 +00:00
|
|
|
key: string;
|
|
|
|
opsLimit: number;
|
|
|
|
memLimit: number;
|
|
|
|
}
|
|
|
|
|
2021-05-23 14:40:29 +00:00
|
|
|
export default function Generate(props) {
|
2020-09-12 21:53:41 +00:00
|
|
|
const [token, setToken] = useState<string>();
|
|
|
|
const router = useRouter();
|
2021-04-22 14:30:07 +00:00
|
|
|
const [recoverModalView, setRecoveryModalView] = useState(false);
|
2020-10-01 01:29:24 +00:00
|
|
|
|
2020-09-12 21:53:41 +00:00
|
|
|
useEffect(() => {
|
2021-05-23 14:40:29 +00:00
|
|
|
props.setLoading(true);
|
2021-04-05 14:41:24 +00:00
|
|
|
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
|
2021-05-23 14:48:27 +00:00
|
|
|
const keyAttributes: KeyAttributes = getData(
|
|
|
|
LS_KEYS.ORIGINAL_KEY_ATTRIBUTES
|
|
|
|
);
|
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);
|
2020-09-12 21:53:41 +00:00
|
|
|
if (!user?.token) {
|
2021-02-09 09:33:54 +00:00
|
|
|
router.push('/');
|
2021-05-23 14:40:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setToken(user.token);
|
2021-05-23 14:48:27 +00:00
|
|
|
if (keyAttributes?.encryptedKey) {
|
2021-05-23 14:40:29 +00:00
|
|
|
const main = async () => {
|
2021-05-23 14:48:27 +00:00
|
|
|
try {
|
|
|
|
await putAttributes(user.token, keyAttributes);
|
|
|
|
} catch (e) {
|
|
|
|
//ignore
|
|
|
|
}
|
|
|
|
setData(LS_KEYS.ORIGINAL_KEY_ATTRIBUTES, null);
|
2021-05-23 14:40:29 +00:00
|
|
|
setRecoveryModalView(true);
|
|
|
|
};
|
|
|
|
main();
|
2020-09-13 06:30:07 +00:00
|
|
|
} else if (key) {
|
2020-09-12 21:53:41 +00:00
|
|
|
router.push('/gallery');
|
|
|
|
}
|
2021-05-23 14:40:29 +00:00
|
|
|
props.setLoading(false);
|
2020-09-12 21:53:41 +00:00
|
|
|
}, []);
|
|
|
|
|
2021-04-05 06:17:31 +00:00
|
|
|
const onSubmit = async (passphrase, setFieldError) => {
|
2020-09-12 21:53:41 +00:00
|
|
|
try {
|
2021-05-23 13:55:56 +00:00
|
|
|
const { keyAttributes, masterKey } = await generateKeyAttributes(
|
|
|
|
passphrase
|
|
|
|
);
|
2021-04-05 06:17:31 +00:00
|
|
|
|
2021-05-23 14:40:29 +00:00
|
|
|
await putAttributes(token, keyAttributes);
|
2021-05-23 13:55:56 +00:00
|
|
|
const intermediateKeyAttribute =
|
|
|
|
await generateIntermediateKeyAttributes(
|
|
|
|
passphrase,
|
|
|
|
keyAttributes,
|
|
|
|
masterKey
|
|
|
|
);
|
|
|
|
setData(LS_KEYS.KEY_ATTRIBUTES, intermediateKeyAttribute);
|
2021-04-05 06:17:31 +00:00
|
|
|
|
2021-05-23 13:55:56 +00:00
|
|
|
await setSessionKeys(masterKey);
|
|
|
|
setJustSignedUp(true);
|
|
|
|
setRecoveryModalView(true);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
setFieldError('passphrase', constants.PASSWORD_GENERATION_FAILED);
|
|
|
|
}
|
2021-02-09 09:33:54 +00:00
|
|
|
};
|
2020-10-01 01:29:24 +00:00
|
|
|
|
2021-02-09 09:33:54 +00:00
|
|
|
return (
|
2021-04-05 06:17:31 +00:00
|
|
|
<>
|
2021-05-23 14:48:27 +00:00
|
|
|
{!recoverModalView && (
|
|
|
|
<SetPasswordForm
|
|
|
|
callback={onSubmit}
|
|
|
|
buttonText={constants.SET_PASSPHRASE}
|
|
|
|
back={logoutUser}
|
|
|
|
/>
|
|
|
|
)}
|
2021-04-22 14:30:07 +00:00
|
|
|
<RecoveryKeyModal
|
|
|
|
show={recoverModalView}
|
|
|
|
onHide={() => {
|
|
|
|
setRecoveryModalView(false);
|
|
|
|
router.push('/gallery');
|
|
|
|
}}
|
|
|
|
somethingWentWrong={() => null}
|
|
|
|
/>
|
2021-04-05 06:17:31 +00:00
|
|
|
</>
|
2021-02-09 09:33:54 +00:00
|
|
|
);
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|