Update token verification and fix typo (#2889)
* Update token verification and fix typo * Fix typo
This commit is contained in:
+10
-33
@@ -3,16 +3,17 @@ import { GraphQLModule } from '@nestjs/graphql';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { APP_FILTER, ContextIdFactory, ModuleRef } from '@nestjs/core';
|
||||
|
||||
import { GraphQLError, GraphQLSchema } from 'graphql';
|
||||
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs';
|
||||
import GraphQLJSON from 'graphql-type-json';
|
||||
import { GraphQLError, GraphQLSchema } from 'graphql';
|
||||
import { ExtractJwt } from 'passport-jwt';
|
||||
import { TokenExpiredError, JsonWebTokenError, verify } from 'jsonwebtoken';
|
||||
import { TokenExpiredError, JsonWebTokenError } from 'jsonwebtoken';
|
||||
|
||||
import { WorkspaceFactory } from 'src/workspace/workspace.factory';
|
||||
import { TypeOrmExceptionFilter } from 'src/filters/typeorm-exception.filter';
|
||||
import { HttpExceptionFilter } from 'src/filters/http-exception.filter';
|
||||
import { GlobalExceptionFilter } from 'src/filters/global-exception.filter';
|
||||
import { TokenService } from 'src/core/auth/services/token.service';
|
||||
import { Workspace } from 'src/core/workspace/workspace.entity';
|
||||
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@@ -20,11 +21,6 @@ import { CoreModule } from './core/core.module';
|
||||
import { IntegrationsModule } from './integrations/integrations.module';
|
||||
import { HealthModule } from './health/health.module';
|
||||
import { WorkspaceModule } from './workspace/workspace.module';
|
||||
import { EnvironmentService } from './integrations/environment/environment.service';
|
||||
import {
|
||||
JwtAuthStrategy,
|
||||
JwtPayload,
|
||||
} from './core/auth/strategies/jwt.auth.strategy';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -38,38 +34,19 @@ import {
|
||||
include: [CoreModule],
|
||||
conditionalSchema: async (request) => {
|
||||
try {
|
||||
// Get the JwtAuthStrategy from the AppModule
|
||||
const jwtStrategy = AppModule.moduleRef.get(JwtAuthStrategy, {
|
||||
// Get TokenService from AppModule
|
||||
const tokenService = AppModule.moduleRef.get(TokenService, {
|
||||
strict: false,
|
||||
});
|
||||
|
||||
// Get the EnvironmentService from the AppModule
|
||||
const environmentService = AppModule.moduleRef.get(
|
||||
EnvironmentService,
|
||||
{
|
||||
strict: false,
|
||||
},
|
||||
);
|
||||
let workspace: Workspace;
|
||||
|
||||
// Extract JWT from the request
|
||||
const token = ExtractJwt.fromAuthHeaderAsBearerToken()(request.req);
|
||||
|
||||
// If there is no token return an empty schema
|
||||
if (!token) {
|
||||
try {
|
||||
workspace = await tokenService.validateToken(request.req);
|
||||
} catch (err) {
|
||||
return new GraphQLSchema({});
|
||||
}
|
||||
|
||||
// Verify and decode JWT
|
||||
const decoded = verify(
|
||||
token,
|
||||
environmentService.getAccessTokenSecret(),
|
||||
);
|
||||
|
||||
// Validate JWT
|
||||
const { workspace } = await jwtStrategy.validate(
|
||||
decoded as JwtPayload,
|
||||
);
|
||||
|
||||
const contextId = ContextIdFactory.create();
|
||||
|
||||
AppModule.moduleRef.registerRequestByContextId(request, contextId);
|
||||
|
||||
Reference in New Issue
Block a user