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
This commit is contained in:
Balu Babu
2024-06-07 12:08:35 +05:30
committed by GitHub
parent 4bd23a8f4c
commit 465ea2b4e0
4 changed files with 14 additions and 12 deletions

View File

@@ -100,7 +100,7 @@ services:
test: test:
[ [
"CMD-SHELL", "CMD-SHELL",
"sh -c 'pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}'" "sh -c 'pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}'",
] ]
interval: 5s interval: 5s
timeout: 5s timeout: 5s
@@ -112,13 +112,13 @@ services:
build: build:
dockerfile: packages/hoppscotch-backend/Dockerfile dockerfile: packages/hoppscotch-backend/Dockerfile
context: . context: .
target: prod target: dev
env_file: env_file:
- ./.env - ./.env
restart: always restart: always
environment: 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) # 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 - PORT=3000
volumes: volumes:
# Uncomment the line below when modifying code. Only applicable when using the "dev" target. # Uncomment the line below when modifying code. Only applicable when using the "dev" target.

View File

@@ -784,13 +784,13 @@ export const ACCESS_TOKEN_NOT_FOUND = 'access_token/access_token_not_found';
* AccessTokens is expired * AccessTokens is expired
* (AccessTokenService) * (AccessTokenService)
*/ */
export const ACCESS_TOKENS_EXPIRED = 'TOKEN_EXPIRED'; export const ACCESS_TOKEN_EXPIRED = 'TOKEN_EXPIRED';
/** /**
* AccessTokens is invalid * AccessTokens is invalid
* (AccessTokenService) * (AccessTokenService)
*/ */
export const ACCESS_TOKENS_INVALID = 'TOKEN_INVALID'; export const ACCESS_TOKEN_INVALID = 'TOKEN_INVALID';
/** /**
* AccessTokens is invalid * AccessTokens is invalid

View File

@@ -8,7 +8,7 @@ import { Request } from 'express';
import { AccessTokenService } from 'src/access-token/access-token.service'; import { AccessTokenService } from 'src/access-token/access-token.service';
import * as E from 'fp-ts/Either'; import * as E from 'fp-ts/Either';
import { DateTime } from 'luxon'; 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'; import { createCLIErrorResponse } from 'src/access-token/helper';
@Injectable() @Injectable()
export class PATAuthGuard implements CanActivate { export class PATAuthGuard implements CanActivate {
@@ -19,14 +19,14 @@ export class PATAuthGuard implements CanActivate {
const token = this.extractTokenFromHeader(request); const token = this.extractTokenFromHeader(request);
if (!token) { if (!token) {
throw new BadRequestException( throw new BadRequestException(
createCLIErrorResponse(ACCESS_TOKENS_INVALID), createCLIErrorResponse(ACCESS_TOKEN_INVALID),
); );
} }
const userAccessToken = await this.accessTokenService.getUserPAT(token); const userAccessToken = await this.accessTokenService.getUserPAT(token);
if (E.isLeft(userAccessToken)) if (E.isLeft(userAccessToken))
throw new BadRequestException( throw new BadRequestException(
createCLIErrorResponse(ACCESS_TOKENS_INVALID), createCLIErrorResponse(ACCESS_TOKEN_INVALID),
); );
request.user = userAccessToken.right.user; request.user = userAccessToken.right.user;
@@ -37,7 +37,7 @@ export class PATAuthGuard implements CanActivate {
if (accessToken.expiresOn.toISOString() > today) return true; if (accessToken.expiresOn.toISOString() > today) return true;
throw new BadRequestException( throw new BadRequestException(
createCLIErrorResponse(ACCESS_TOKENS_EXPIRED), createCLIErrorResponse(ACCESS_TOKEN_EXPIRED),
); );
} }

View File

@@ -1,13 +1,14 @@
import { import {
BadRequestException,
CallHandler, CallHandler,
ExecutionContext, ExecutionContext,
Injectable, Injectable,
NestInterceptor, NestInterceptor,
UnauthorizedException,
} from '@nestjs/common'; } from '@nestjs/common';
import { Observable, map } from 'rxjs'; import { Observable, map } from 'rxjs';
import { AccessTokenService } from 'src/access-token/access-token.service'; import { AccessTokenService } from 'src/access-token/access-token.service';
import * as E from 'fp-ts/Either'; import * as E from 'fp-ts/Either';
import { ACCESS_TOKEN_NOT_FOUND } from 'src/errors';
@Injectable() @Injectable()
export class AccessTokenInterceptor implements NestInterceptor { export class AccessTokenInterceptor implements NestInterceptor {
@@ -18,14 +19,15 @@ export class AccessTokenInterceptor implements NestInterceptor {
const authHeader = req.headers.authorization; const authHeader = req.headers.authorization;
const token = authHeader && authHeader.split(' ')[1]; const token = authHeader && authHeader.split(' ')[1];
if (!token) { if (!token) {
throw new UnauthorizedException(); throw new BadRequestException(ACCESS_TOKEN_NOT_FOUND);
} }
return handler.handle().pipe( return handler.handle().pipe(
map(async (data) => { map(async (data) => {
const userAccessToken = const userAccessToken =
await this.accessTokenService.updateLastUsedForPAT(token); await this.accessTokenService.updateLastUsedForPAT(token);
if (E.isLeft(userAccessToken)) throw new UnauthorizedException(); if (E.isLeft(userAccessToken))
throw new BadRequestException(userAccessToken.left);
return data; return data;
}), }),