-
Notifications
You must be signed in to change notification settings - Fork 0
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 #33 from BinaryStudioAcademy/task/OV-8-protect-rou…
…ting OV-8: protect routing
- Loading branch information
Showing
21 changed files
with
141 additions
and
8 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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { USER_PASSWORD_SALT_ROUNDS } from './user.constants.js'; | ||
export { WHITE_ROUTES } from './white-routes.constants.js'; |
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,14 @@ | ||
import { ApiPath, AuthApiPath } from 'shared'; | ||
|
||
const WHITE_ROUTES = [ | ||
{ | ||
path: `/api/v1${ApiPath.AUTH}${AuthApiPath.SIGN_IN}`, | ||
method: 'POST', | ||
}, | ||
{ | ||
path: `/api/v1${ApiPath.AUTH}${AuthApiPath.SIGN_UP}`, | ||
method: 'POST', | ||
}, | ||
]; | ||
|
||
export { WHITE_ROUTES }; |
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,58 @@ | ||
import fp from 'fastify-plugin'; | ||
import { HttpCode, HttpError, HttpHeader } from 'shared'; | ||
|
||
import { userService } from '~/bundles/users/users.js'; | ||
import { tokenService } from '~/common/services/services.js'; | ||
|
||
import { ErrorMessage, Hook } from './enums/enums.js'; | ||
import { type Route } from './types/types.js'; | ||
import { isRouteInWhiteList } from './utils/utils.js'; | ||
|
||
type Options = { | ||
routesWhiteList: Route[]; | ||
}; | ||
|
||
const authenticateJWT = fp<Options>((fastify, { routesWhiteList }, done) => { | ||
fastify.decorateRequest('user', null); | ||
|
||
fastify.addHook(Hook.PRE_HANDLER, async (request) => { | ||
if (isRouteInWhiteList(routesWhiteList, request)) { | ||
return; | ||
} | ||
|
||
const authHeader = request.headers[HttpHeader.AUTHORIZATION]; | ||
|
||
if (!authHeader) { | ||
throw new HttpError({ | ||
message: ErrorMessage.MISSING_TOKEN, | ||
status: HttpCode.UNAUTHORIZED, | ||
}); | ||
} | ||
|
||
const [, token] = authHeader.split(' '); | ||
|
||
const userId = await tokenService.getUserIdFromToken(token as string); | ||
|
||
if (!userId) { | ||
throw new HttpError({ | ||
message: ErrorMessage.INVALID_TOKEN, | ||
status: HttpCode.UNAUTHORIZED, | ||
}); | ||
} | ||
|
||
const user = await userService.findById(userId); | ||
|
||
if (!user) { | ||
throw new HttpError({ | ||
message: ErrorMessage.MISSING_USER, | ||
status: HttpCode.BAD_REQUEST, | ||
}); | ||
} | ||
|
||
request.user = user; | ||
}); | ||
|
||
done(); | ||
}); | ||
|
||
export { authenticateJWT }; |
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,2 @@ | ||
export { ErrorMessage } from './error-message.enum.js'; | ||
export { Hook } from './hook.enum.js'; |
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,7 @@ | ||
const ErrorMessage = { | ||
MISSING_TOKEN: 'You are not logged in', | ||
INVALID_TOKEN: 'Token is no longer valid. Please log in again.', | ||
MISSING_USER: 'User with this id does not exist.', | ||
} as const; | ||
|
||
export { ErrorMessage }; |
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,5 @@ | ||
const Hook = { | ||
PRE_HANDLER: 'preHandler', | ||
} as const; | ||
|
||
export { Hook }; |
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 @@ | ||
type Route = { | ||
path: string; | ||
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; | ||
}; | ||
|
||
export { type Route }; |
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 @@ | ||
export { type Route } from './route.type.js'; |
15 changes: 15 additions & 0 deletions
15
backend/src/common/plugins/auth/utils/check-white-routes.util.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,15 @@ | ||
import { type FastifyRequest } from 'fastify'; | ||
|
||
import { type Route } from '../types/types.js'; | ||
|
||
const isRouteInWhiteList = ( | ||
routesWhiteList: Route[], | ||
request: FastifyRequest, | ||
): boolean => { | ||
return routesWhiteList.some( | ||
(route) => | ||
route.path === request.url && route.method === request.method, | ||
); | ||
}; | ||
|
||
export { isRouteInWhiteList }; |
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 @@ | ||
export { isRouteInWhiteList } from './check-white-routes.util.js'; |
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 @@ | ||
export { authenticateJWT } from './auth/auth-jwt.plugin.js'; |
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,9 @@ | ||
import 'fastify'; | ||
|
||
import { type UserEntity } from '~/bundles/users/users.js'; | ||
|
||
declare module 'fastify' { | ||
interface FastifyRequest { | ||
user: 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
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
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