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 0000000..c1a978e Binary files /dev/null and b/backend/src/bun.lockb differ diff --git a/backend/src/index.ts b/backend/src/index.ts new file mode 100644 index 0000000..9a47ea3 --- /dev/null +++ b/backend/src/index.ts @@ -0,0 +1,9 @@ +import {Elysia} from 'elysia' +import userController from './User/Controller' +import adminController from './admin/Controller' + +const app = new Elysia() +.get('/', () => "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 + } +}