2020-10-01 01:29:24 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-09-12 21:53:41 +00:00
|
|
|
import styled from 'styled-components';
|
2021-04-05 11:19:15 +00:00
|
|
|
|
2020-09-12 21:53:41 +00:00
|
|
|
import constants from 'utils/strings/constants';
|
2021-04-05 11:09:21 +00:00
|
|
|
import { getData, LS_KEYS } from 'utils/storage/localStorage';
|
2020-09-12 21:53:41 +00:00
|
|
|
import { useRouter } from 'next/router';
|
2021-04-02 04:03:00 +00:00
|
|
|
import { KeyAttributes } from 'types';
|
2021-04-05 11:19:15 +00:00
|
|
|
import { 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-05 11:19:15 +00:00
|
|
|
import PassPhraseForm from 'components/PassphraseForm';
|
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-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
|
|
|
}
|
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-04-05 11:19:15 +00:00
|
|
|
<PassPhraseForm
|
|
|
|
callback={verifyPassphrase}
|
|
|
|
title={constants.ENTER_PASSPHRASE}
|
|
|
|
placeholder={constants.RETURN_PASSPHRASE_HINT}
|
|
|
|
buttonText={constants.VERIFY_PASSPHRASE}
|
|
|
|
fieldType="password"
|
|
|
|
alternateOption={{
|
|
|
|
text: constants.FORGOT_PASSWORD,
|
|
|
|
click: () => router.push('/recover'),
|
|
|
|
}}
|
|
|
|
back={logoutUser}
|
|
|
|
/>
|
2021-02-09 09:33:54 +00:00
|
|
|
);
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|