Skip to content

Commit 91e2445

Browse files
committedDec 4, 2024
♻️ api: make certification result token configurable
1 parent 771a05b commit 91e2445

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed
 

Diff for: ‎api/sample.env

+14
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,20 @@ TEST_REDIS_URL=redis://localhost:6379
783783
# default: false
784784
# FT_ENABLE_CERTIF_TOKEN_SCOPE=false
785785

786+
# Control the scope of certification result tokens
787+
#
788+
# presence: optional
789+
# type: string
790+
# default: certificationResultsLink
791+
# CERTIFICATION_RESULTS_JWT_SCOPE=certificationResultsLink
792+
793+
# Control the lifespan of certification result tokens
794+
#
795+
# presence: optional
796+
# type: string
797+
# default: 30d
798+
# CERTIFICATION_RESULTS_JWT_TOKEN_LIFE_SPAN=30d
799+
786800
# Enable the text to speech button on challenges
787801
#
788802
# presence: optional
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import { config } from '../../../../shared/config.js';
22
import { tokenService } from '../../../../shared/domain/services/token-service.js';
33

4-
const generateResultsLink = function ({ sessionId, i18n }) {
5-
const daysBeforeExpiration = 30;
6-
7-
const token = tokenService.createCertificationResultsLinkToken({ sessionId, daysBeforeExpiration });
4+
export const generateResultsLink = function ({ sessionId, i18n }) {
5+
const token = tokenService.createCertificationResultsLinkToken({ sessionId });
86
const lang = i18n.getLocale();
9-
const link = `${config.domain.pixApp + config.domain.tldOrg}/resultats-session#${token}?lang=${lang}`;
10-
11-
return link;
7+
return `${config.domain.pixApp + config.domain.tldOrg}/resultats-session#${token}?lang=${lang}`;
128
};
13-
14-
export { generateResultsLink };

Diff for: ‎api/src/shared/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ const configuration = (function () {
252252
secret: process.env.PIX_DATA_AUTH_SECRET,
253253
tokenLifespan: process.env.TOKEN_LIFE_SPAN || '1h',
254254
},
255+
certificationResults: {
256+
scope: process.env.CERTIFICATION_RESULTS_JWT_SCOPE || 'certificationResultsLink',
257+
tokenLifespan: process.env.CERTIFICATION_RESULTS_JWT_TOKEN_LIFE_SPAN || '30d',
258+
},
255259
},
256260
lcms: {
257261
url: _removeTrailingSlashFromUrl(process.env.CYPRESS_LCMS_API_URL || process.env.LCMS_API_URL || ''),

Diff for: ‎api/src/shared/domain/services/token-service.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
InvalidTemporaryKeyError,
1010
} from '../errors.js';
1111

12-
const CERTIFICATION_RESULTS_LINK_SCOPE = 'certificationResultsLink';
1312
const CERTIFICATION_RESULTS_BY_RECIPIENT_EMAIL_LINK_SCOPE = 'certificationResultsByRecipientEmailLink';
1413

1514
function _createAccessToken({ userId, source, expirationDelaySeconds }) {
@@ -93,15 +92,15 @@ function createCertificationResultsByRecipientEmailLinkToken({
9392
);
9493
}
9594

96-
function createCertificationResultsLinkToken({ sessionId, daysBeforeExpiration }) {
95+
function createCertificationResultsLinkToken({ sessionId }) {
9796
return jsonwebtoken.sign(
9897
{
9998
session_id: sessionId,
100-
scope: CERTIFICATION_RESULTS_LINK_SCOPE,
99+
scope: config.jwtConfig.certificationResults.scope,
101100
},
102101
config.authentication.secret,
103102
{
104-
expiresIn: `${daysBeforeExpiration}d`,
103+
expiresIn: `${config.jwtConfig.certificationResults.tokenLifespan}`,
105104
},
106105
);
107106
}
@@ -172,7 +171,7 @@ function extractCertificationResultsLink(token) {
172171
}
173172

174173
if (config.featureToggles.isCertificationTokenScopeEnabled) {
175-
if (decoded.scope !== CERTIFICATION_RESULTS_LINK_SCOPE) {
174+
if (decoded.scope !== config.jwtConfig.certificationResults.scope) {
176175
throw new InvalidSessionResultTokenError();
177176
}
178177
}

Diff for: ‎api/tests/certification/results/unit/domain/services/session-results-link-service_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Certification | Results | Unit | Domain | Service | Session Results Li
1010
const sessionId = 12345;
1111
const i18n = getI18n();
1212
const tokenServiceStub = sinon.stub(tokenService, 'createCertificationResultsLinkToken');
13-
tokenServiceStub.withArgs({ sessionId, daysBeforeExpiration: 30 }).returns('a_valid_token');
13+
tokenServiceStub.withArgs({ sessionId }).returns('a_valid_token');
1414

1515
// when
1616
const link = sessionResultsLinkService.generateResultsLink({ sessionId, i18n });

Diff for: ‎api/tests/shared/unit/domain/services/token-service_test.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import lodash from 'lodash';
22
const { omit } = lodash;
33
import jsonwebtoken from 'jsonwebtoken';
44

5-
import { config as settings } from '../../../../../src/shared/config.js';
5+
import { config, config as settings } from '../../../../../src/shared/config.js';
66
import {
77
ForbiddenAccess,
88
InvalidExternalUserTokenError,
@@ -229,7 +229,7 @@ describe('Unit | Shared | Domain | Services | Token Service', function () {
229229
scope: 'certificationResultsLink',
230230
},
231231
settings.authentication.secret,
232-
{ expiresIn: '30d' },
232+
{ expiresIn: config.jwtConfig.certificationResults.tokenLifespan },
233233
);
234234

235235
// when
@@ -463,14 +463,13 @@ describe('Unit | Shared | Domain | Services | Token Service', function () {
463463
it('should return a valid token with sessionId and resultRecipientEmail', function () {
464464
// given
465465
const sessionId = 'abcd1234';
466-
const daysBeforeExpiration = 30;
467466
const expectedTokenAttributes = {
468467
session_id: 'abcd1234',
469468
scope: 'certificationResultsLink',
470469
};
471470

472471
// when
473-
const linkToken = tokenService.createCertificationResultsLinkToken({ sessionId, daysBeforeExpiration });
472+
const linkToken = tokenService.createCertificationResultsLinkToken({ sessionId });
474473

475474
// then
476475
const decodedToken = jsonwebtoken.verify(linkToken, settings.authentication.secret);

0 commit comments

Comments
 (0)