Skip to content

Commit

Permalink
refactor: basic authentication with username:password encoded by base64
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbsx committed Mar 27, 2023
1 parent e6f63ea commit 12487ad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
BASIC=basic_token
USERNAME=basic_username
PASSWORD=basic_password
5 changes: 3 additions & 2 deletions src/core/middleware/basic-authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { env } from 'node:process'

class BasicAuthenticate implements Middleware {
async handle (request: Request): Promise<Either<Response<Error>, 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' }))
}
}
Expand Down

0 comments on commit 12487ad

Please sign in to comment.