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

[TECH] Ajout de monitoring sur api/token (PIX-15565) #10716

Merged
merged 1 commit into from
Dec 4, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { logger } from '../../shared/infrastructure/utils/logger.js';
import { generateHash } from '../infrastructure/utils/crypto.js';

async function monitorApiTokenRoute(request, h, dependencies = { logger }) {
const { username, refresh_token, grant_type, scope } = request.payload;

if (grant_type === 'password') {
const hash = generateHash(username);
dependencies.logger.warn({ hash, grant_type, scope }, 'Authentication attempt');
} else if (grant_type === 'refresh_token') {
const hash = generateHash(refresh_token);
dependencies.logger.warn({ hash, grant_type, scope }, 'Authentication attempt');
} else {
dependencies.logger.warn(request.payload, 'Authentication attempt with unknown method');
}

return true;
}

export const monitorPreHandlers = { monitorApiTokenRoute };
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Joi from 'joi';

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

export const tokenRoutes = [
Expand All @@ -28,7 +29,7 @@ export const tokenRoutes = [
}),
),
},
pre: [{ method: securityPreHandlers.checkIfUserIsBlocked }],
pre: [{ method: monitorPreHandlers.monitorApiTokenRoute }, { method: securityPreHandlers.checkIfUserIsBlocked }],
handler: (request, h) => tokenController.createToken(request, h),
tags: ['identity-access-management', 'api', 'token'],
notes: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import crypto from 'node:crypto';

export function generateHash(data) {
if (!data) return null;

const hash = crypto.createHash('sha256');
hash.update(data);
return hash.digest('hex');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { monitorPreHandlers } from '../../../../src/identity-access-management/application/monitor-pre-handlers.js';
import { generateHash } from '../../../../src/identity-access-management/infrastructure/utils/crypto.js';
import { expect, hFake, sinon } from '../../../test-helper.js';

describe('Unit | Identity Access Management | Application | monitor-pre-handlers', function () {
describe('#monitorApiTokenRoute', function () {
it('logs authentication attempt with grant type password', function () {
// given
const username = '[email protected]';
const grant_type = 'password';
const scope = 'pix-app';
const hash = generateHash(username);
const logger = { warn: sinon.stub() };
const request = { payload: { grant_type, username, scope } };

// when
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger });

// then
expect(logger.warn).to.have.been.calledWith({ hash, grant_type, scope }, 'Authentication attempt');
});

it('logs authentication attempt with grant type refresh token', async function () {
// given
const refresh_token = '123';
const grant_type = 'refresh_token';
const scope = 'pix-app';
const hash = generateHash(refresh_token);
const logger = { warn: sinon.stub() };
const request = { payload: { grant_type, refresh_token, scope } };

// when
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger });

// then
expect(logger.warn).to.have.been.calledWith({ hash, grant_type, scope }, 'Authentication attempt');
});

it('logs authentication attempt with grant type unknown', async function () {
// given
const grant_type = 'unknown';
const logger = { warn: sinon.stub() };
const request = { payload: { foo: 'bar', grant_type } };

// when
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger });

// then
expect(logger.warn).to.have.been.calledWith(request.payload, 'Authentication attempt with unknown method');
});
});
});
Loading