mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
891 B
28 lines
891 B
import { BULL_BOARD_COOKIE_NAME } from '@ghostfolio/common/config';
|
|
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
|
|
|
|
import { ForbiddenException, Injectable, NestMiddleware } from '@nestjs/common';
|
|
import { NextFunction, Request, Response } from 'express';
|
|
import passport from 'passport';
|
|
|
|
@Injectable()
|
|
export class BullBoardAuthMiddleware implements NestMiddleware {
|
|
public use(req: Request, res: Response, next: NextFunction) {
|
|
const token = req.cookies?.[BULL_BOARD_COOKIE_NAME];
|
|
|
|
if (token) {
|
|
req.headers.authorization = `Bearer ${token}`;
|
|
}
|
|
|
|
passport.authenticate('jwt', { session: false }, (error, user) => {
|
|
if (
|
|
error ||
|
|
!hasPermission(user?.permissions, permissions.accessAdminControl)
|
|
) {
|
|
next(new ForbiddenException());
|
|
} else {
|
|
next();
|
|
}
|
|
})(req, res, next);
|
|
}
|
|
}
|
|
|