From 349ad6356a598aa402abb611ff9da27924fa8cd5 Mon Sep 17 00:00:00 2001 From: Jesper <> Date: Mon, 16 Sep 2024 00:18:42 +0200 Subject: [PATCH] Login and such --- backend/src/Database.ts | 7 +++++++ backend/src/README.md | 15 +++++++++++++++ backend/src/User/Controller.ts | 26 ++++++++++++++++++++++++++ backend/src/User/index.ts | 14 ++++++++++++++ backend/src/admin/Controller.ts | 10 ++++++++++ backend/src/bun.lockb | Bin 0 -> 18946 bytes backend/src/index.ts | 9 +++++++++ backend/src/package.json | 18 ++++++++++++++++++ backend/src/tsconfig.json | 27 +++++++++++++++++++++++++++ 9 files changed, 126 insertions(+) create mode 100644 backend/src/Database.ts create mode 100644 backend/src/README.md create mode 100644 backend/src/User/Controller.ts create mode 100644 backend/src/User/index.ts create mode 100644 backend/src/admin/Controller.ts create mode 100755 backend/src/bun.lockb create mode 100644 backend/src/index.ts create mode 100644 backend/src/package.json create mode 100644 backend/src/tsconfig.json diff --git a/backend/src/Database.ts b/backend/src/Database.ts new file mode 100644 index 0000000..b63db5d --- /dev/null +++ b/backend/src/Database.ts @@ -0,0 +1,7 @@ +import { Database } from "bun:sqlite"; + + +const db = new Database("../db.db", {strict: true}) +db.exec("PRAGMA journal_mode = WAL;"); + +export default db \ No newline at end of file diff --git a/backend/src/README.md b/backend/src/README.md new file mode 100644 index 0000000..355c1c7 --- /dev/null +++ b/backend/src/README.md @@ -0,0 +1,15 @@ +# corpopasswordmanager + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run index.ts +``` + +This project was created using `bun init` in bun v1.1.22. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. diff --git a/backend/src/User/Controller.ts b/backend/src/User/Controller.ts new file mode 100644 index 0000000..081c107 --- /dev/null +++ b/backend/src/User/Controller.ts @@ -0,0 +1,26 @@ +import { Elysia } from 'elysia' +import db from '../Database' +import { User } from './index.ts' +import { authenticator } from 'otplib'; + +const user = new User(); + +const userController = new Elysia() + .post('/login', async ({ body }) => { + const msg = db.query(`select id, password, otp from users WHERE username = ?;`) + .get(body.username) + console.log(msg) + if(msg == null) return new Response("Invalid username or password", { status: 401 }) + const validPassword = await Bun.password.verify(body.password, msg.password) + if (!validPassword) return new Response("Invalid username or password", { status: 401 }) + // if(!authenticator.check(body.otp, msg.otp)) return new Response("Invalid OTP code", { status: 401 }) + return await user.createToken(msg.id) + }) + .post('/register', async({body}) => { + return await Bun.password.hash(body.password); + }) + .get('/otp', () => { + return authenticator.generateSecret(); + }) + +export default userController diff --git a/backend/src/User/index.ts b/backend/src/User/index.ts new file mode 100644 index 0000000..86791c3 --- /dev/null +++ b/backend/src/User/index.ts @@ -0,0 +1,14 @@ +import { nanoid } from 'nanoid'; +import db from '../Database' + +export class User { + + async createToken(userId: number) { + db.query(`DELETE FROM tokens WHERE user_id = ?;`).run(userId); + const token = nanoid(24); + const dbRes = db.query(`INSERT into tokens (user_id, token) VALUES (?, ?);`) + .run(userId, token) + console.log(token); + return token; + } +} \ No newline at end of file diff --git a/backend/src/admin/Controller.ts b/backend/src/admin/Controller.ts new file mode 100644 index 0000000..732fc7c --- /dev/null +++ b/backend/src/admin/Controller.ts @@ -0,0 +1,10 @@ +import { Elysia } from 'elysia' + +const adminController = new Elysia() +.get('/', () => "admin endpoint") +.post('/register', async({body}) => { + return await Bun.password.hash(body.password); + +}) + +export default adminController \ No newline at end of file diff --git a/backend/src/bun.lockb b/backend/src/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..c1a978ed5fedaf1dc2cd1bbbe584e8a92f3b768d GIT binary patch literal 18946 zcmeHPcUTiy*AEy(u}}o8C?Kem1PE0GL_r00RqPGc2mu2j2}uB9E#NA$t_>7rEg)@W z1r#h;LF@%=h!re|4MAL8S8);f&Y9%G?7l1D?)S(0K9ARDJTr6e?|08R_s-lhISe$g z2$f1K=JSOXJYksr{7|7994vvzH^`qa;IaHgl3=ck6=tcX%wRBd^%!>DJcHlFXP#Q| zPScECrm|SaGCcaXpuO+Si`V4!l6XQPNF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jM4$Gc({S19H4f<=LR7CZ^O zg?uT?KU5%~`XdWt%UNO(Unpy(?LUKnYCn_1qUHr{cz~u7Fww49)O@22bc<;U>*aQ` zs96kR(Q3KDtytDl2v}M!patP2+m;5jXB>5)UAF6qQcO?*syKso!!Z$bmq*yM#9Y|| zJXqLhvew>6v{JxFy8=#_owQ*ItvW!{t|&jWg(lNAtXT240a`3+#rKNIoE(OKN&viu z{igLNj+*FE7e^7OVOStkb(~64CWdPoM4it(?sSYERMF@DU zwGFKl(9o{HOA32^MFNooo<$*ym#saBe=3J2(Qz%W4f*XhhKQCz%|wWR*B(qW+h_z_ zj{yn=)xoHjO)Gw+{?-EkDA}$~)iMqPtmOivEP z^c8UV5(}K_&KE88=SpQPn6Kan9xCC(>myh!67qyHk(ejsiuo*RO5BR)|5OgD{L?i$ z36?W$sQXzRfbO$-RDWt~bd@j4fJS}--ouf-^|9eUTm*21vj9&m_S#}-UPb^E|J)D0 z4TmBKFWPcwa~TDgDo;_s4jVS1oIPBsI1xhAwbDMcQb34>S@N}?74?1&75edg+iqog zF`%JeLN|s5V_P%rW~~7oE``Y%EKI2$DPQr)S9z_NK^aXg(tx1l0xHLW6x "Hello") +.group('/user', (app) => app.use(userController)) +.group('/admin', (app) => app.use(adminController)) +.listen(3000) \ No newline at end of file diff --git a/backend/src/package.json b/backend/src/package.json new file mode 100644 index 0000000..fee179a --- /dev/null +++ b/backend/src/package.json @@ -0,0 +1,18 @@ +{ + "name": "corpopasswordmanager", + "module": "index.ts", + "type": "module", + "devDependencies": { + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "dependencies": { + "elysia": "^1.1.12", + "kysely": "^0.27.4", + "nanoid": "^5.0.7", + "otplib": "^12.0.1", + "qrcode": "^1.5.4" + } +} \ No newline at end of file diff --git a/backend/src/tsconfig.json b/backend/src/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/backend/src/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +}