From e3e6dd63a3b11254a67435b85192bd5ae46ebddb Mon Sep 17 00:00:00 2001 From: justuravgcoder Date: Mon, 19 Aug 2024 14:50:25 +0500 Subject: [PATCH 1/4] Server Files Updated --- .gitignore | 3 +++ server.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 .gitignore create mode 100644 server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42aea45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +package.json +package-lock.json \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..7312152 --- /dev/null +++ b/server.js @@ -0,0 +1,75 @@ +const exp = require("constants"); +const express = require("express"); +const { request } = require("http"); +const { type } = require("os"); +const app = express(); + +const PORT = 8080; + +app.use(express.json()); +app.use(express.urlencoded({extended:false})); +app.use(express.text()); + +const repo_data = [ + { + "repo_name": "Hackathon prep", + "details": "this repo comprises code regarding hackathon 2024", + "repo_author": "Shurahbeel Peerzada", + "commits": 214, + "year_created": 2024, + }, + { + "repo_name": "Machine Learning Models", + "details": "This repository contains machine learning models and datasets.", + "repo_author": "Alexis Jordan", + "commits": 150, + "year_created": 2023 + }, + { + "repo_name": "Weather App", + "details": "A weather forecasting application using React and Node.js.", + "repo_author": "Liam Patterson", + "commits": 75, + "year_created": 2022 + }, + { + "repo_name": "E-commerce Backend", + "details": "Backend code for an e-commerce platform built with Django.", + "repo_author": "Emily Martinez", + "commits": 340, + "year_created": 2021 + }, + { + "repo_name": "Portfolio Website", + "details": "Personal portfolio website showcasing projects and blogs.", + "repo_author": "Shurahbeel Peerzada", + "commits": 90, + "year_created": 2024 + }, + { + "repo_name": "Data Visualization Tool", + "details": "A tool for visualizing complex data sets using D3.js.", + "repo_author": "Nina West", + "commits": 125, + "year_created": 2023 + } +] + +app.listen(PORT, ()=>console.log(`Running on the port number: ${PORT}`)); + +app.get('/getallrepos', (request,response)=>{ + response.send(repo_data); +}) + +app.get('/getrepodetail',(request,response)=>{ + let name = request.query.name; + let specificRepo = repo_data.find(repo => repo.repo_name === name); + response.send(specificRepo); +}) + +app.post('/create-repo',(request,response)=>{ + const data = request.body; + console.log(data); + repo_data.push(data); + response.status(201).send("Repo Created") +}) \ No newline at end of file From 9f0312f1f22a5706aa0574c7eea46a0fa993dcbc Mon Sep 17 00:00:00 2001 From: justuravgcoder Date: Mon, 19 Aug 2024 15:49:45 +0500 Subject: [PATCH 2/4] Updated with changes --- .gitignore | 3 --- server.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 42aea45..e69de29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +0,0 @@ -node_modules/ -package.json -package-lock.json \ No newline at end of file diff --git a/server.js b/server.js index 7312152..85a94c4 100644 --- a/server.js +++ b/server.js @@ -10,6 +10,17 @@ app.use(express.json()); app.use(express.urlencoded({extended:false})); app.use(express.text()); +function specialwords(data) +{ + const notallowed = "!@#$%^&*()_+[]{}|;':\",./<>?`~\\-="; + for (let i =0; i{ app.get('/getrepodetail',(request,response)=>{ let name = request.query.name; - let specificRepo = repo_data.find(repo => repo.repo_name === name); - response.send(specificRepo); + if (name){ + if (specialwords(name)){ + response.status(400).send("Please avoid using special characters"); + } + else{ + let specificRepo = repo_data.find(repo => repo.repo_name === name); + if (specificRepo == undefined){ + response.status(404).send("No repo matchin provided name exists"); + } + else{ + response.status(200).send(specificRepo); + } + } + } + else{ + response.status(400).send("Please provide a repo name"); + } }) app.post('/create-repo',(request,response)=>{ const data = request.body; - console.log(data); repo_data.push(data); response.status(201).send("Repo Created") }) \ No newline at end of file From 8ad3680d5c5868b7fe8330a0a4ddea60a539675e Mon Sep 17 00:00:00 2001 From: justuravgcoder Date: Tue, 20 Aug 2024 13:29:15 +0500 Subject: [PATCH 3/4] Added Cookie and Further Authentication --- server.js | 92 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 31 deletions(-) diff --git a/server.js b/server.js index 85a94c4..cedd4f2 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,7 @@ const exp = require("constants"); +const cookieParser = require("cookie-parser"); const express = require("express"); -const { request } = require("http"); +const { request} = require("http"); const { type } = require("os"); const app = express(); @@ -9,12 +10,15 @@ const PORT = 8080; app.use(express.json()); app.use(express.urlencoded({extended:false})); app.use(express.text()); +app.use(cookieParser()); -function specialwords(data) -{ +const USERNAME = "Shurahbeel"; +const PASSWORD = "password"; + +function specialwords(data) { const notallowed = "!@#$%^&*()_+[]{}|;':\",./<>?`~\\-="; - for (let i =0; iconsole.log(`Running on the port number: ${PORT}`)); +app.listen(PORT, () => console.log(`Running on port number: ${PORT}`)); -app.get('/getallrepos', (request,response)=>{ - response.send(repo_data); -}) +app.post('/login', (req, res) => { + const { username, password } = req.body; + if (username === USERNAME && password === PASSWORD) { + res.cookie('auth', 'loggedin', { httpOnly: true }); + res.status(201).send("User Successfully Logged in"); + } else { + res.status(404).send("No user found with these credentials"); + } +}); -app.get('/getrepodetail',(request,response)=>{ - let name = request.query.name; - if (name){ - if (specialwords(name)){ - response.status(400).send("Please avoid using special characters"); - } - else{ - let specificRepo = repo_data.find(repo => repo.repo_name === name); - if (specificRepo == undefined){ - response.status(404).send("No repo matchin provided name exists"); - } - else{ - response.status(200).send(specificRepo); +app.get('/getallrepos', (req, res) => { + if (req.cookies.auth === "loggedin") { + res.status(200).send(repo_data); + } else { + res.status(401).send("Unauthorized Access. Please Login"); + } +}); + +app.get('/getrepodetail', (req, res) => { + if (req.cookies.auth === 'loggedin') { + let name = req.query.name; + if (name) { + if (specialwords(name)) { + res.status(400).send("Please avoid using special characters"); + } else { + let specificRepo = repo_data.find(repo => repo.repo_name === name); + if (specificRepo === undefined) { + res.status(404).send("No repo matching the provided name exists"); + } else { + res.status(200).send(specificRepo); } + } + } else { + res.status(400).send("Please provide a repo name"); } + } else { + res.status(401).send("Unauthorized Access. Please Login"); } - else{ - response.status(400).send("Please provide a repo name"); - } -}) +}); + +app.post('/create-repo', (req, res) => { + if (req.cookies.auth === 'loggedin') { + const data = req.body; -app.post('/create-repo',(request,response)=>{ - const data = request.body; - repo_data.push(data); - response.status(201).send("Repo Created") -}) \ No newline at end of file + if (specialwords(data.repo_name) || specialwords(data.repo_author) || specialwords(data.details)) { + res.status(400).send("Please avoid special characters in Name, Author, and Details"); + } else if (typeof data.year_created !== "number" || typeof data.commits !== "number") { + res.status(400).send("Please use numbers when providing commits and year created"); + } else { + repo_data.push(data); + res.status(201).send("Repo Created"); + } + } else { + res.status(401).send("Unauthorized Access. Please Login"); + } +}); From f133717ce791454639d8f0457987a2b4147e0f25 Mon Sep 17 00:00:00 2001 From: justuravgcoder Date: Tue, 20 Aug 2024 15:45:48 +0500 Subject: [PATCH 4/4] Added Middlewares --- server.js | 78 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/server.js b/server.js index cedd4f2..2ae08d9 100644 --- a/server.js +++ b/server.js @@ -15,6 +15,32 @@ app.use(cookieParser()); const USERNAME = "Shurahbeel"; const PASSWORD = "password"; +const authMiddleware = (req,res,next)=>{ + if (req.cookies.auth === "loggedin") { + next(); + } else { + res.status(401).send("Unauthorized Access. Please Login"); + } +} + +const repovalidation = (req,res,next)=>{ + const data = req.body; + if (specialwords(data.repo_name) || specialwords(data.repo_author) || specialwords(data.details)) + { + res.status(400).send("Please avoid special characters in Name, Author, and Details"); + } + else if (typeof data.year_created !== "number" || typeof data.commits !== "number") + { + res.status(400).send("Please use numbers when providing commits and year created"); + } + else if (!data.repo_name || !data.repo_author || !data.details || !data.year_created || !data.commits) + { + res.status(401).send("Missing Required Fields"); + } + + next(); +} + function specialwords(data) { const notallowed = "!@#$%^&*()_+[]{}|;':\",./<>?`~\\-="; for (let i = 0; i < data.length; i++) { @@ -74,24 +100,28 @@ app.listen(PORT, () => console.log(`Running on port number: ${PORT}`)); app.post('/login', (req, res) => { const { username, password } = req.body; - if (username === USERNAME && password === PASSWORD) { - res.cookie('auth', 'loggedin', { httpOnly: true }); - res.status(201).send("User Successfully Logged in"); - } else { - res.status(404).send("No user found with these credentials"); + if((username == "") && (password == "")) + { + res.status(400).send("Missing Username and Password") + } + else{ + if (username === USERNAME && password === PASSWORD) { + res.cookie('auth', 'loggedin', { httpOnly: true}); + res.status(200).send("User Successfully Logged in"); + } + else + { + res.status(401).send("Incorrect Credentials Entered"); + } } + }); -app.get('/getallrepos', (req, res) => { - if (req.cookies.auth === "loggedin") { +app.get('/getallrepos',authMiddleware ,(req, res) => { res.status(200).send(repo_data); - } else { - res.status(401).send("Unauthorized Access. Please Login"); - } }); -app.get('/getrepodetail', (req, res) => { - if (req.cookies.auth === 'loggedin') { +app.get('/getrepodetail',authMiddleware ,(req, res) => { let name = req.query.name; if (name) { if (specialwords(name)) { @@ -106,25 +136,11 @@ app.get('/getrepodetail', (req, res) => { } } else { res.status(400).send("Please provide a repo name"); - } - } else { - res.status(401).send("Unauthorized Access. Please Login"); - } + } }); -app.post('/create-repo', (req, res) => { - if (req.cookies.auth === 'loggedin') { - const data = req.body; - - if (specialwords(data.repo_name) || specialwords(data.repo_author) || specialwords(data.details)) { - res.status(400).send("Please avoid special characters in Name, Author, and Details"); - } else if (typeof data.year_created !== "number" || typeof data.commits !== "number") { - res.status(400).send("Please use numbers when providing commits and year created"); - } else { - repo_data.push(data); - res.status(201).send("Repo Created"); - } - } else { - res.status(401).send("Unauthorized Access. Please Login"); - } +app.post('/create-repo',authMiddleware,repovalidation,(req, res) => { + const data = req.body; + repo_data.push(data); + res.status(201).send("Repo Created"); });