Refactor
This commit is contained in:
parent
c8557b9442
commit
2e5bff25a9
|
@ -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')
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,9 +7,9 @@ import { authMiddleware } from './middleware/authMiddleware.js'
|
||||||
import { hasRolesMiddleware } from './middleware/hasRolesMiddleware.js'
|
import { hasRolesMiddleware } from './middleware/hasRolesMiddleware.js'
|
||||||
|
|
||||||
import { RequestWithUser } from './types.js'
|
import { RequestWithUser } from './types.js'
|
||||||
import { login } from './authentication.js'
|
|
||||||
|
|
||||||
import { UserRouteController } from './controllers/UserRouteController.js'
|
import { UserRouteController } from './controllers/UserRouteController.js'
|
||||||
|
import { AuthenticationController } from './controllers/AuthenticationController.js'
|
||||||
|
|
||||||
const app = express()
|
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' })
|
return res.status(400).json({ error: 'Missing username or password' })
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const token = await login(username, password)
|
const token = await AuthenticationController.login(username, password)
|
||||||
res.json({ token })
|
res.json({ token })
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
res.status(401).json({ error: error.message })
|
res.status(401).json({ error: error.message })
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { NextFunction, Request, Response } from 'express'
|
import { NextFunction, Request, Response } from 'express'
|
||||||
import { RequestWithUser } from '../types.js'
|
import { RequestWithUser } from '../types.js'
|
||||||
import { authenticate } from '../authentication.js'
|
import { AuthenticationController } from '../controllers/AuthenticationController.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(
|
||||||
const token = getToken(req)
|
const token = getToken(req)
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
const user = await authenticate(token)
|
const user = await AuthenticationController.authenticate(token)
|
||||||
req.user = user
|
req.user = user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user