From 2d7bbc997b8031c7d6abfff0811fa8ef89a2a963 Mon Sep 17 00:00:00 2001 From: Le Huu Hoan Date: Thu, 1 Aug 2024 14:51:52 +0700 Subject: [PATCH] update authentication request --- .env.example | 3 +++ .github/workflows/node.js.yml | 7 ++++++- apps/server/.env.example | 5 ++++- apps/server/src/routes/crawl.route.ts | 3 ++- apps/server/src/shared/middleware-github.ts | 21 +++++++++++++++++++++ docker-compose.yaml | 1 + 6 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 apps/server/src/shared/middleware-github.ts diff --git a/.env.example b/.env.example index ad862a3..1498a9e 100644 --- a/.env.example +++ b/.env.example @@ -12,6 +12,9 @@ TYPEORM_DATABASE= ## LOG LOG_DIR=logs +## CRAWL +AUTH_TOKEN= + # FRONTEND FE_PORT=3000 NEXT_PUBLIC_API_URL=api \ No newline at end of file diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index dff956f..bd08a7a 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -7,6 +7,9 @@ on: push: branches: - demo +env: + AUTH_TOKEN: ${{ vars.AUTH_TOKEN }} + jobs: handle-commit: runs-on: ubuntu-latest @@ -47,4 +50,6 @@ jobs: run: | echo $output echo "$output" - curl "$output" + curl --request GET \ + --url "$output" \ + --header "Authorization: Bearer $AUTH_TOKEN" diff --git a/apps/server/.env.example b/apps/server/.env.example index 697544d..e8a1afb 100644 --- a/apps/server/.env.example +++ b/apps/server/.env.example @@ -9,4 +9,7 @@ TYPEORM_PASSWORD= TYPEORM_DATABASE= ## logging -LOG_DIR= \ No newline at end of file +LOG_DIR= + +## Crawl +AUTH_TOKEN= \ No newline at end of file diff --git a/apps/server/src/routes/crawl.route.ts b/apps/server/src/routes/crawl.route.ts index 53b89ad..a908346 100644 --- a/apps/server/src/routes/crawl.route.ts +++ b/apps/server/src/routes/crawl.route.ts @@ -1,5 +1,6 @@ import { CrawlController } from '@/controllers/crawl.controller' import { Routes } from '@/decorator/routes' +import { authenticationGithub } from '@/shared/middleware-github' import { Router } from 'express' export class CrawlRoute implements Routes { @@ -12,6 +13,6 @@ export class CrawlRoute implements Routes { } private initializeRoute() { - this.router.get('/', this.crawlController.crawl) + this.router.get('/', authenticationGithub, this.crawlController.crawl) } } diff --git a/apps/server/src/shared/middleware-github.ts b/apps/server/src/shared/middleware-github.ts new file mode 100644 index 0000000..b1fb723 --- /dev/null +++ b/apps/server/src/shared/middleware-github.ts @@ -0,0 +1,21 @@ +import { NextFunction, Request, Response } from 'express' + +function authenticationGithub(req: Request, res: Response, next: NextFunction) { + try { + const token = (req.headers.authorization ?? '').split(' ')[1] + if (token === process.env.AUTH_TOKEN) { + next() + } + return res.status(403).send({ + code: 403, + message: 'Token is invalid!', + }) + } catch (error) { + return res.status(403).send({ + code: 500, + message: error?.message ?? 'Something went wrong', + }) + } +} + +export { authenticationGithub } diff --git a/docker-compose.yaml b/docker-compose.yaml index e945bc3..b673afc 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -28,6 +28,7 @@ services: - TYPEORM_PASSWORD=${TYPEORM_PASSWORD} - TYPEORM_DATABASE=${TYPEORM_DATABASE} - LOG_DIR=${LOG_DIR} + - AUTH_TOKEN=${AUTH_TOKEN} - NODE_ENV=${NODE_ENV} ports: - ${PORT}:${PORT}