Skip to content

Commit 92f524e

Browse files
authoredDec 4, 2024
[TECH] Ajout de monitoring sur api/token (PIX-15565)
#10716
2 parents 021de30 + 479b46e commit 92f524e

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { logger } from '../../shared/infrastructure/utils/logger.js';
2+
import { generateHash } from '../infrastructure/utils/crypto.js';
3+
4+
async function monitorApiTokenRoute(request, h, dependencies = { logger }) {
5+
const { username, refresh_token, grant_type, scope } = request.payload;
6+
7+
if (grant_type === 'password') {
8+
const hash = generateHash(username);
9+
dependencies.logger.warn({ hash, grant_type, scope }, 'Authentication attempt');
10+
} else if (grant_type === 'refresh_token') {
11+
const hash = generateHash(refresh_token);
12+
dependencies.logger.warn({ hash, grant_type, scope }, 'Authentication attempt');
13+
} else {
14+
dependencies.logger.warn(request.payload, 'Authentication attempt with unknown method');
15+
}
16+
17+
return true;
18+
}
19+
20+
export const monitorPreHandlers = { monitorApiTokenRoute };

Diff for: ‎api/src/identity-access-management/application/token/token.route.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Joi from 'joi';
22

33
import { BadRequestError, sendJsonApiError } from '../../../shared/application/http-errors.js';
44
import { securityPreHandlers } from '../../../shared/application/security-pre-handlers.js';
5+
import { monitorPreHandlers } from '../monitor-pre-handlers.js';
56
import { tokenController } from './token.controller.js';
67

78
export const tokenRoutes = [
@@ -28,7 +29,7 @@ export const tokenRoutes = [
2829
}),
2930
),
3031
},
31-
pre: [{ method: securityPreHandlers.checkIfUserIsBlocked }],
32+
pre: [{ method: monitorPreHandlers.monitorApiTokenRoute }, { method: securityPreHandlers.checkIfUserIsBlocked }],
3233
handler: (request, h) => tokenController.createToken(request, h),
3334
tags: ['identity-access-management', 'api', 'token'],
3435
notes: [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import crypto from 'node:crypto';
2+
3+
export function generateHash(data) {
4+
if (!data) return null;
5+
6+
const hash = crypto.createHash('sha256');
7+
hash.update(data);
8+
return hash.digest('hex');
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { monitorPreHandlers } from '../../../../src/identity-access-management/application/monitor-pre-handlers.js';
2+
import { generateHash } from '../../../../src/identity-access-management/infrastructure/utils/crypto.js';
3+
import { expect, hFake, sinon } from '../../../test-helper.js';
4+
5+
describe('Unit | Identity Access Management | Application | monitor-pre-handlers', function () {
6+
describe('#monitorApiTokenRoute', function () {
7+
it('logs authentication attempt with grant type password', function () {
8+
// given
9+
const username = 'test@email.com';
10+
const grant_type = 'password';
11+
const scope = 'pix-app';
12+
const hash = generateHash(username);
13+
const logger = { warn: sinon.stub() };
14+
const request = { payload: { grant_type, username, scope } };
15+
16+
// when
17+
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger });
18+
19+
// then
20+
expect(logger.warn).to.have.been.calledWith({ hash, grant_type, scope }, 'Authentication attempt');
21+
});
22+
23+
it('logs authentication attempt with grant type refresh token', async function () {
24+
// given
25+
const refresh_token = '123';
26+
const grant_type = 'refresh_token';
27+
const scope = 'pix-app';
28+
const hash = generateHash(refresh_token);
29+
const logger = { warn: sinon.stub() };
30+
const request = { payload: { grant_type, refresh_token, scope } };
31+
32+
// when
33+
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger });
34+
35+
// then
36+
expect(logger.warn).to.have.been.calledWith({ hash, grant_type, scope }, 'Authentication attempt');
37+
});
38+
39+
it('logs authentication attempt with grant type unknown', async function () {
40+
// given
41+
const grant_type = 'unknown';
42+
const logger = { warn: sinon.stub() };
43+
const request = { payload: { foo: 'bar', grant_type } };
44+
45+
// when
46+
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger });
47+
48+
// then
49+
expect(logger.warn).to.have.been.calledWith(request.payload, 'Authentication attempt with unknown method');
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)