diff --git a/package-lock.json b/package-lock.json index 0e1c4ebe..c7df6191 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@mui/material": "^5.13.0", "@reduxjs/toolkit": "^1.9.5", "bootstrap": "^4.6.0", + "crypto-js": "^4.2.0", "dotenv": "^16.3.1", "react": "^18.2.0", "react-bootstrap": "^2.8.0", @@ -30,6 +31,7 @@ "@babel/preset-typescript": "^7.21.5", "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^14.0.0", + "@types/crypto-js": "^4.2.2", "@types/jest": "^29.5.1", "@types/react": "^18.2.6", "@types/react-dom": "^18.2.4", @@ -3798,6 +3800,12 @@ "@types/node": "*" } }, + "node_modules/@types/crypto-js": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-4.2.2.tgz", + "integrity": "sha512-sDOLlVbHhXpAUAL0YHDUUwDZf3iN4Bwi4W6a0W0b+QcAezUbRtH4FVb+9J4h+XFPW7l/gQ9F8qC7P+Ec4k8QVQ==", + "dev": true + }, "node_modules/@types/eslint": { "version": "8.37.0", "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.37.0.tgz", @@ -6107,6 +6115,11 @@ "node": ">= 8" } }, + "node_modules/crypto-js": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.2.0.tgz", + "integrity": "sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==" + }, "node_modules/css-loader": { "version": "6.7.3", "resolved": "https://registry.npmjs.org/css-loader/-/css-loader-6.7.3.tgz", @@ -19417,6 +19430,12 @@ "@types/node": "*" } }, + "@types/crypto-js": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-4.2.2.tgz", + "integrity": "sha512-sDOLlVbHhXpAUAL0YHDUUwDZf3iN4Bwi4W6a0W0b+QcAezUbRtH4FVb+9J4h+XFPW7l/gQ9F8qC7P+Ec4k8QVQ==", + "dev": true + }, "@types/eslint": { "version": "8.37.0", "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.37.0.tgz", @@ -21236,6 +21255,11 @@ "which": "^2.0.1" } }, + "crypto-js": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.2.0.tgz", + "integrity": "sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==" + }, "css-loader": { "version": "6.7.3", "resolved": "https://registry.npmjs.org/css-loader/-/css-loader-6.7.3.tgz", diff --git a/package.json b/package.json index ad9d281c..78c68a1d 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "@mui/material": "^5.13.0", "@reduxjs/toolkit": "^1.9.5", "bootstrap": "^4.6.0", + "crypto-js": "^4.2.0", "dotenv": "^16.3.1", "react": "^18.2.0", "react-bootstrap": "^2.8.0", @@ -44,6 +45,7 @@ "@babel/preset-typescript": "^7.21.5", "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^14.0.0", + "@types/crypto-js": "^4.2.2", "@types/jest": "^29.5.1", "@types/react": "^18.2.6", "@types/react-dom": "^18.2.4", diff --git a/src/components/Pages/HomePage/HomePage.tsx b/src/components/Pages/HomePage/HomePage.tsx index af063ef8..3616fb04 100644 --- a/src/components/Pages/HomePage/HomePage.tsx +++ b/src/components/Pages/HomePage/HomePage.tsx @@ -1,4 +1,5 @@ import { bindActionCreators } from '@reduxjs/toolkit'; +import CryptoJS from 'crypto-js'; import React, { useEffect } from 'react'; import { Button } from 'react-bootstrap'; import { useIntl } from 'react-intl'; @@ -25,6 +26,8 @@ const HomePage = () => { const intl = useIntl(); + const secret = process.env.REACT_APP_DATA_SECRET ? process.env.REACT_APP_DATA_SECRET : ''; + useEffect(() => { fetchQuestions(setAllQuestions, setQuestionApiError); // eslint-disable-next-line react-hooks/exhaustive-deps @@ -56,10 +59,25 @@ const HomePage = () => { } }, [firstQuestion, setFirstQuestion]); + const decryptString = ( + cipher_text: string, + key: string | CryptoJS.lib.WordArray, + iv: CryptoJS.lib.WordArray, + ) => { + const bytes = CryptoJS.AES.decrypt(cipher_text, key, { iv: iv, mode: CryptoJS.mode.CBC }); + return bytes.toString(CryptoJS.enc.Utf8); + }; + const handleClick = async () => { const userValues = await startPoll(setIsLoggedIn); + const dataStr = userValues?.data[0]; + const iv = userValues?.data[1]; + const secretParse = window.atob(secret); + const key = CryptoJS.enc.Utf8.parse(secretParse); + const ivParsed = CryptoJS.enc.Base64.parse(iv); + const decrypted = decryptString(dataStr, key, ivParsed); setUserId(userValues?.id); - setCsrfToken(userValues?.token); + setCsrfToken(decrypted); }; return (