diff --git a/Makefile b/Makefile index 45089a7..88feac3 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ run: clean build podman pod create --name=e2ee -p 3333:3333 podman run -d --pod=e2ee -e POSTGRES_PASSWORD="password" -e POSTGRES_DB="e2ee" -e POSTGRES_USER="e2ee" -e POSTGRES_INITDB_ARGS="--encoding=UTF-8 --lc-collate=C --lc-ctype=C" --name=e2ee-db docker.io/library/postgres:15 - podman run -d --pod=e2ee -e POSTGRES_PASSWORD="password" -e POSTGRES_DB="e2ee" -e POSTGRES_USER="e2ee" --name=e2ee-app e2ee-messaging-service:latest + podman run -d --pod=e2ee -e POSTGRES_PASSWORD="password" -e POSTGRES_DB="e2ee" -e POSTGRES_USER="e2ee" -e SHARED_SECRET="toto" --name=e2ee-app e2ee-messaging-service:latest build: podman build -t e2ee-messaging-service . test: diff --git a/src/app.js b/src/app.js index a97ddf2..36762e5 100644 --- a/src/app.js +++ b/src/app.js @@ -6,6 +6,7 @@ app.set("port", port); var io = require('socket.io')(http); const cookieParser = require('cookie-parser'); app.use(cookieParser()); +app.use(express.json()); // bootstrap app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); diff --git a/src/authentication.js b/src/authentication.js new file mode 100644 index 0000000..5368ae0 --- /dev/null +++ b/src/authentication.js @@ -0,0 +1,9 @@ +const sharedSecret = process.env.SHARED_SECRET; + +const authentication = { + checkSharedSecret: (providedSecret) => { + return sharedSecret === providedSecret; + } +}; + +module.exports = authentication; diff --git a/src/controllers/account.js b/src/controllers/account.js index 3c923c3..96b7ff3 100644 --- a/src/controllers/account.js +++ b/src/controllers/account.js @@ -1,4 +1,6 @@ const crypto = require('crypto'); +const database = require("../db"); +const authentication = require("../authentication"); const accountController = { getCookie: (req, res) => { @@ -18,6 +20,24 @@ const accountController = { console.log("cookie set"); } res.redirect('/'); + }, + register: async (req, res) => { + try { + const { sharedSecret, publicKey } = req.body; + if (!sharedSecret || !publicKey) { + return res.status(400).json({ error: "Missing sharedSecret or publicKey" }); + } + console.log('Received data:', { sharedSecret, publicKey }); + if (authentication.checkSharedSecret(sharedSecret)) { + database.addUser(publicKey); + } else { + return res.status(400).json({ error: "Wrong sharedSecret" }); + } + return res.status(201).json({ message: "Registration successful" }); + } catch (error) { + console.error("Error during registration:", error); + return res.status(500).json({ error: "Server error during registration" }); + } } }; diff --git a/src/db.js b/src/db.js index 5003a89..3932d02 100644 --- a/src/db.js +++ b/src/db.js @@ -83,6 +83,23 @@ const database = { } }); }); + }, + addUser: async (pubkey) => { + if (!pubkey) { + console.error("Pubkey is required"); + return; + } + try { + const result = await pool.query('SELECT NEXTVAL(\'uuid_sequence\') AS next_uuid'); + const nextUuid = result.rows[0].next_uuid; + await pool.query( + 'INSERT INTO "user" (uuid, pubkey) VALUES ($1, $2)', + [nextUuid, pubkey] + ); + console.log(`Added user with the public key ${pubkey} .`); + } catch (err) { + console.error('Error adding user:', err); + } } }; diff --git a/src/public/ecc.js b/src/public/ecc.js index ea359b2..2e18557 100644 --- a/src/public/ecc.js +++ b/src/public/ecc.js @@ -15,10 +15,6 @@ function exportedKeyToPem(key, type) { async function genKey() { // Generate keys const { publicKey, privateKey } = await crypto.subtle.generateKey( - // { - // name: "ECDSA", - // namedCurve: "P-384", - // }, { name: "Ed25519", }, @@ -28,9 +24,11 @@ async function genKey() { const exportedPubkey = await crypto.subtle.exportKey("spki", publicKey); const exportedPrivkey = await crypto.subtle.exportKey("pkcs8", privateKey); // const privkey = await crypto.subtle.wrapKey("pkcs8", privateKey, wrapkey, { name: "AES-KW" }); - - console.log(`publicKey: ${exportedKeyToPem(exportedPubkey, publicKey.type)}`); - console.log(`privateKey: ${exportedKeyToPem(exportedPrivkey, privateKey.type)}`); + const key = { + privateKey: exportedPrivkey, + publicKey: exportedPubkey + } + return key; } async function test(data) { diff --git a/src/public/index.html b/src/public/index.html index bd5ae5e..8537d04 100644 --- a/src/public/index.html +++ b/src/public/index.html @@ -23,11 +23,12 @@