Skip to content

Commit f6cce65

Browse files
author
Nicolas MACHEREY
committed
Fix bad injection in async options
1 parent 8737099 commit f6cce65

9 files changed

+119
-82
lines changed

lib/app/oauth2-core.module.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export class Oauth2CoreModule implements OnModuleInit {
127127
const providers: Provider[] = this.createAsyncProviders(options);
128128

129129
const userLoaderProvider = {
130-
imports: options.imports,
131130
provide: 'UserLoaderInterface',
132131
useFactory: async (options) => {
133132
return options.userLoader;
@@ -136,7 +135,6 @@ export class Oauth2CoreModule implements OnModuleInit {
136135
};
137136

138137
const userValidatorProvider = {
139-
imports: options.imports,
140138
provide: 'UserValidatorInterface',
141139
useFactory: async (options) => {
142140
return options.userValidator;
@@ -147,7 +145,7 @@ export class Oauth2CoreModule implements OnModuleInit {
147145
return {
148146
module: Oauth2CoreModule,
149147
imports: [
150-
...options.imports,
148+
...(options.imports || []),
151149
CqrsModule,
152150
TypeOrmModule.forFeature([
153151
ClientEntity,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@switchit/nestjs-oauth2-server",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "NestJS oauth2 server by SWITCH IT CONSULTING",
55
"author": "Switch IT - Consulting",
66
"license": "MIT",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Controller, Get, UseGuards} from "@nestjs/common";
2+
import {AuthGuard} from "@nestjs/passport";
3+
4+
@Controller('oauth2-secured')
5+
export class TestSecuredController {
6+
@Get('me')
7+
@UseGuards(AuthGuard('access-token'))
8+
async auth(): Promise<any> {
9+
return {message: 'hello'};
10+
}
11+
}

test/e2e/mock-user/user.loader.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Injectable} from "@nestjs/common";
2+
import {UserInterface, UserLoaderInterface} from "../../../lib/domain/interface";
3+
import {InvalidUserException} from "../../../lib/domain/exception";
4+
import {users} from "./users";
5+
6+
7+
@Injectable()
8+
export class UserLoader implements UserLoaderInterface {
9+
async load(userId: string): Promise<UserInterface> {
10+
if (users[userId] !== undefined) {
11+
return {
12+
id: users[userId],
13+
username: users[userId],
14+
email: users[userId],
15+
}
16+
}
17+
18+
throw InvalidUserException.withId(userId);
19+
}
20+
}

test/e2e/mock-user/user.module.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Module} from "@nestjs/common";
2+
import {UserValidator} from "./user.validator";
3+
import {UserLoader} from "./user.loader";
4+
5+
@Module({
6+
providers: [
7+
UserValidator,
8+
UserLoader,
9+
],
10+
exports: [
11+
UserValidator,
12+
UserLoader,
13+
]
14+
})
15+
export class UserModule {
16+
}

test/e2e/mock-user/user.validator.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Injectable} from "@nestjs/common";
2+
import {UserInterface, UserValidatorInterface} from "../../../lib/domain/interface";
3+
import {InvalidUserException} from "../../../lib/domain/exception";
4+
import {users} from "./users";
5+
6+
@Injectable()
7+
export class UserValidator implements UserValidatorInterface {
8+
async validate(username, password): Promise<UserInterface> {
9+
if (users[username] !== undefined && users[username] === password) {
10+
return {
11+
id: users[username],
12+
username: users[username],
13+
email: users[username],
14+
}
15+
}
16+
17+
throw InvalidUserException.withUsernameAndPassword(username, password);
18+
}
19+
}

test/e2e/mock-user/users.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const users: {[s:string]: string} = {
2+
'alice@change.me': 'alice',
3+
'bob@change.me': 'bob',
4+
'kyle@change.me': 'kyle',
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {Oauth2Module} from "../../../lib/app";
2+
import {Module} from "@nestjs/common";
3+
import {UserValidator} from "../mock-user/user.validator";
4+
import {UserLoader} from "../mock-user/user.loader";
5+
import {TypeOrmModule} from "@nestjs/typeorm";
6+
import {FixturesLoaderService} from "../fixtures-loader.service";
7+
import {TestSecuredController} from "../controller/test-secure.controller";
8+
import {UserModule} from "../mock-user/user.module";
9+
10+
@Module({
11+
imports: [
12+
TypeOrmModule.forRoot({
13+
type: 'postgres',
14+
host: 'localhost',
15+
port: 5432,
16+
username: 'postgres',
17+
password: 'postgres',
18+
database: 'oauth2-server',
19+
entities: [process.cwd() + '/lib/**/*.entity{.ts,.js}'],
20+
dropSchema: true,
21+
synchronize: true
22+
}),
23+
Oauth2Module.forRootAsync({
24+
imports: [UserModule],
25+
useFactory: (userValidator, userLoader) => ({
26+
userValidator,
27+
userLoader,
28+
}),
29+
inject: [UserValidator, UserLoader],
30+
}),
31+
],
32+
providers: [
33+
FixturesLoaderService,
34+
],
35+
controllers: [
36+
TestSecuredController,
37+
]
38+
})
39+
export class Oauth2AsyncUseFactoryModule {}

test/e2e/oauth2.controller.e2e-spec.ts test/e2e/oauth2-async-use-factory.e2e-spec.ts

+7-78
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,18 @@
11
import * as request from 'supertest';
2-
import {Controller, Get, INestApplication, Injectable, UseGuards, ValidationPipe} from "@nestjs/common";
3-
import {Test, TestingModule} from "@nestjs/testing";
4-
import {AuthGuard} from '@nestjs/passport';
2+
import {INestApplication, ValidationPipe} from "@nestjs/common";
53
import {FixturesLoaderService} from "./fixtures-loader.service";
6-
import {Oauth2Module} from "../../lib/app/oauth2.module";
7-
import {TypeOrmModule} from "@nestjs/typeorm";
8-
import {UserLoaderInterface, UserValidatorInterface} from "../../lib/domain/interface";
9-
import {InvalidUserException, UserInterface} from "../../lib/domain";
4+
import {Oauth2AsyncUseFactoryModule} from "./modules/oauth2-async-use-factory.module";
5+
import {Test} from "@nestjs/testing";
106

11-
@Controller('oauth2-secured')
12-
export class TestSecuredController {
13-
@Get('me')
14-
@UseGuards(AuthGuard('access-token'))
15-
async auth(): Promise<any> {
16-
return {message: 'hello'};
17-
}
18-
}
19-
20-
const users: {[s:string]: string} = {
21-
'alice@change.me': 'alice',
22-
'bob@change.me': 'bob',
23-
'kyle@change.me': 'kyle',
24-
};
25-
26-
@Injectable()
27-
export class UserValidator implements UserValidatorInterface {
28-
async validate(username, password): Promise<UserInterface> {
29-
if (users[username] !== undefined && users[username] === password) {
30-
return {
31-
id: users[username],
32-
username: users[username],
33-
email: users[username],
34-
}
35-
}
36-
37-
throw InvalidUserException.withUsernameAndPassword(username, password);
38-
}
39-
}
40-
41-
@Injectable()
42-
export class UserLoader implements UserLoaderInterface {
43-
async load(userId: string): Promise<UserInterface> {
44-
if (users[userId] !== undefined) {
45-
return {
46-
id: users[userId],
47-
username: users[userId],
48-
email: users[userId],
49-
}
50-
}
51-
52-
throw InvalidUserException.withId(userId);
53-
}
54-
}
55-
56-
describe('OAuth2 Controller (e2e)', () => {
7+
describe('OAuth2 Async Module Use Factory Controller (e2e)', () => {
578
let app: INestApplication;
589

5910
beforeEach(async () => {
60-
const moduleFixture: TestingModule = await Test.createTestingModule({
61-
imports: [
62-
TypeOrmModule.forRoot({
63-
type: 'postgres',
64-
host: 'localhost',
65-
port: 5432,
66-
username: 'postgres',
67-
password: 'postgres',
68-
database: 'oauth2-server',
69-
entities: [process.cwd() + '/lib/**/*.entity{.ts,.js}'],
70-
dropSchema: true,
71-
synchronize: true
72-
}),
73-
Oauth2Module.forRoot({
74-
userValidator: new UserValidator(),
75-
userLoader: new UserLoader(),
76-
})
77-
],
78-
providers: [
79-
FixturesLoaderService,
80-
],
81-
controllers: [
82-
TestSecuredController,
83-
],
11+
const module = await Test.createTestingModule({
12+
imports: [Oauth2AsyncUseFactoryModule],
8413
}).compile();
8514

86-
app = moduleFixture.createNestApplication();
15+
app = module.createNestApplication();
8716
app.useGlobalPipes(new ValidationPipe({
8817
transform: true,
8918
}));

0 commit comments

Comments
 (0)