From 465ea2b4e0cd43eeb11178609cf4a223538673dd Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Fri, 7 Jun 2024 12:08:35 +0530 Subject: [PATCH] chore: changed the returned status type for errors in the access-tokens (#4107) * chore: changed the returned status type for errors in the access-token interceptor * chore: removed unused pagination args --- docker-compose.yml | 6 +++--- packages/hoppscotch-backend/src/errors.ts | 4 ++-- .../hoppscotch-backend/src/guards/rest-pat-auth.guard.ts | 8 ++++---- .../src/interceptors/access-token.interceptor.ts | 8 +++++--- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index b17c726a8..14efa4527 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -100,7 +100,7 @@ services: test: [ "CMD-SHELL", - "sh -c 'pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}'" + "sh -c 'pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}'", ] interval: 5s timeout: 5s @@ -112,13 +112,13 @@ services: build: dockerfile: packages/hoppscotch-backend/Dockerfile context: . - target: prod + target: dev env_file: - ./.env restart: always environment: # Edit the below line to match your PostgresDB URL if you have an outside DB (make sure to update the .env file as well) - # - DATABASE_URL=postgresql://postgres:testpass@hoppscotch-db:5432/hoppscotch?connect_timeout=300 + - DATABASE_URL=postgresql://postgres:testpass@hoppscotch-db:5432/hoppscotch?connect_timeout=300 - PORT=3000 volumes: # Uncomment the line below when modifying code. Only applicable when using the "dev" target. diff --git a/packages/hoppscotch-backend/src/errors.ts b/packages/hoppscotch-backend/src/errors.ts index 3c7725ae8..56ecd2238 100644 --- a/packages/hoppscotch-backend/src/errors.ts +++ b/packages/hoppscotch-backend/src/errors.ts @@ -784,13 +784,13 @@ export const ACCESS_TOKEN_NOT_FOUND = 'access_token/access_token_not_found'; * AccessTokens is expired * (AccessTokenService) */ -export const ACCESS_TOKENS_EXPIRED = 'TOKEN_EXPIRED'; +export const ACCESS_TOKEN_EXPIRED = 'TOKEN_EXPIRED'; /** * AccessTokens is invalid * (AccessTokenService) */ -export const ACCESS_TOKENS_INVALID = 'TOKEN_INVALID'; +export const ACCESS_TOKEN_INVALID = 'TOKEN_INVALID'; /** * AccessTokens is invalid diff --git a/packages/hoppscotch-backend/src/guards/rest-pat-auth.guard.ts b/packages/hoppscotch-backend/src/guards/rest-pat-auth.guard.ts index 8c65f3072..d59e77af6 100644 --- a/packages/hoppscotch-backend/src/guards/rest-pat-auth.guard.ts +++ b/packages/hoppscotch-backend/src/guards/rest-pat-auth.guard.ts @@ -8,7 +8,7 @@ import { Request } from 'express'; import { AccessTokenService } from 'src/access-token/access-token.service'; import * as E from 'fp-ts/Either'; import { DateTime } from 'luxon'; -import { ACCESS_TOKENS_EXPIRED, ACCESS_TOKENS_INVALID } from 'src/errors'; +import { ACCESS_TOKEN_EXPIRED, ACCESS_TOKEN_INVALID } from 'src/errors'; import { createCLIErrorResponse } from 'src/access-token/helper'; @Injectable() export class PATAuthGuard implements CanActivate { @@ -19,14 +19,14 @@ export class PATAuthGuard implements CanActivate { const token = this.extractTokenFromHeader(request); if (!token) { throw new BadRequestException( - createCLIErrorResponse(ACCESS_TOKENS_INVALID), + createCLIErrorResponse(ACCESS_TOKEN_INVALID), ); } const userAccessToken = await this.accessTokenService.getUserPAT(token); if (E.isLeft(userAccessToken)) throw new BadRequestException( - createCLIErrorResponse(ACCESS_TOKENS_INVALID), + createCLIErrorResponse(ACCESS_TOKEN_INVALID), ); request.user = userAccessToken.right.user; @@ -37,7 +37,7 @@ export class PATAuthGuard implements CanActivate { if (accessToken.expiresOn.toISOString() > today) return true; throw new BadRequestException( - createCLIErrorResponse(ACCESS_TOKENS_EXPIRED), + createCLIErrorResponse(ACCESS_TOKEN_EXPIRED), ); } diff --git a/packages/hoppscotch-backend/src/interceptors/access-token.interceptor.ts b/packages/hoppscotch-backend/src/interceptors/access-token.interceptor.ts index 23433e918..b9e536094 100644 --- a/packages/hoppscotch-backend/src/interceptors/access-token.interceptor.ts +++ b/packages/hoppscotch-backend/src/interceptors/access-token.interceptor.ts @@ -1,13 +1,14 @@ import { + BadRequestException, CallHandler, ExecutionContext, Injectable, NestInterceptor, - UnauthorizedException, } from '@nestjs/common'; import { Observable, map } from 'rxjs'; import { AccessTokenService } from 'src/access-token/access-token.service'; import * as E from 'fp-ts/Either'; +import { ACCESS_TOKEN_NOT_FOUND } from 'src/errors'; @Injectable() export class AccessTokenInterceptor implements NestInterceptor { @@ -18,14 +19,15 @@ export class AccessTokenInterceptor implements NestInterceptor { const authHeader = req.headers.authorization; const token = authHeader && authHeader.split(' ')[1]; if (!token) { - throw new UnauthorizedException(); + throw new BadRequestException(ACCESS_TOKEN_NOT_FOUND); } return handler.handle().pipe( map(async (data) => { const userAccessToken = await this.accessTokenService.updateLastUsedForPAT(token); - if (E.isLeft(userAccessToken)) throw new UnauthorizedException(); + if (E.isLeft(userAccessToken)) + throw new BadRequestException(userAccessToken.left); return data; }),