diff --git a/aws/src/lambdas/signup/index.js b/aws/src/lambdas/signup/index.js index 1f1e6e8..2f02f03 100644 --- a/aws/src/lambdas/signup/index.js +++ b/aws/src/lambdas/signup/index.js @@ -1,12 +1,83 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const handler = async (event) => { - console.log("Event: ", JSON.stringify(event, null, 2)); +const mysql = require('mysql2'); + +const dbConfig = { + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_NAME, +}; + +module.exports.handler = async (event) => { + const httpMethod = event.requestContext.http.method; + if (httpMethod === 'OPTIONS') { + return { + statusCode: 200, + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type,Authorization' + }, + body: '' + }; + } + console.log("Event: ", JSON.stringify(event, null, 2)); + + let request_body; + if (event.body) { + request_body = JSON.parse(event.body); + } else { + console.log("No body found"); return { + statusCode: 400, + body: JSON.stringify({ message: "No body found" }), + }; + } + + const { uuid, enc_uuid, enc_name, email, enc_phone, enc_answers } = request_body; + const time_creation = new Date().toISOString(); + + let connection; + + try { + connection = mysql.createConnection(dbConfig).promise(); + + const [existing] = await connection.execute( + 'SELECT uuid FROM users WHERE enc_email = ?', + [email] + ); + + if (existing.length > 0) { + return { statusCode: 200, - body: JSON.stringify({ - message: "Hello from signup lambda!" - }), + body: JSON.stringify({ message: "User already signed up." }), + }; + } + + await connection.execute( + 'INSERT INTO users (uuid, enc_uuid, enc_name, enc_email, enc_phone_num, enc_time_creation) VALUES (?, ?, ?, ?, ?, ?)', + [uuid, enc_uuid, enc_name, email, enc_phone, time_creation] + ); + + await connection.execute( + 'INSERT INTO sec_questions (uuid, enc_uuid, enc_question1, enc_question2, enc_question3, enc_question4, enc_question5, enc_question6, enc_question7, enc_question8, enc_question9, enc_question10) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + [uuid, enc_uuid, ...enc_answers] + ); + + return { + statusCode: 200, + body: JSON.stringify({ message: "User and security questions inserted successfully!" }), + }; + + } catch (error) { + console.error("Database error:", error); + return { + statusCode: 500, + body: JSON.stringify({ message: "Database operation failed", error: error.message }), }; + + } finally { + if (connection) { + await connection.end(); + } + } }; -module.exports = { handler }; diff --git a/aws/src/lambdas/signup/index.ts b/aws/src/lambdas/signup/index.ts deleted file mode 100644 index a4f8454..0000000 --- a/aws/src/lambdas/signup/index.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { APIGatewayEvent } from "aws-lambda"; - -const handler = async (event: APIGatewayEvent) => { - console.log("Event: ", JSON.stringify(event, null, 2)); - return { - statusCode: 200, - body: JSON.stringify({ - message: "Hello from signup lambda!" - }), - } -}; - -module.exports = { handler }; \ No newline at end of file diff --git a/aws/src/lambdas/signup/package.json b/aws/src/lambdas/signup/package.json index a9576d8..9d41263 100644 --- a/aws/src/lambdas/signup/package.json +++ b/aws/src/lambdas/signup/package.json @@ -1,9 +1,16 @@ { - "devDependencies": { - "@types/aws-lambda": "^8.10.147", - "typescript": "^5.7.3" + "name": "lambda", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", "dependencies": { - "cookie": "^1.0.2" + "mysql2": "^3.14.0" } -} + } + \ No newline at end of file diff --git a/aws/src/lambdas/signup/signup.zip b/aws/src/lambdas/signup/signup.zip deleted file mode 100644 index 9a093ce..0000000 Binary files a/aws/src/lambdas/signup/signup.zip and /dev/null differ diff --git a/aws/src/lambdas/signup/tsconfig.json b/aws/src/lambdas/signup/tsconfig.json deleted file mode 100644 index c40e4a5..0000000 --- a/aws/src/lambdas/signup/tsconfig.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "compilerOptions": { - "target": "ES2020", - "module": "CommonJS", - "lib": ["ES2020"], - "moduleResolution": "Node", - "rootDir": "./", - "strict": true, - "esModuleInterop": true - } -} \ No newline at end of file diff --git a/extension/src/hashpass/app/hashpass_signup/page.tsx b/extension/src/hashpass/app/hashpass_signup/page.tsx index 00380d1..297d9e3 100644 --- a/extension/src/hashpass/app/hashpass_signup/page.tsx +++ b/extension/src/hashpass/app/hashpass_signup/page.tsx @@ -3,6 +3,8 @@ import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { TextField, Button, Card, CardContent, Typography, Box, CircularProgress } from '@mui/material'; +import { encrypt } from '../security_components/tools/AES_tool'; +import { v4 as uuidv4 } from 'uuid'; const securityQuestions = [ "What is your favorite color?", @@ -66,10 +68,24 @@ const handleSubmit = async (e: React.FormEvent) => { } try { - const res = await fetch('/api/user-signup', { + const uuid = uuidv4(); + const enc_uuid = await encrypt(uuid, formData.passphrase); + const enc_name = await encrypt(formData.name, formData.passphrase); + const enc_phone = await encrypt(formData.phone, formData.passphrase); + const enc_answers = await Promise.all(formData.securityAnswers.map((answer: string) => encrypt(answer, formData.passphrase))); + const dataToSend = { + uuid: uuid, + enc_uuid: enc_uuid, + enc_name: enc_name, + email: formData.email, + enc_phone: enc_phone, + enc_answers: enc_answers, + } + + const res = await fetch('https://8fy84busdk.execute-api.us-east-1.amazonaws.com/API/signup', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(formData), + body: JSON.stringify(dataToSend), }); const data = await res.json();