-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Karlen-ll/module8-task1
- Loading branch information
Showing
39 changed files
with
353 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ DB_NAME=six-cities | |
|
||
# UPLOAD | ||
UPLOAD_DIRECTORY=/home/node/app/upload | ||
|
||
# Auth | ||
JWT_SECRET=secret |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/shared/libs/rest/exception-filter/exception-filter.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
|
||
export interface ExceptionFilter { | ||
catch(error: Error, req: Request, res: Response, next: NextFunction): void; | ||
catch(error: Error, request: Request, response: Response, next: NextFunction): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
|
||
export interface Middleware { | ||
execute(req: Request, res: Response, next: NextFunction): void; | ||
execute(request: Request, response: Response, next: NextFunction): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { jwtVerify } from 'jose'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { createSecretKey } from 'node:crypto'; | ||
import { ENCODING } from '../../../../constants/index.js'; | ||
|
||
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' && | ||
'name' in payload && | ||
typeof payload.name === 'string' && | ||
'id' in payload && | ||
typeof payload.id === 'string' | ||
); | ||
} | ||
|
||
export class ParseTokenMiddleware implements Middleware { | ||
constructor(private readonly jwtSecret: string) {} | ||
|
||
public async execute(request: Request, _response: Response, next: NextFunction): Promise<void> { | ||
const authorizationHeader = request.headers?.authorization?.split(' '); | ||
if (!authorizationHeader) { | ||
return next(); | ||
} | ||
|
||
const [, token] = authorizationHeader; | ||
|
||
try { | ||
const { payload } = await jwtVerify(token, createSecretKey(this.jwtSecret, ENCODING)); | ||
|
||
if (isTokenPayload(payload)) { | ||
request.tokenPayload = { ...payload }; | ||
return next(); | ||
} else { | ||
throw new Error('Не корректный токен'); | ||
} | ||
} catch { | ||
return next( | ||
new HttpError(StatusCodes.UNAUTHORIZED, 'Неверный токен', 'AuthenticateMiddleware') | ||
); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/shared/libs/rest/middleware/private-route.middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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, | ||
_response: Response, | ||
next: NextFunction | ||
): Promise<void> { | ||
if (!tokenPayload) { | ||
throw new HttpError(StatusCodes.UNAUTHORIZED, 'Unauthorized', 'PrivateRouteMiddleware'); | ||
} | ||
|
||
return next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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; | ||
} |
Oops, something went wrong.