ente/src/pages/change-password/index.tsx

87 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-06-04 07:57:48 +00:00
import React, { useState, useEffect, useContext } from 'react';
2021-04-05 05:38:42 +00:00
import constants from 'utils/strings/constants';
2021-05-30 16:56:48 +00:00
import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
import { useRouter } from 'next/router';
2021-04-05 05:44:56 +00:00
import CryptoWorker, {
SaveKeyInSessionStore,
generateAndSaveIntermediateKeyAttributes,
2021-08-13 03:19:48 +00:00
B64EncryptionResult,
2021-04-05 05:44:56 +00:00
} from 'utils/crypto';
2021-05-30 16:56:48 +00:00
import { getActualKey } from 'utils/common/key';
import { setKeys, UpdatedKey } from 'services/userService';
import SetPasswordForm from 'components/SetPasswordForm';
2021-06-04 07:57:48 +00:00
import { AppContext } from 'pages/_app';
import { SESSION_KEYS } from 'utils/storage/sessionStorage';
2021-04-05 05:38:42 +00:00
export interface KEK {
key: string;
opsLimit: number;
memLimit: number;
}
export default function Generate() {
const [token, setToken] = useState<string>();
const router = useRouter();
2021-06-04 07:57:48 +00:00
const appContext = useContext(AppContext);
2021-04-05 05:38:42 +00:00
useEffect(() => {
const user = getData(LS_KEYS.USER);
if (!user?.token) {
router.push('/');
} else {
setToken(user.token);
}
2021-06-04 07:57:48 +00:00
appContext.showNavBar(true);
2021-04-05 05:38:42 +00:00
}, []);
const onSubmit = async (passphrase, setFieldError) => {
const cryptoWorker = await new CryptoWorker();
const key: string = await getActualKey();
const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
const kekSalt: string = await cryptoWorker.generateSaltToDeriveKey();
let kek: KEK;
2021-04-05 05:38:42 +00:00
try {
kek = await cryptoWorker.deriveSensitiveKey(passphrase, kekSalt);
} catch (e) {
setFieldError('confirm', constants.PASSWORD_GENERATION_FAILED);
return;
}
2021-08-13 02:38:38 +00:00
const encryptedKeyAttributes: B64EncryptionResult =
await cryptoWorker.encryptToB64(key, kek.key);
const updatedKey: UpdatedKey = {
kekSalt,
encryptedKey: encryptedKeyAttributes.encryptedData,
keyDecryptionNonce: encryptedKeyAttributes.nonce,
opsLimit: kek.opsLimit,
memLimit: kek.memLimit,
};
2021-04-05 05:38:42 +00:00
await setKeys(token, updatedKey);
2021-04-05 05:38:42 +00:00
const updatedKeyAttributes = Object.assign(keyAttributes, updatedKey);
await generateAndSaveIntermediateKeyAttributes(
passphrase,
updatedKeyAttributes,
2021-08-13 02:38:38 +00:00
key
);
2021-04-05 05:38:42 +00:00
await SaveKeyInSessionStore(SESSION_KEYS.ENCRYPTION_KEY, key);
redirectToGallery();
};
const redirectToGallery = () => {
2021-05-30 16:56:48 +00:00
setData(LS_KEYS.SHOW_BACK_BUTTON, { value: false });
router.push('/gallery');
2021-04-05 05:38:42 +00:00
};
return (
<SetPasswordForm
callback={onSubmit}
buttonText={constants.CHANGE_PASSWORD}
back={
2021-08-12 15:40:02 +00:00
getData(LS_KEYS.SHOW_BACK_BUTTON)?.value
? redirectToGallery
: null
}
/>
2021-04-05 05:38:42 +00:00
);
}