Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware to pass the auth token #18

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/apps/human-app/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { JobsDiscoveryModule } from './modules/jobs-discovery/jobs-discovery.mod
import { JobsDiscoveryController } from './modules/jobs-discovery/jobs-discovery.controller';
import { JobAssignmentController } from './modules/job-assignment/job-assignment.controller';
import { JobAssignmentModule } from './modules/job-assignment/job-assignment.module';
import { RequestContext } from './common/utils/request-context.util';

@Module({
imports: [
Expand All @@ -41,7 +42,7 @@ import { JobAssignmentModule } from './modules/job-assignment/job-assignment.mod
JobsDiscoveryController,
JobAssignmentController,
],
providers: [],
providers: [RequestContext],
exports: [HttpModule],
})
export class AppModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { RequestContext } from '../utils/request-context.util';
import { Injectable, NestMiddleware } from '@nestjs/common';

@Injectable()
export class TokenMiddleware implements NestMiddleware {
constructor(private readonly requestContext: RequestContext) {}

use(req: any, res: any, next: () => void) {
const authorization = req.headers['authorization'];
this.requestContext.token = authorization;
next();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Injectable, Scope } from '@nestjs/common';

@Injectable({ scope: Scope.REQUEST })
export class RequestContext {
token: string;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Module } from '@nestjs/common';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { JobAssignmentService } from './job-assignment.service';
import { JobAssignmentProfile } from './job-assignment.mapper';
import { HttpModule } from '@nestjs/axios';
import { TokenMiddleware } from '../../common/interceptors/auth-token.middleware';
import { JobAssignmentController } from './job-assignment.controller';
import { RequestContext } from '../../common/utils/request-context.util';

@Module({
imports: [HttpModule],
providers: [JobAssignmentService, JobAssignmentProfile],
providers: [JobAssignmentService, JobAssignmentProfile, RequestContext],
exports: [JobAssignmentService],
})
export class JobAssignmentModule {}
export class JobAssignmentModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(TokenMiddleware).forRoutes(JobAssignmentController);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import {
import { HttpService } from '@nestjs/axios';
import { InjectMapper } from '@automapper/nestjs';
import { Mapper } from '@automapper/core';
import { RequestContext } from '../../common/utils/request-context.util';

@Injectable()
export class JobAssignmentService {
constructor(
public httpService: HttpService,
@InjectMapper() private readonly mapper: Mapper,
private readonly requestContext: RequestContext,
) {}

async processJobAssignment(
Expand All @@ -29,10 +31,12 @@ export class JobAssignmentService {
);
try {
const url = jobAssignmentData.exchange_oracle_url;
const token = this.requestContext.token;
const options = {
method: 'POST',
url: `${url}/assignment`,
data: jobAssignmentData,
headers: { Authorization: `Bearer ${token}` },
};
const response = await lastValueFrom(this.httpService.request(options));
return response.data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Module } from '@nestjs/common';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { JobsDiscoveryService } from './jobs-discovery.service';
import { JobsDiscoveryProfile } from './jobs-discovery.mapper';
import { HttpModule } from '@nestjs/axios';
import { TokenMiddleware } from '../../common/interceptors/auth-token.middleware';
import { JobsDiscoveryController } from './jobs-discovery.controller';
import { RequestContext } from '../../common/utils/request-context.util';

@Module({
imports: [HttpModule],
providers: [JobsDiscoveryService, JobsDiscoveryProfile],
providers: [JobsDiscoveryService, JobsDiscoveryProfile, RequestContext],
exports: [JobsDiscoveryService],
})
export class JobsDiscoveryModule {}
export class JobsDiscoveryModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(TokenMiddleware).forRoutes(JobsDiscoveryController);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import {
import { HttpService } from '@nestjs/axios';
import { InjectMapper } from '@automapper/nestjs';
import { Mapper } from '@automapper/core';
import { RequestContext } from '../../common/utils/request-context.util';

@Injectable()
export class JobsDiscoveryService {
constructor(
public httpService: HttpService,
@InjectMapper() private readonly mapper: Mapper,
private readonly requestContext: RequestContext,
) {}

async processJobsDiscovery(
Expand All @@ -26,10 +28,12 @@ export class JobsDiscoveryService {
);
try {
const url = jobsDiscoveryParamsCommand.exchange_oracle_url;
const token = this.requestContext.token;
const options = {
method: 'GET',
url: `${url}/jobs`,
params: jobsDiscoveryParamsData,
headers: { Authorization: `Bearer ${token}` },
};
const response = await lastValueFrom(this.httpService.request(options));
return response.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { AutomapperModule } from '@automapper/nestjs';
import { classes } from '@automapper/classes';
import { JobsDiscoveryProfile } from '../jobs-discovery.mapper';
import { HttpService } from '@nestjs/axios';
import { TokenMiddleware } from '../../../common/interceptors/auth-token.middleware';
import { RequestContext } from '../../../common/utils/request-context.util';

describe('JobsDiscoveryController', () => {
let controller: JobsDiscoveryController;
Expand All @@ -41,6 +43,7 @@ describe('JobsDiscoveryController', () => {
),
},
},
RequestContext,
],
})
.overrideProvider(JobsDiscoveryService)
Expand Down Expand Up @@ -75,4 +78,27 @@ describe('JobsDiscoveryController', () => {
);
});
});
describe('TokenMiddleware', () => {
let middleware: TokenMiddleware;
let requestContext: RequestContext;

beforeEach(() => {
requestContext = new RequestContext();
middleware = new TokenMiddleware(requestContext);
});

it('should set token in requestContext', () => {
const mockReq = {
headers: {
authorization: 'Bearer token',
},
};

const next = jest.fn();
middleware.use(mockReq, {}, next);

expect(requestContext.token).toEqual('Bearer token');
expect(next).toHaveBeenCalled();
});
});
});
Loading