From 2e5bff25a9782a4d8f9733be93d0d924540565e9 Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Thu, 15 Jun 2023 10:25:46 -0400 Subject: [PATCH] Refactor --- src/authentication.ts | 36 ------------------ src/controllers/AuthenticationController.ts | 41 ++++++++++++++++++++- src/index.ts | 4 +- src/middleware/injectUserMiddleware.ts | 4 +- 4 files changed, 44 insertions(+), 41 deletions(-) delete mode 100644 src/authentication.ts diff --git a/src/authentication.ts b/src/authentication.ts deleted file mode 100644 index c516e8f..0000000 --- a/src/authentication.ts +++ /dev/null @@ -1,36 +0,0 @@ -import jwt from 'jsonwebtoken' -import { prisma } from './prisma.js' -import { LdapController } from './controllers/LdapController.js' -import { UserController } from './controllers/UserController.js' - -const JWT_SECRET = process.env.JWT_SECRET || 'secret' - -export async function login(username: string, password: string) { - const ldap = new LdapController() - - await ldap.authenticate(username, password) - - await UserController.importUser(username) - - const token = jwt.sign({ username }, JWT_SECRET, { - expiresIn: '2 days' - }) - - return `Bearer ${token}` -} - -export async function authenticate(token: string) { - try { - const { username } = jwt.verify(token, JWT_SECRET) as { username: string } - - const user = await prisma.user.findUnique({ - where: { username } - }) - - if (!user) return null - - return user - } catch (error: any) { - throw new Error('Invalid token') - } -} diff --git a/src/controllers/AuthenticationController.ts b/src/controllers/AuthenticationController.ts index 804f68e..ef4ddba 100644 --- a/src/controllers/AuthenticationController.ts +++ b/src/controllers/AuthenticationController.ts @@ -1 +1,40 @@ -export class LoginController {} +import jwt from 'jsonwebtoken' +import { prisma } from '../prisma.js' +import { LdapController } from '../controllers/LdapController.js' +import { UserController } from '../controllers/UserController.js' + +const JWT_SECRET = process.env.JWT_SECRET || 'secret' + +export class AuthenticationController { + private constructor() {} + + static async login(username: string, password: string) { + const ldap = new LdapController() + + await ldap.authenticate(username, password) + + await UserController.importUser(username) + + const token = jwt.sign({ username }, JWT_SECRET, { + expiresIn: '2 days' + }) + + return `Bearer ${token}` + } + + static async authenticate(token: string) { + try { + const { username } = jwt.verify(token, JWT_SECRET) as { username: string } + + const user = await prisma.user.findUnique({ + where: { username } + }) + + if (!user) return null + + return user + } catch (error: any) { + throw new Error('Invalid token') + } + } +} diff --git a/src/index.ts b/src/index.ts index b535d40..12a74f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,9 +7,9 @@ import { authMiddleware } from './middleware/authMiddleware.js' import { hasRolesMiddleware } from './middleware/hasRolesMiddleware.js' import { RequestWithUser } from './types.js' -import { login } from './authentication.js' import { UserRouteController } from './controllers/UserRouteController.js' +import { AuthenticationController } from './controllers/AuthenticationController.js' const app = express() @@ -34,7 +34,7 @@ app.post('/api/login', async (req: Request, res: Response) => { return res.status(400).json({ error: 'Missing username or password' }) try { - const token = await login(username, password) + const token = await AuthenticationController.login(username, password) res.json({ token }) } catch (error: any) { res.status(401).json({ error: error.message }) diff --git a/src/middleware/injectUserMiddleware.ts b/src/middleware/injectUserMiddleware.ts index 0d849ec..d5e23c0 100644 --- a/src/middleware/injectUserMiddleware.ts +++ b/src/middleware/injectUserMiddleware.ts @@ -1,6 +1,6 @@ import { NextFunction, Request, Response } from 'express' import { RequestWithUser } from '../types.js' -import { authenticate } from '../authentication.js' +import { AuthenticationController } from '../controllers/AuthenticationController.js' function getToken(req: Request) { const authHeader = req.headers.authorization as string @@ -21,7 +21,7 @@ export async function injectUserMiddleware( const token = getToken(req) if (token) { - const user = await authenticate(token) + const user = await AuthenticationController.authenticate(token) req.user = user }