From 12487ade47a537b76cbe65869cacbe08e5597b42 Mon Sep 17 00:00:00 2001 From: Gabriel Barbosa Date: Sun, 26 Mar 2023 23:46:09 -0300 Subject: [PATCH] refactor: basic authentication with username:password encoded by base64 --- .env.example | 3 ++- src/core/middleware/basic-authenticate.ts | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index e23c457..c34e2d5 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ -BASIC=basic_token \ No newline at end of file +USERNAME=basic_username +PASSWORD=basic_password \ No newline at end of file diff --git a/src/core/middleware/basic-authenticate.ts b/src/core/middleware/basic-authenticate.ts index 6038347..401c5b2 100644 --- a/src/core/middleware/basic-authenticate.ts +++ b/src/core/middleware/basic-authenticate.ts @@ -4,9 +4,10 @@ import { env } from 'node:process' class BasicAuthenticate implements Middleware { async handle (request: Request): Promise, Response>> { - const [type, credentials] = request.headers?.authorization?.split(' ') ?? [] + const [type, basicEncoded] = request.headers?.authorization?.split(' ') ?? [] if (type === 'Basic') { - if (credentials === env.BASIC) { + const [username, password] = Buffer.from(basicEncoded, 'base64').toString().split(':') + if (username === env.USERNAME && password === env.PASSWORD) { return right(ok({ message: 'Authenticated' })) } }