Refactor
This commit is contained in:
parent
36ef372726
commit
aa0305d1cb
20
src/controllers/LoginController.ts
Normal file
20
src/controllers/LoginController.ts
Normal file
|
@ -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 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { NextFunction, Request, Response } from 'express'
|
import { NextFunction, Request, Response } from 'express'
|
||||||
import { AuthenticationController } from '../controllers/AuthenticationController.js'
|
import { AuthenticationService } from '../services/AuthenticationService.js'
|
||||||
|
|
||||||
function getToken(req: Request) {
|
function getToken(req: Request) {
|
||||||
const authHeader = req.headers.authorization as string
|
const authHeader = req.headers.authorization as string
|
||||||
|
@ -21,7 +21,7 @@ export async function injectUserMiddleware(
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
try {
|
try {
|
||||||
const user = await AuthenticationController.authenticate(token)
|
const user = await AuthenticationService.jwtAuth(token)
|
||||||
res.locals.user = user
|
res.locals.user = user
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return res.status(401).json({ error: error.message })
|
return res.status(401).json({ error: error.message })
|
||||||
|
|
8
src/routers/LoginRouter.ts
Normal file
8
src/routers/LoginRouter.ts
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import { LoginController } from '../controllers/LoginController.js'
|
||||||
|
import { Router } from 'express'
|
||||||
|
|
||||||
|
const router = Router()
|
||||||
|
|
||||||
|
router.post('/', LoginController.login)
|
||||||
|
|
||||||
|
export default router
|
|
@ -4,17 +4,16 @@ import bodyParser from 'body-parser'
|
||||||
import { injectUserMiddleware } from './middleware/injectUserMiddleware.js'
|
import { injectUserMiddleware } from './middleware/injectUserMiddleware.js'
|
||||||
import { authMiddleware } from './middleware/authMiddleware.js'
|
import { authMiddleware } from './middleware/authMiddleware.js'
|
||||||
|
|
||||||
import { LoginRouteController } from './controllers/routes/LoginRouteController.js'
|
import LoginRouter from './routers/LoginRouter.js'
|
||||||
|
|
||||||
export const app = express()
|
export const app = express()
|
||||||
|
|
||||||
app.use('/', express.static('public'))
|
app.use('/', express.static('public'))
|
||||||
|
|
||||||
|
app.use(bodyParser.json())
|
||||||
app.use(injectUserMiddleware)
|
app.use(injectUserMiddleware)
|
||||||
|
|
||||||
app.use(bodyParser.json())
|
app.use('/api/login', LoginRouter)
|
||||||
|
|
||||||
app.post('/api/login', LoginRouteController.login)
|
|
||||||
|
|
||||||
app.get('/api/me', authMiddleware, async (req: Request, res: Response) =>
|
app.get('/api/me', authMiddleware, async (req: Request, res: Response) =>
|
||||||
res.json(res.locals.user)
|
res.json(res.locals.user)
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
import jwt from 'jsonwebtoken'
|
import jwt from 'jsonwebtoken'
|
||||||
import { prisma } from '../prisma.js'
|
import { prisma } from '../prisma.js'
|
||||||
import { LdapController } from '../controllers/LdapController.js'
|
import { LdapService } from './LdapService.js'
|
||||||
import { UserController } from '../controllers/UserController.js'
|
import { UserService } from './UserService.js'
|
||||||
|
|
||||||
const JWT_SECRET = process.env.JWT_SECRET || 'secret'
|
const JWT_SECRET = process.env.JWT_SECRET || 'secret'
|
||||||
|
|
||||||
export class AuthenticationController {
|
export class AuthenticationService {
|
||||||
private constructor() {}
|
private constructor() {}
|
||||||
|
|
||||||
static async login(username: string, password: string) {
|
static async login(username: string, password: string) {
|
||||||
const ldap = new LdapController()
|
const ldap = new LdapService()
|
||||||
|
|
||||||
await ldap.authenticate(username, password)
|
await ldap.authenticate(username, password)
|
||||||
|
|
||||||
await UserController.importUser(username)
|
await UserService.importUser(username)
|
||||||
|
|
||||||
const token = jwt.sign({ username }, JWT_SECRET, {
|
const token = jwt.sign({ username }, JWT_SECRET, {
|
||||||
expiresIn: '2 days'
|
expiresIn: '2 days'
|
||||||
|
@ -22,7 +22,7 @@ export class AuthenticationController {
|
||||||
return `Bearer ${token}`
|
return `Bearer ${token}`
|
||||||
}
|
}
|
||||||
|
|
||||||
static async authenticate(token: string) {
|
static async jwtAuth(token: string) {
|
||||||
try {
|
try {
|
||||||
const { username } = jwt.verify(token, JWT_SECRET) as { username: string }
|
const { username } = jwt.verify(token, JWT_SECRET) as { username: string }
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ export class AuthenticationController {
|
||||||
where: { username }
|
where: { username }
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!user) return await UserController.importUser(username)
|
if (!user) return await UserService.importUser(username)
|
||||||
|
|
||||||
return user
|
return user
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
|
@ -18,17 +18,17 @@ type LdapUser = {
|
||||||
groups?: string[]
|
groups?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LdapController extends Client implements LdapClientInterface {
|
export class LdapService extends Client implements LdapClientInterface {
|
||||||
private static instance: LdapController
|
private static instance: LdapService
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
if (LdapController.instance) return LdapController.instance
|
if (LdapService.instance) return LdapService.instance
|
||||||
|
|
||||||
super({
|
super({
|
||||||
url: `ldap://${process.env.AD_HOST}`
|
url: `ldap://${process.env.AD_HOST}`
|
||||||
})
|
})
|
||||||
|
|
||||||
LdapController.instance = this
|
LdapService.instance = this
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -1,14 +1,14 @@
|
||||||
import { User } from '@prisma/client'
|
import { User } from '@prisma/client'
|
||||||
import { LdapController } from '../controllers/LdapController.js'
|
import { LdapService } from './LdapService.js'
|
||||||
import { prisma } from '../prisma.js'
|
import { prisma } from '../prisma.js'
|
||||||
|
|
||||||
const ADMIN_GROUP = process.env.ADMIN_GROUP || 'PP-SERTI'
|
const ADMIN_GROUP = process.env.ADMIN_GROUP || 'PP-SERTI'
|
||||||
const INSPECTOR_GROUP = process.env.INSPECTOR_GROUP || 'Inspectors'
|
const INSPECTOR_GROUP = process.env.INSPECTOR_GROUP || 'Inspectors'
|
||||||
const USER_GROUP = process.env.USER_GROUP || 'G_SERVIDORES'
|
const USER_GROUP = process.env.USER_GROUP || 'G_SERVIDORES'
|
||||||
|
|
||||||
export class UserController {
|
export class UserService {
|
||||||
static async importUser(username: string) {
|
static async importUser(username: string) {
|
||||||
const ldap = new LdapController()
|
const ldap = new LdapService()
|
||||||
|
|
||||||
const ldapUser = await ldap.getUser(username)
|
const ldapUser = await ldap.getUser(username)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user