From 1671eb9bdc4c24bc243fd16aa708c14762a871a4 Mon Sep 17 00:00:00 2001 From: Kallesh B S Date: Thu, 26 Jun 2025 10:48:38 +0530 Subject: [PATCH] domain added --- src/auth/auth.service.ts | 12 ++++++------ src/guards/jwt-auth.guard.ts | 23 ++++++++++++++--------- src/main.ts | 8 +++++++- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 650a92a..b6ff11e 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -204,14 +204,14 @@ export class AuthService { } } - async introspectToken(token: string, publicKey: string): Promise { + async introspectToken(token: string): Promise { try { // Securely verify the token - const decoded = jwt.verify(token, publicKey, { algorithms: ['RS256'], }) as DecodedToken; + const decoded = jwt.decode(token) as DecodedToken; const { email } = decoded; - if (!email) throw new UnauthorizedException('Token does not contain an email.'); + if (!email) throw new UnauthorizedException('Authentication failed'); // const uid = await this.getUserIdByEmail(email); @@ -234,8 +234,8 @@ export class AuthService { } catch (error: any) { console.log("Error in introspectToken:", error.message || error); - if (error instanceof UnauthorizedException || error instanceof jwt.TokenExpiredError || error instanceof jwt.JsonWebTokenError || error instanceof jwt.NotBeforeError) { - throw new UnauthorizedException('Authentication failed'); + if (error instanceof UnauthorizedException) { + throw error; } throw new InternalServerErrorException('Failed to introspect token'); @@ -280,7 +280,7 @@ export class AuthService { const publicKey = await this.getPublicKey(kid); - const introspection = await this.introspectToken(token, publicKey); + const introspection = await this.introspectToken(token); if (!introspection.active) { console.log("Introspect failed for token"); diff --git a/src/guards/jwt-auth.guard.ts b/src/guards/jwt-auth.guard.ts index 794ca52..9df100a 100644 --- a/src/guards/jwt-auth.guard.ts +++ b/src/guards/jwt-auth.guard.ts @@ -25,18 +25,18 @@ export class JwtAuthGuard implements CanActivate { } // Decode the token without verifying the signature - const decoded = jwt.decode(token) as { exp?: number }; + // const decoded = jwt.decode(token) as { exp?: number }; - if (!decoded || !decoded.exp) { - throw new UnauthorizedException('Authentication failed'); - } + // if (!decoded || !decoded.exp) { + // throw new UnauthorizedException('Authentication failed'); + // } - const currentTime = Math.floor(Date.now() / 1000); // in seconds + // const currentTime = Math.floor(Date.now() / 1000); // in seconds - if (decoded.exp < currentTime) { - console.log('Token expired'); - throw new UnauthorizedException('Invalid Token'); - } + // if (decoded.exp < currentTime) { + // console.log('Token expired'); + // throw new UnauthorizedException('Invalid Token'); + // } // Token is valid and not expired - continue with your normal logic try { @@ -44,9 +44,14 @@ export class JwtAuthGuard implements CanActivate { request.user = verifiedUser; return true; } catch (error) { + if (error instanceof jwt.TokenExpiredError || error instanceof jwt.JsonWebTokenError || error instanceof jwt.NotBeforeError) { + throw new UnauthorizedException('Invalid Token'); + } + if (error instanceof UnauthorizedException || error instanceof UE) { throw error; } + throw new InternalServerException(); } } diff --git a/src/main.ts b/src/main.ts index fc815fe..2b0d996 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,7 +14,13 @@ async function bootstrap() { logger: ['error', 'warn'], cors: { // origin: 'https://dev.alphaomegainfosys.com/', - origin: ['http://localhost:3000', 'http://localhost:5173'], + origin: [ + 'http://localhost:3000', + 'http://localhost:5173', + 'https://policy.alphaomegainfosys.com', + 'https://sp.alphaomegainfosys.com', + 'https://client.alphaomegainfosys.com' + ], methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', preflightContinue: false, optionsSuccessStatus: 204,