Skip to content

Commit 9edc169

Browse files
fix(pencil): adding logs and env variables validation with joi
1 parent b19cbc6 commit 9edc169

File tree

8 files changed

+209
-28
lines changed

8 files changed

+209
-28
lines changed

package-lock.json

Lines changed: 107 additions & 0 deletions
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: 2 additions & 0 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';
@@ -50,6 +51,7 @@ import { MailerModule } from '@nestjs-modules/mailer';
5051
ConfigModule.forRoot({
5152
isGlobal: true,
5253
envFilePath: `.env.${process.env.NODE_ENV}`,
54+
validationSchema: configValidationSchema,
5355
}),
5456
MongooseModule.forRootAsync({
5557
imports: [ConfigModule],

src/authentication/authentication.service.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ConfigService } from '@nestjs/config';
12
import { HttpService } from '@nestjs/axios';
23
import {
34
Logger,
@@ -12,21 +13,20 @@ import { HttpsProxyAgent } from 'https-proxy-agent';
1213
export class AuthenticationService {
1314
private readonly logger = new Logger(AuthenticationService.name);
1415

15-
constructor(private readonly httpService: HttpService) {}
16+
constructor(
17+
private readonly httpService: HttpService,
18+
private readonly configService: ConfigService,
19+
) {}
1620

1721
loginAuthorize(state: string, nonce: string) {
18-
this.logger.log(
19-
"envoi du lien d'authorization {/authentication/login_authorize} route",
20-
);
21-
return `${process.env.AGENTCONNECT_URL}/api/v2/authorize?response_type=code&acr_values=eidas1&scope=${process.env.AGENTCONNECT_SCOPE}&client_id=${process.env.AGENTCONNECT_CLIENTID}&redirect_uri=${process.env.AGENTCONNECT_REDIRECT_URL}/login_callback&state=${state}&nonce=${nonce}`;
22+
return `${this.configService.get('AGENTCONNECT_URL')}/api/v2/authorize?response_type=code&acr_values=eidas1&scope=${this.configService.get('AGENTCONNECT_SCOPE')}&client_id=${this.configService.get('AGENTCONNECT_CLIENTID')}&redirect_uri=${this.configService.get('AGENTCONNECT_REDIRECT_URL')}/login_callback&state=${state}&nonce=${nonce}`;
2223
}
2324

2425
async loginCallback(code: string, state: string, sendedState: string) {
25-
this.logger.log('{/authentication/login_callback} route');
26-
const client_id = process.env.AGENTCONNECT_CLIENTID;
27-
const client_secret = process.env.AGENTCONNECT_SECRET;
26+
const client_id = this.configService.get('AGENTCONNECT_CLIENTID');
27+
const client_secret = this.configService.get('AGENTCONNECT_SECRET');
2828
const redirect_uri =
29-
process.env.AGENTCONNECT_REDIRECT_URL + '/login_callback';
29+
this.configService.get('AGENTCONNECT_REDIRECT_URL') + '/login_callback';
3030

3131
if (sendedState !== state) {
3232
this.logger.warn(
@@ -41,7 +41,7 @@ export class AuthenticationService {
4141
const {
4242
data: { access_token: accessToken, id_token: idToken },
4343
} = await this.httpService.axiosRef.post(
44-
`${process.env.AGENTCONNECT_URL}/api/v2/token`,
44+
`${this.configService.get('AGENTCONNECT_URL')}/api/v2/token`,
4545
queryString.stringify({
4646
grant_type: 'authorization_code',
4747
code,
@@ -54,19 +54,23 @@ export class AuthenticationService {
5454
'Content-Type': 'application/x-www-form-urlencoded',
5555
},
5656
proxy: false,
57-
httpsAgent: new HttpsProxyAgent(process.env.AGENTCONNECT_PROXYURL),
57+
httpsAgent: new HttpsProxyAgent(
58+
this.configService.get('AGENTCONNECT_PROXYURL'),
59+
),
5860
},
5961
);
6062
this.logger.log(
6163
"accessToken récupéré d'agentConnect {/authentication/login_callback} route",
6264
);
6365

6466
const { data: userinfo } = await this.httpService.axiosRef.get(
65-
`${process.env.AGENTCONNECT_URL}/api/v2/userinfo`,
67+
`${this.configService.get('AGENTCONNECT_URL')}/api/v2/userinfo`,
6668
{
6769
headers: { Authorization: `Bearer ${accessToken}` },
6870
proxy: false,
69-
httpsAgent: new HttpsProxyAgent(process.env.AGENTCONNECT_PROXYURL),
71+
httpsAgent: new HttpsProxyAgent(
72+
this.configService.get('AGENTCONNECT_PROXYURL'),
73+
),
7074
},
7175
);
7276
this.logger.log(
@@ -77,6 +81,7 @@ export class AuthenticationService {
7781
} catch (error) {
7882
this.logger.error(
7983
"erreur lors de récupération de l'accessToken ou userinfo d'agentConnect",
84+
error,
8085
);
8186
throw new NotFoundException(
8287
"erreur lors de récupération de l'accessToken ou userinfo d'agentConnect",
@@ -90,9 +95,11 @@ export class AuthenticationService {
9095
id_token_hint: idToken,
9196
state,
9297
post_logout_redirect_uri:
93-
process.env.AGENTCONNECT_REDIRECT_URL + '/logout_callback',
98+
this.configService.get('AGENTCONNECT_REDIRECT_URL') +
99+
'/logout_callback',
94100
};
95-
const url = process.env.AGENTCONNECT_URL + '/api/v2/session/end' + '?';
101+
const url =
102+
this.configService.get('AGENTCONNECT_URL') + '/api/v2/session/end' + '?';
96103
return url + queryString.stringify(query);
97104
}
98105
}

src/conference/conference.service.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ConfigService } from '@nestjs/config';
12
import {
23
Logger,
34
BadRequestException,
@@ -22,6 +23,7 @@ export class ConferenceService {
2223
private readonly prosodyService: ProsodyService,
2324
private readonly jwtService: JwtService,
2425
private readonly mailerService: MailerService,
26+
private readonly configService: ConfigService,
2527
) {}
2628

2729
async roomExists(roomName: string) {
@@ -41,10 +43,10 @@ export class ConferenceService {
4143
roomName.length === 30
4244
) {
4345
const jwt = this.jwtService.sign({
44-
iss: process.env.JITSI_JITSIJWT_ISS,
46+
iss: this.configService.get('JITSI_JITSIJWT_ISS'),
4547
exp: moment().add('5', 'minutes').unix(),
46-
aud: process.env.JITSI_JITSIJWT_AUD,
47-
sub: process.env.JITSI_JITSIJWT_SUB,
48+
aud: this.configService.get('JITSI_JITSIJWT_AUD'),
49+
sub: this.configService.get('JITSI_JITSIJWT_SUB'),
4850
room: roomName,
4951
});
5052
return { roomName, jwt };
@@ -125,7 +127,7 @@ export class ConferenceService {
125127
Voici les liens pour accéder à la conférence:
126128
<br>
127129
<p style="overflow-wrap: break-word; margin:10px; color: black; background-color: white; border-radius: 2px; font-weight: bold;">
128-
Lien modérateur (Valable pendant ${process.env.JITSI_JITSIJWT_EXPIRESAFTER} heures à partir de la réception de cet email) :
130+
Lien modérateur (Valable pendant ${this.configService.get('JITSI_JITSIJWT_EXPIRESAFTER')} heures à partir de la réception de cet email) :
129131
<br>
130132
<small>Ce lien vous permet de contrôler le fonctionnement de votre conférence.</small>
131133
<br>
@@ -154,9 +156,9 @@ export class ConferenceService {
154156
</html>
155157
`;
156158
await this.mailerService.sendMail({
157-
from: process.env.EMAIL_FROM, // sender address
159+
from: this.configService.get('EMAIL_FROM'), // sender address
158160
to: email, // list of receivers
159-
subject: process.env.EMAIL_SUBJECT + roomName, // Subject line
161+
subject: this.configService.get('EMAIL_SUBJECT') + roomName, // Subject line
160162
html: html,
161163
});
162164

@@ -173,7 +175,7 @@ export class ConferenceService {
173175
return { jwt };
174176
}
175177
} catch (error) {
176-
this.logger.error("l'accessToken est expiré");
178+
this.logger.error("l'accessToken est expiré", error);
177179
throw new UnauthorizedException("l'accessToken est expiré");
178180
}
179181
}
@@ -184,12 +186,12 @@ export class ConferenceService {
184186

185187
sendToken(roomName: string) {
186188
const jwt = this.jwtService.sign({
187-
iss: process.env.JITSI_JITSIJWT_ISS,
189+
iss: this.configService.get('JITSI_JITSIJWT_ISS'),
188190
exp: moment()
189-
.add(process.env.JITSI_JITSIJWT_EXPIRESAFTER, 'hours')
191+
.add(this.configService.get('JITSI_JITSIJWT_EXPIRESAFTER'), 'hours')
190192
.unix(),
191-
aud: process.env.JITSI_JITSIJWT_AUD,
192-
sub: process.env.JITSI_JITSIJWT_SUB,
193+
aud: this.configService.get('JITSI_JITSIJWT_AUD'),
194+
sub: this.configService.get('JITSI_JITSIJWT_SUB'),
193195
room: roomName,
194196
});
195197
return { roomName, jwt };

0 commit comments

Comments
 (0)