diff --git a/src/@shared/package.json b/src/@shared/package.json index 3585902..a75a800 100644 --- a/src/@shared/package.json +++ b/src/@shared/package.json @@ -16,6 +16,8 @@ "fastify": "^5.0.0", "fastify-plugin": "^5.0.1", "joi": "^18.0.0", + "otp": "^1.1.2", + "rfc4648": "^1.5.4", "uuidv7": "^1.0.2" }, "devDependencies": { diff --git a/src/@shared/src/auth/_inner.ts b/src/@shared/src/auth/_inner.ts index 58f5329..72534cd 100644 --- a/src/@shared/src/auth/_inner.ts +++ b/src/@shared/src/auth/_inner.ts @@ -2,42 +2,15 @@ //! //! This file is here because it is easier to share code in here. -import { FastifyInstance } from "fastify"; -import type { Database } from "@shared/database" -import { UserId } from "../database/mixin/user"; +import type { Database } from "@shared/database"; +import type { UserId } from "../database/mixin/user"; +import OTP, * as OtpModules from "otp"; -export default {}; +let secret = "JKZSGXRCBP3UHOFVDVLYQ3W43IZH3D76"; +let otp = new OTP({ secret, name: "test" }); - -class OTP { - private db: Database; - private static EPOCH: number = 0; - private static KEY_SIZE: number = 64; - private static TIME_STEP: number = 30; - - constructor(db: Database) { - this.db = db; - } - - private static Now(): number { - return Math.floor(Date.now() / 1000) - } - - private static getT(): number { - return Math.floor((OTP.Now() - this.EPOCH) / this.TIME_STEP) - } - - public verify(userid: UserId, code: string): boolean { - return true; - } - - public newUser(userid: UserId): string { - return "super topt secret"; - } - - public generate(userid: UserId): string | null { - let secret = this.db.getUserOTPSecret(userid); - - return null - } -} +console.log(`${otp.totpURL}`); +console.log(new URL(otp.totpURL)); +setInterval(() => { + console.log(`${otp.totp(Date.now())}`); +}, 999); diff --git a/src/@shared/src/auth/index.ts b/src/@shared/src/auth/index.ts index b720ba8..95137c3 100644 --- a/src/@shared/src/auth/index.ts +++ b/src/@shared/src/auth/index.ts @@ -2,7 +2,10 @@ import fastifyJwt from "@fastify/jwt"; import { FastifyPluginAsync } from "fastify"; import fp from 'fastify-plugin' import { user } from "@shared/database" +import OTP from "otp"; + +export const Otp = OTP; export const jwtPlugin = fp(async (fastify, _opts) => { let env = process.env.JWT_SECRET; if (env === undefined || env === null) diff --git a/src/tsconfig.base.json b/src/tsconfig.base.json index 315f159..e0ab587 100644 --- a/src/tsconfig.base.json +++ b/src/tsconfig.base.json @@ -22,8 +22,10 @@ "skipLibCheck": true, "lib": ["ESNext"], "paths": { + "@shared/auth": ["@shared/src/auth"], + "@shared/auth/*": ["@shared/src/auth/*"], "@shared/database": ["@shared/src/database"], - "@shared/auth": ["@shared/src/auth"] + "@shared/database/*": ["@shared/src/database/*"] } } }