Skip to content

Commit 7d68d6b

Browse files
Merge pull request #10 from DISIC:dev
fix(pencil): adding logs and env variables validation with joi
2 parents 698e306 + 9edc169 commit 7d68d6b

File tree

12 files changed

+311
-66
lines changed

12 files changed

+311
-66
lines changed

package-lock.json

Lines changed: 107 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"cross-env": "^7.0.3",
4242
"dotenv": "^16.3.1",
4343
"https-proxy-agent": "^7.0.4",
44+
"joi": "^17.12.3",
4445
"moment": "^2.30.1",
4546
"nodemailer": "^6.9.11",
4647
"openid-client": "^5.6.4",
@@ -56,6 +57,7 @@
5657
"@nestjs/testing": "^10.0.0",
5758
"@types/express": "^4.17.17",
5859
"@types/jest": "^29.5.2",
60+
"@types/joi": "^17.2.3",
5961
"@types/node": "^20.3.1",
6062
"@types/supertest": "^2.0.12",
6163
"@typescript-eslint/eslint-plugin": "^6.0.0",

src/app.module.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { configValidationSchema } from './config.schema';
12
import { Module } from '@nestjs/common';
23
import { AppController } from './app.controller';
34
import { AppService } from './app.service';
@@ -13,43 +14,53 @@ import { MailerModule } from '@nestjs-modules/mailer';
1314

