Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Модуль 8 «Ограничение доступа» #8

Merged
merged 18 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
fcb67e1
8.1. Установит пакет `jose`.
AntonovIgor Feb 20, 2024
6ea4ade
8.2. Добавит в конфигурацию `JWT_SECRET`.
AntonovIgor Feb 20, 2024
2487c3f
8.3. Добавит метод `verifyPassword` в `UserEntity`.
AntonovIgor Feb 20, 2024
7f0830a
8.4. Добавит `AuthService` и `DefaultAuthService`. WIP
AntonovIgor Feb 20, 2024
4fa7dd6
8.5. Новые ошибки `UserNotFoundException` и `UserPasswordIncorrectExc…
AntonovIgor Feb 20, 2024
2b23934
8.6. Добавит `AuthExceptionFilter`. WIP
AntonovIgor Feb 20, 2024
df7642b
8.7. Подготовим контейнер зависимостей для модуля `Auth`. WIP
AntonovIgor Feb 20, 2024
52c4d64
8.8. Добавит `login` в `UserController`.
AntonovIgor Feb 20, 2024
b1d3092
8.9. Добавит `ParseTokenMiddleware`. WIP
AntonovIgor Feb 20, 2024
dd150df
8.10. Расширит тип `Request`. WIP
AntonovIgor Feb 20, 2024
48834e5
8.11. Подключит `ParseTokenMiddleware` глобально.
AntonovIgor Feb 20, 2024
b0e46bd
8.12. Обновит `CommentController`.
AntonovIgor Feb 20, 2024
6e24d13
8.13. Добавит `PrivateRouteMiddleware`.
AntonovIgor Feb 20, 2024
ac73fb5
8.14. Подключит `PrivateRouteMiddleware` в `CommentController`.
AntonovIgor Feb 20, 2024
347edbb
8.15. Добавит ограничения для ресурсов создания данных.
AntonovIgor Feb 20, 2024
7bd1a3f
8.16. Заполнит `userId` при создании объявления.
AntonovIgor Feb 20, 2024
8a428e9
8.17. Добавит сценарий проверки авторизации.
AntonovIgor Feb 20, 2024
0f8b6f1
8.18. Добавит запрос для проверки состояния пользователя.
AntonovIgor Feb 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions custom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TokenPayload } from './src/shared/modules/auth/index.js';

declare module 'express-serve-static-core' {
export interface Request {
tokenPayload: TokenPayload;
}
}
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"got": "13.0.0",
"http-status-codes": "2.3.0",
"inversify": "6.0.1",
"jose": "4.15.4",
"mime-types": "2.1.35",
"mongoose": "7.5.3",
"multer": "1.4.5-lts.1",
Expand Down
2 changes: 2 additions & 0 deletions src/main.rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createUserContainer } from './shared/modules/user/index.js';
import { createCategoryContainer } from './shared/modules/category/index.js';
import { createOfferContainer } from './shared/modules/offer/index.js';
import { createCommentContainer } from './shared/modules/comment/index.js';
import { createAuthContainer } from './shared/modules/auth/index.js';


async function bootstrap() {
Expand All @@ -16,6 +17,7 @@ async function bootstrap() {
createCategoryContainer(),
createOfferContainer(),
createCommentContainer(),
createAuthContainer(),
);

const application = appContainer.get<RestApplication>(Component.RestApplication);
Expand Down
6 changes: 6 additions & 0 deletions src/rest/rest.application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Component } from '../shared/types/index.js';
import { DatabaseClient } from '../shared/libs/database-client/index.js';
import { getMongoURI } from '../shared/helpers/index.js';
import { Controller, ExceptionFilter } from '../shared/libs/rest/index.js';
import { ParseTokenMiddleware } from '../shared/libs/rest/middleware/parse-token.middleware.js';

@injectable()
export class RestApplication {
Expand All @@ -20,6 +21,7 @@ export class RestApplication {
@inject(Component.UserController) private readonly userController: Controller,
@inject(Component.OfferController) private readonly offerController: Controller,
@inject(Component.CommentController) private readonly commentController: Controller,
@inject(Component.AuthExceptionFilter) private readonly authExceptionFilter: ExceptionFilter,
) {
this.server = express();
}
Expand Down Expand Up @@ -49,14 +51,18 @@ export class RestApplication {
}

private async _initMiddleware() {
const authenticateMiddleware = new ParseTokenMiddleware(this.config.get('JWT_SECRET'));

this.server.use(express.json());
this.server.use(
'/upload',
express.static(this.config.get('UPLOAD_DIRECTORY'))
);
this.server.use(authenticateMiddleware.execute.bind(authenticateMiddleware));
}

private async _initExceptionFilters() {
this.server.use(this.authExceptionFilter.catch.bind(this.authExceptionFilter));
this.server.use(this.appExceptionFilter.catch.bind(this.appExceptionFilter));
}

Expand Down
7 changes: 7 additions & 0 deletions src/shared/libs/config/rest.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type RestSchema = {
DB_PORT: string;
DB_NAME: string;
UPLOAD_DIRECTORY: string;
JWT_SECRET: string;
}

export const configRestSchema = convict<RestSchema>({
Expand Down Expand Up @@ -63,4 +64,10 @@ export const configRestSchema = convict<RestSchema>({
env: 'UPLOAD_DIRECTORY',
default: null
},
JWT_SECRET: {
doc: 'Secret for sign JWT',
format: String,
env: 'JWT_SECRET',
default: null
},
});
2 changes: 2 additions & 0 deletions src/shared/libs/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export * from './middleware/validate-objectid.middleware.js';
export * from './middleware/validate-dto.middleware.js';
export * from './middleware/document-exists.middleware.js';
export * from './middleware/upload-file.middleware.js';
export * from './middleware/parse-token.middleware.js';
export * from './middleware/private-route.middleware.js';
48 changes: 48 additions & 0 deletions src/shared/libs/rest/middleware/parse-token.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { NextFunction, Request, Response } from 'express';
import { jwtVerify } from 'jose';
import { StatusCodes } from 'http-status-codes';
import { createSecretKey } from 'node:crypto';
import { Middleware } from './middleware.interface.js';
import { HttpError } from '../errors/index.js';
import { TokenPayload } from '../../../modules/auth/index.js';

function isTokenPayload(payload: unknown): payload is TokenPayload {
return (
(typeof payload === 'object' && payload !== null) &&
('email' in payload && typeof payload.email === 'string') &&
('firstname' in payload && typeof payload.firstname === 'string') &&
('lastname' in payload && typeof payload.lastname === 'string') &&
('id' in payload && typeof payload.id === 'string')
);
}

export class ParseTokenMiddleware implements Middleware {
constructor(private readonly jwtSecret: string) {}

public async execute(req: Request, _res: Response, next: NextFunction): Promise<void> {
const authorizationHeader = req.headers?.authorization?.split(' ');
if (!authorizationHeader) {
return next();
}

const [, token] = authorizationHeader;

try {
const { payload } = await jwtVerify(token, createSecretKey(this.jwtSecret, 'utf-8'));

if (isTokenPayload(payload)) {
req.tokenPayload = { ...payload };
return next();
} else {
throw new Error('Bad token');
}
} catch {

return next(new HttpError(
StatusCodes.UNAUTHORIZED,
'Invalid token',
'AuthenticateMiddleware')
);
}
}
}
18 changes: 18 additions & 0 deletions src/shared/libs/rest/middleware/private-route.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { StatusCodes } from 'http-status-codes';
import { NextFunction, Request, Response } from 'express';
import { Middleware } from './middleware.interface.js';
import { HttpError } from '../errors/index.js';

export class PrivateRouteMiddleware implements Middleware {
public async execute({ tokenPayload }: Request, _res: Response, next: NextFunction): Promise<void> {
if (! tokenPayload) {
throw new HttpError(
StatusCodes.UNAUTHORIZED,
'Unauthorized',
'PrivateRouteMiddleware'
);
}

return next();
}
}
6 changes: 6 additions & 0 deletions src/shared/modules/auth/auth-service.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { LoginUserDto, UserEntity } from '../user/index.js';

export interface AuthService {
authenticate(user: UserEntity): Promise<string>;
verify(dto: LoginUserDto): Promise<UserEntity>;
}
2 changes: 2 additions & 0 deletions src/shared/modules/auth/auth.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const JWT_ALGORITHM = 'HS256';
export const JWT_EXPIRED = '2d';
14 changes: 14 additions & 0 deletions src/shared/modules/auth/auth.container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Container } from 'inversify';
import { AuthService } from './auth-service.interface.js';
import { Component } from '../../types/index.js';
import { DefaultAuthService } from './default-auth.service.js';
import { ExceptionFilter } from '../../libs/rest/index.js';
import { AuthExceptionFilter } from './auth.exception-filter.js';

export function createAuthContainer() {
const authContainer = new Container();
authContainer.bind<AuthService>(Component.AuthService).to(DefaultAuthService).inSingletonScope();
authContainer.bind<ExceptionFilter>(Component.AuthExceptionFilter).to(AuthExceptionFilter).inSingletonScope();

return authContainer;
}
28 changes: 28 additions & 0 deletions src/shared/modules/auth/auth.exception-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { inject, injectable } from 'inversify';
import { NextFunction, Request, Response } from 'express';
import { ExceptionFilter } from '../../libs/rest/index.js';
import { Component } from '../../types/index.js';
import { Logger } from '../../libs/logger/index.js';
import { BaseUserException } from './errors/index.js';

@injectable()
export class AuthExceptionFilter implements ExceptionFilter {
constructor(
@inject(Component.Logger) private readonly logger: Logger
) {
this.logger.info('Register AuthExceptionFilter');
}

public catch(error: unknown, _req: Request, res: Response, next: NextFunction): void {
if (! (error instanceof BaseUserException)) {
return next(error);
}

this.logger.error(`[AuthModule] ${error.message}`, error);
res.status(error.httpStatusCode)
.json({
type: 'AUTHORIZATION',
error: error.message,
});
}
}
55 changes: 55 additions & 0 deletions src/shared/modules/auth/default-auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { inject, injectable } from 'inversify';
import * as crypto from 'node:crypto';
import { SignJWT } from 'jose';
import { AuthService } from './auth-service.interface.js';
import { Component } from '../../types/index.js';
import { Logger } from '../../libs/logger/index.js';
import { LoginUserDto, UserEntity, UserService } from '../user/index.js';
import { TokenPayload } from './types/TokenPayload.js';
import { Config, RestSchema } from '../../libs/config/index.js';
import { UserNotFoundException, UserPasswordIncorrectException } from './errors/index.js';
import { JWT_ALGORITHM, JWT_EXPIRED } from './auth.constant.js';

@injectable()
export class DefaultAuthService implements AuthService {
constructor(
@inject(Component.Logger) private readonly logger: Logger,
@inject(Component.UserService) private readonly userService: UserService,
@inject(Component.Config) private readonly config: Config<RestSchema>,
) {}

public async authenticate(user: UserEntity): Promise<string> {
const jwtSecret = this.config.get('JWT_SECRET');
const secretKey = crypto.createSecretKey(jwtSecret, 'utf-8');
const tokenPayload: TokenPayload = {
email: user.email,
firstname: user.firstname,
lastname: user.lastname,
id: user.id,
};

this.logger.info(`Create token for ${user.email}`);
return new SignJWT(tokenPayload)
.setProtectedHeader({ alg: JWT_ALGORITHM })
.setIssuedAt()
.setExpirationTime(JWT_EXPIRED)
.sign(secretKey);
}

public async verify(dto: LoginUserDto): Promise<UserEntity> {
const user = await this.userService.findByEmail(dto.email);
if (! user) {
this.logger.warn(`User with ${dto.email} not found`);
throw new UserNotFoundException();
}

if (! user.verifyPassword(dto.password, this.config.get('SALT'))) {
this.logger.warn(`Incorrect password for ${dto.email}`);
throw new UserPasswordIncorrectException();
}

return user;
}


}
7 changes: 7 additions & 0 deletions src/shared/modules/auth/errors/base-user.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpError } from '../../../libs/rest/index.js';

export class BaseUserException extends HttpError {
constructor(httpStatusCode: number, message: string) {
super(httpStatusCode, message);
}
}
3 changes: 3 additions & 0 deletions src/shared/modules/auth/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './base-user.exception.js';
export * from './user-not-found.exception.js';
export * from './user-password-incorrect.exception.js';
8 changes: 8 additions & 0 deletions src/shared/modules/auth/errors/user-not-found.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { StatusCodes } from 'http-status-codes';
import { BaseUserException } from './base-user.exception.js';

export class UserNotFoundException extends BaseUserException {
constructor() {
super(StatusCodes.NOT_FOUND, 'User not found');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { StatusCodes } from 'http-status-codes';
import { BaseUserException } from './base-user.exception.js';

export class UserPasswordIncorrectException extends BaseUserException {
constructor() {
super(StatusCodes.UNAUTHORIZED, 'Incorrect user name or password');
}
}
4 changes: 4 additions & 0 deletions src/shared/modules/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './auth-service.interface.js';
export * from './types/TokenPayload.js';
export * from './auth.container.js';
export * from './default-auth.service.js';
6 changes: 6 additions & 0 deletions src/shared/modules/auth/types/TokenPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type TokenPayload = {
email: string;
firstname: string;
lastname: string;
id: string;
};
7 changes: 5 additions & 2 deletions src/shared/modules/category/category.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Response, Request } from 'express';
import {
BaseController,
HttpError,
HttpMethod,
HttpMethod, PrivateRouteMiddleware,
RequestQuery, ValidateDtoMiddleware,
ValidateObjectIdMiddleware,
} from '../../libs/rest/index.js';
Expand Down Expand Up @@ -33,7 +33,10 @@ export class CategoryController extends BaseController {
path: '/',
method: HttpMethod.Post,
handler: this.create,
middlewares: [new ValidateDtoMiddleware(CreateCategoryDto)]
middlewares: [
new PrivateRouteMiddleware(),
new ValidateDtoMiddleware(CreateCategoryDto)
]
});
this.addRoute({
path: '/:categoryId/offers',
Expand Down
Loading
Loading