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

@@ -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

View File

@@ -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),
);
}

View File

@@ -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;
}),