1415
@Module({
1516
imports: [
16-
MailerModule.forRoot({
17-
transport: {
18-
pool: process.env.EMAIL_SMTP_POOL,
19-
host: process.env.EMAIL_SMTP_HOST,
20-
port: process.env.EMAIL_SMTP_PORT,
21-
secure: process.env.EMAIL_SMTP_SECURE === 'true',
22-
// ignoneTLS: true,
23-
auth: {
24-
user: process.env.EMAIL_SMTP_AUTH_USER,
25-
pass: process.env.EMAIL_SMTP_AUTH_PASS,
26-
},
27-
tls: {
28-
// do not fail on invalid certs
29-
rejectUnauthorized:
30-
process.env.EMAIL_SMTP_TLS_REJECTUNAUTHORIZED === 'true',
31-
},
32-
},
33-
defaults: {
34-
from: '"nest-modules" <[email protected]>',
17+
MailerModule.forRootAsync({
18+
imports: [ConfigModule],
19+
inject: [ConfigService],
20+
useFactory: async (configService: ConfigService) => {
21+
return {
22+
transport: {
23+
pool: configService.get('EMAIL_SMTP_POOL'),
24+
host: configService.get('EMAIL_SMTP_HOST'),
25+
port: configService.get('EMAIL_SMTP_PORT'),
26+
secure: configService.get('EMAIL_SMTP_SECURE') === 'true',
27+
auth: {
28+
user: configService.get('EMAIL_SMTP_AUTH_USER'),
29+
pass: configService.get('EMAIL_SMTP_AUTH_PASS'),
30+
},
31+
tls: {
32+
rejectUnauthorized:
33+
configService.get('EMAIL_SMTP_TLS_REJECTUNAUTHORIZED') ===
34+
'true',
35+
},
36+
},
37+
defaults: {
38+
from: '"nest-modules" <[email protected]>',
39+
},
40+
};
3541
},
3642
}),
37-
JwtModule.register({
43+
JwtModule.registerAsync({
44+
imports: [ConfigModule],
45+
inject: [ConfigService],
46+
useFactory: async (configService: ConfigService) => {
47+
return { secret: configService.get('JITSI_JITSIJWT_SECRET') };
48+
},
3849
global: true,
39-
secret: process.env.JITSI_JITSIJWT_SECRET,
4050
}),
4151
ConfigModule.forRoot({
4252
isGlobal: true,
4353
envFilePath: `.env.${process.env.NODE_ENV}`,
54+
validationSchema: configValidationSchema,
4455
}),
4556
MongooseModule.forRootAsync({
4657
imports: [ConfigModule],
58+
inject: [ConfigService],
4759
useFactory: (configService: ConfigService) => {
4860
return {
4961
uri: configService.get('MONGO_URI'),
5062
};
5163
},
52-
inject: [ConfigService],
5364
}),
5465
AuthenticationModule,
5566
ConferenceModule,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { IsString } from 'class-validator';
3+
4+
@Injectable()
5+
export class LoginCallbackDTO {
6+
@IsString()
7+
state: string;
8+
@IsString()
9+
code: string;
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { IsString } from 'class-validator';
3+
4+
@Injectable()
5+
export class LogoutCallbackDTO {
6+
@IsString()
7+
state: string;
8+
}

src/authentication/authentication.controller.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ConfigService } from '@nestjs/config';
12
import { AuthenticationService } from './authentication.service';
23
import {
34
Controller,
@@ -14,22 +15,16 @@ import * as crypto from 'crypto';
1415
import { ConferenceService } from 'src/conference/conference.service';
1516
import { JwtService } from '@nestjs/jwt';
1617
import * as moment from 'moment';
17-
18-
interface LoginCallbackQuery {
19-
code: string;
20-
state: string;
21-
}
22-
23-
interface LogoutCallbackQuery {
24-
state: string;
25-
}
18+
import { LoginCallbackDTO } from './DTOs/LoginCallbackDTO';
19+
import { LogoutCallbackDTO } from './DTOs/LogoutCallbackDTO';
2620

2721
@Controller('authentication')
2822
export class AuthenticationController {
2923
constructor(
3024
private readonly authenticationService: AuthenticationService,
3125
private readonly conferenceService: ConferenceService,
3226
private readonly jwtService: JwtService,
27+
private readonly configService: ConfigService,
3328
) {}
3429

3530
@Get('whereami')
@@ -60,7 +55,7 @@ export class AuthenticationController {
6055

6156
@Get('login_callback')
6257
async loginCallback(
63-
@Query() query: LoginCallbackQuery,
58+
@Query() query: LoginCallbackDTO,
6459
@Req() request: Request,
6560
@Res({ passthrough: true }) response: Response,
6661
) {
@@ -71,9 +66,9 @@ export class AuthenticationController {
7166
await this.authenticationService.loginCallback(code, state, sendedState);
7267

7368
const tokenClaims = {
74-
iss: process.env.JITSI_JITSIJWT_ISS,
75-
aud: process.env.JITSI_JITSIJWT_AUD,
76-
sub: process.env.JITSI_JITSIJWT_SUB,
69+
iss: this.configService.get('JITSI_JITSIJWT_ISS'),
70+
aud: this.configService.get('JITSI_JITSIJWT_AUD'),
71+
sub: this.configService.get('JITSI_JITSIJWT_SUB'),
7772
email: this.jwtService.decode(userinfo)?.email,
7873
idToken,
7974
};
@@ -121,7 +116,7 @@ export class AuthenticationController {
121116
@Get('logout_callback')
122117
// @Redirect('', 302)
123118
logoutCallback(
124-
@Query() query: LogoutCallbackQuery,
119+
@Query() query: LogoutCallbackDTO,
125120
@Req() request: Request,
126121
@Res({ passthrough: true }) response: Response,
127122
) {
@@ -149,9 +144,9 @@ export class AuthenticationController {
149144
await this.jwtService.verify(refreshToken);
150145

151146
const tokenClaims = {
152-
iss: process.env.JITSI_JITSIJWT_ISS,
153-
aud: process.env.JITSI_JITSIJWT_AUD,
154-
sub: process.env.JITSI_JITSIJWT_SUB,
147+
iss: this.configService.get('JITSI_JITSIJWT_ISS'),
148+
aud: this.configService.get('JITSI_JITSIJWT_AUD'),
149+
sub: this.configService.get('JITSI_JITSIJWT_SUB'),
155150
email: this.jwtService.decode(refreshToken)?.email,
156151
idToken: this.jwtService.decode(refreshToken)?.idToken,
157152
};
@@ -174,7 +169,7 @@ export class AuthenticationController {
174169

175170
return { accessToken };
176171
} catch (error) {
177-
throw new UnauthorizedException('veuillez vous authetifier');
172+
throw new UnauthorizedException('veuillez vous authentifier');
178173
}
179174
}
180175
}

0 commit comments

Comments
 (0)