diff --git a/src/controllers/LoginController.ts b/src/controllers/LoginController.ts new file mode 100644 index 0000000..547cd59 --- /dev/null +++ b/src/controllers/LoginController.ts @@ -0,0 +1,20 @@ +import { Request, Response } from 'express' +import { AuthenticationService } from '../services/AuthenticationService.js' + +export class LoginController { + static async login(req: Request, res: Response) { + const { username, password } = req.body + + if (!username || !password) { + res.status(400).json({ error: 'Missing username or password' }) + return + } + + try { + const token = await AuthenticationService.login(username, password) + res.json({ token }) + } catch (error: any) { + res.status(401).json({ error: error.message }) + } + } +} diff --git a/src/controllers/routes/LoginRouteController.ts b/src/controllers/routes/LoginRouteController.ts deleted file mode 100644 index ea2b777..0000000 --- a/src/controllers/routes/LoginRouteController.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Request, Response } from 'express' -import { AuthenticationController } from '../AuthenticationController.js' - -export class LoginRouteController { - static async login(req: Request, res: Response) { - const { username, password } = req.body - - if (!username || !password) - return res.status(400).json({ error: 'Missing username or password' }) - - try { - 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 a22ddb4..66b78ee 100644 --- a/src/middleware/injectUserMiddleware.ts +++ b/src/middleware/injectUserMiddleware.ts @@ -1,5 +1,5 @@ import { NextFunction, Request, Response } from 'express' -import { AuthenticationController } from '../controllers/AuthenticationController.js' +import { AuthenticationService } from '../services/AuthenticationService.js' function getToken(req: Request) { const authHeader = req.headers.authorization as string @@ -21,7 +21,7 @@ export async function injectUserMiddleware( if (token) { try { - const user = await AuthenticationController.authenticate(token) + const user = await AuthenticationService.jwtAuth(token) res.locals.user = user } catch (error: any) { return res.status(401).json({ error: error.message }) diff --git a/src/routers/LoginRouter.ts b/src/routers/LoginRouter.ts new file mode 100644 index 0000000..66217a3 --- /dev/null +++ b/src/routers/LoginRouter.ts @@ -0,0 +1,8 @@ +import { LoginController } from '../controllers/LoginController.js' +import { Router } from 'express' + +const router = Router() + +router.post('/', LoginController.login) + +export default router diff --git a/src/server.ts b/src/server.ts index a701932..3710da2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4,17 +4,16 @@ import bodyParser from 'body-parser' import { injectUserMiddleware } from './middleware/injectUserMiddleware.js' import { authMiddleware } from './middleware/authMiddleware.js' -import { LoginRouteController } from './controllers/routes/LoginRouteController.js' +import LoginRouter from './routers/LoginRouter.js' export const app = express() app.use('/', express.static('public')) +app.use(bodyParser.json()) app.use(injectUserMiddleware) -app.use(bodyParser.json()) - -app.post('/api/login', LoginRouteController.login) +app.use('/api/login', LoginRouter) app.get('/api/me', authMiddleware, async (req: Request, res: Response) => res.json(res.locals.user) diff --git a/src/controllers/AuthenticationController.ts b/src/services/AuthenticationService.ts similarity index 64% rename from src/controllers/AuthenticationController.ts rename to src/services/AuthenticationService.ts index 3563223..b51f63a 100644 --- a/src/controllers/AuthenticationController.ts +++ b/src/services/AuthenticationService.ts @@ -1,19 +1,19 @@ import jwt from 'jsonwebtoken' import { prisma } from '../prisma.js' -import { LdapController } from '../controllers/LdapController.js' -import { UserController } from '../controllers/UserController.js' +import { LdapService } from './LdapService.js' +import { UserService } from './UserService.js' const JWT_SECRET = process.env.JWT_SECRET || 'secret' -export class AuthenticationController { +export class AuthenticationService { private constructor() {} static async login(username: string, password: string) { - const ldap = new LdapController() + const ldap = new LdapService() await ldap.authenticate(username, password) - await UserController.importUser(username) + await UserService.importUser(username) const token = jwt.sign({ username }, JWT_SECRET, { expiresIn: '2 days' @@ -22,7 +22,7 @@ export class AuthenticationController { return `Bearer ${token}` } - static async authenticate(token: string) { + static async jwtAuth(token: string) { try { const { username } = jwt.verify(token, JWT_SECRET) as { username: string } @@ -30,7 +30,7 @@ export class AuthenticationController { where: { username } }) - if (!user) return await UserController.importUser(username) + if (!user) return await UserService.importUser(username) return user } catch (error: any) { diff --git a/src/controllers/LdapController.ts b/src/services/LdapService.ts similarity index 92% rename from src/controllers/LdapController.ts rename to src/services/LdapService.ts index 496358f..72e7cc1 100644 --- a/src/controllers/LdapController.ts +++ b/src/services/LdapService.ts @@ -18,17 +18,17 @@ type LdapUser = { groups?: string[] } -export class LdapController extends Client implements LdapClientInterface { - private static instance: LdapController +export class LdapService extends Client implements LdapClientInterface { + private static instance: LdapService constructor() { - if (LdapController.instance) return LdapController.instance + if (LdapService.instance) return LdapService.instance super({ url: `ldap://${process.env.AD_HOST}` }) - LdapController.instance = this + LdapService.instance = this } /** diff --git a/src/controllers/UserController.ts b/src/services/UserService.ts similarity index 88% rename from src/controllers/UserController.ts rename to src/services/UserService.ts index 1ba2e7c..5ff2389 100644 --- a/src/controllers/UserController.ts +++ b/src/services/UserService.ts @@ -1,14 +1,14 @@ import { User } from '@prisma/client' -import { LdapController } from '../controllers/LdapController.js' +import { LdapService } from './LdapService.js' import { prisma } from '../prisma.js' const ADMIN_GROUP = process.env.ADMIN_GROUP || 'PP-SERTI' const INSPECTOR_GROUP = process.env.INSPECTOR_GROUP || 'Inspectors' const USER_GROUP = process.env.USER_GROUP || 'G_SERVIDORES' -export class UserController { +export class UserService { static async importUser(username: string) { - const ldap = new LdapController() + const ldap = new LdapService() const ldapUser = await ldap.getUser(username)