Skip to content

Commit

Permalink
use generate_random_string from e-mission-common
Browse files Browse the repository at this point in the history
  • Loading branch information
JGreenlee committed Jan 17, 2025
1 parent 89127bc commit af51c90
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.serve.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"chartjs-adapter-luxon": "^1.3.1",
"chartjs-plugin-annotation": "^3.0.1",
"core-js": "^2.5.7",
"e-mission-common": "github:JGreenlee/e-mission-common#semver:0.6.2",
"e-mission-common": "github:JGreenlee/e-mission-common#semver:0.6.4",
"enketo-core": "^6.1.7",
"enketo-transformer": "^4.0.0",
"fast-xml-parser": "^4.2.2",
Expand Down
16 changes: 2 additions & 14 deletions www/js/config/opcode.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import i18next from 'i18next';
import { logDebug } from '../plugin/logger';
import AppConfig from '../types/appConfigTypes';

/**
* Adapted from https://stackoverflow.com/a/63363662/4040267
* made available under a CC BY-SA 4.0 license
*/
function generateRandomString(length: number) {
const randomInts = window.crypto.getRandomValues(new Uint8Array(length * 2));
const randomChars = Array.from(randomInts).map((b) => String.fromCharCode(b));
const randomString = randomChars.join('');
const validRandomString = window.btoa(randomString).replace(/[+/]/g, '');
const truncatedRandomString = validRandomString.substring(0, length);
return truncatedRandomString;
}
import { opcode } from 'e-mission-common';

/*
* We want to support both old style and new style tokens.
Expand Down Expand Up @@ -111,7 +99,7 @@ export function getStudyNameFromUrl(url) {
function generateOpcodeFromUrl(url: URL) {
const studyName = getStudyNameFromUrl(url);
const subgroup = url.searchParams.get('sub_group') || 'default';
const randomString = generateRandomString(32);
const randomString = opcode.generate_random_string(32, undefined);
return url.searchParams.get('tester') == 'true'
? `nrelop_${studyName}_${subgroup}_test_${randomString}`
: `nrelop_${studyName}_${subgroup}_${randomString}`;
Expand Down

0 comments on commit af51c90

Please sign in to comment.