-
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 #74 from codex-team/feat/policy
feat(policy): implement policies — way for custom checks for route handlers
- Loading branch information
Showing
13 changed files
with
917 additions
and
751 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { FastifyInstance } from 'fastify'; | ||
import type AuthService from '@domain/service/auth.js'; | ||
import type Logger from '@infrastructure/logging/index.js'; | ||
import notEmpty from '@infrastructure/utils/notEmpty.js'; | ||
|
||
/** | ||
* Add middleware for resolve userId from Access Token and add it to request | ||
* | ||
* @param server - fastify instance | ||
* @param authService - auth domain service | ||
* @param logger - logger instance | ||
*/ | ||
export default function addAuthMiddleware(server: FastifyInstance, authService: AuthService, logger: typeof Logger ): void { | ||
/** | ||
* Default userId value — null | ||
*/ | ||
server.decorateRequest('userId', null); | ||
|
||
/** | ||
* Resolve userId from Access Token on each request | ||
*/ | ||
server.addHook('preHandler', (request, _reply, done) => { | ||
const authorizationHeader = request.headers.authorization; | ||
|
||
if (notEmpty(authorizationHeader)) { | ||
const token = authorizationHeader.replace('Bearer ', ''); | ||
|
||
try { | ||
request.userId = authService.verifyAccessToken(token)['id']; | ||
} catch (error) { | ||
logger.error('Invalid Access Token'); | ||
logger.error(error); | ||
} | ||
} | ||
done(); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { FastifyReply, FastifyRequest } from 'fastify'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
|
||
/** | ||
* Policy to enforce user to be logged in | ||
* | ||
* @param request - Fastify request object | ||
* @param reply - Fastify reply object | ||
*/ | ||
export default async function authRequired(request: FastifyRequest, reply: FastifyReply): Promise<void> { | ||
const { userId } = request; | ||
|
||
if (userId === null) { | ||
await reply | ||
.code(StatusCodes.UNAUTHORIZED) | ||
.send({ | ||
message: 'Permission denied', | ||
}); | ||
} | ||
} |
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 @@ | ||
import authRequired from './authRequired.js'; | ||
|
||
export default { | ||
authRequired, | ||
}; |
Oops, something went wrong.