Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: login and signup forms, params recuparation, home page and links #1

Merged
merged 18 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add encryption and decryption of files
  • Loading branch information
adrienfort committed Jun 5, 2021
commit 3e73071ec1a6a70f98d0f316b415711bbcf9f108
5 changes: 2 additions & 3 deletions src/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ function App() {
const ipfs = IPFS('http://localhost:5001');
const [orbit_db, setOrbitDB] = useState(null)
const [fileHash, setFileHash] = useState("");
const [token, setToken] = useState("no token");

useEffect(() => {
async function initInstance() {
Expand All @@ -33,13 +32,13 @@ function App() {
<Route path="/signup">
<div className="route-container">
<h3>Inter Planetary Cloud</h3>
<Signup orbit_db={orbit_db} setToken={setToken}/>
<Signup orbit_db={orbit_db}/>
</div>
</Route>
<Route path="/login">
<div className="route-container">
<h3>Inter Planetary Cloud</h3>
<Login orbit_db={orbit_db} setToken={setToken}/>
<Login orbit_db={orbit_db}/>
</div>
</Route>
<Route path="/dashboard">
Expand Down
26 changes: 21 additions & 5 deletions src/components/button/DownloadButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import Button from '@material-ui/core/Button';
import './DownloadButton.css'

const fileDownload = require('js-file-download');
const all = require('it-all')
const concat = require('it-concat')
const all = require('it-all');
const concat = require('it-concat');

const CryptoJS = require("crypto-js");
const jwt = require('jsonwebtoken');
const JWT_SECRET = process.env.REACT_APP_JWT_SECRET;

/**
* Download the content of a file from IPFS via the client & hash, then extract it via the setter.
Expand All @@ -14,8 +18,12 @@ const concat = require('it-concat')
*/
async function downloadFromIPFS(ipfs, hash) {
const result = await all(ipfs.get(hash));
console.log(result)
const fileContent = await concat(result[0].content);
const bufferList = await concat(result[0].content);
const hashFileContent = bufferList._bufs[0].toString();
const password = jwt.verify(localStorage.token, JWT_SECRET).password;
const bytes = CryptoJS.AES.decrypt(hashFileContent, password);
const fileContent = bytes.toString(CryptoJS.enc.Utf8);

return (fileContent);
}

Expand All @@ -36,7 +44,15 @@ export function DownloadButton({ipfs}) {
const blob = new Blob([data]);
fileDownload(blob, hash + '.txt');
}
const inputOnChange = (event) => setHash(event.target.value);
const inputOnChange = (event) => {
const token = localStorage.token;

if (token !== undefined) {
setHash(event.target.value);
} else {
console.log("user has no token")
}
}

return (
<div>
Expand Down
24 changes: 21 additions & 3 deletions src/components/button/UploadButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import React, { useState } from 'react';
import Button from '@material-ui/core/Button';
import './UploadButton.css'

const CryptoJS = require("crypto-js");
const jwt = require('jsonwebtoken');
const JWT_SECRET = process.env.REACT_APP_JWT_SECRET;

/**
* Upload the content of a file to IPFS via the client and save the file's hash.
* @param ipfs - IPFS Client.
Expand All @@ -11,6 +15,7 @@ import './UploadButton.css'
*/
async function uploadToIPFS(ipfs, fileContent, setFileHash) {
const result = await ipfs.add(fileContent);

await setFileHash(result.path);
}

Expand All @@ -21,6 +26,7 @@ async function uploadToIPFS(ipfs, fileContent, setFileHash) {
*/
function extractFilename(filepath, setFilename) {
const result = /[^\\]*$/.exec(filepath)[0];

setFilename(result);
}

Expand All @@ -31,7 +37,13 @@ function extractFilename(filepath, setFilename) {
*/
function getFileContent(file, setFileContent) {
const reader = new window.FileReader();
reader.onload = (event) => setFileContent(event.target.result);
const token = localStorage.token;
const password = jwt.verify(token, JWT_SECRET).password;

reader.onload = (event) => {
const hashFileContent = CryptoJS.AES.encrypt(event.target.result, password).toString();
setFileContent(hashFileContent);
}
reader.readAsText(file);
}

Expand All @@ -47,8 +59,14 @@ function UploadButton({ ipfs, setFileHash }) {
const [fileContent, setFileContent] = useState("");

const inputOnChange = async (event) => {
extractFilename(event.target.value, setFilename);
getFileContent(event.target.files[0], setFileContent);
const token = localStorage.token;

if (token !== undefined) {
extractFilename(event.target.value, setFilename);
getFileContent(event.target.files[0], setFileContent);
} else {
console.log("user has no token")
}
};

const buttonOnClick = async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/forms/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const jwt = require('jsonwebtoken');
const KEYVALUE_DB_ADDRESS = process.env.REACT_APP_KEYVALUE_DB_ADDRESS
const JWT_SECRET = process.env.REACT_APP_JWT_SECRET

export function Login({orbit_db, setToken}) {
export function Login({orbit_db}) {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");

Expand Down Expand Up @@ -36,8 +36,8 @@ export function Login({orbit_db, setToken}) {
password: password
}
const token = jwt.sign(playload, JWT_SECRET)
setToken(token)
console.log(token)
localStorage.setItem('token', token);
console.log(localStorage.token)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/forms/Signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const jwt = require('jsonwebtoken');
const JWT_SECRET = process.env.REACT_APP_JWT_SECRET
const KEYVALUE_DB_ADDRESS = process.env.REACT_APP_KEYVALUE_DB_ADDRESS;

export function Signup({orbit_db, setToken}) {
export function Signup({orbit_db}) {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [repeatPassword, setRepeatPassword] = useState("");
Expand Down Expand Up @@ -37,8 +37,8 @@ export function Signup({orbit_db, setToken}) {
password: password
}
const token = jwt.sign(playload, JWT_SECRET)
setToken(token)
console.log(token)
localStorage.setItem('token', token);
console.log(localStorage.token)
}
}

Expand Down