diff --git a/services/tunnelbroker/src/Constants.h b/services/tunnelbroker/src/Constants.h --- a/services/tunnelbroker/src/Constants.h +++ b/services/tunnelbroker/src/Constants.h @@ -43,6 +43,8 @@ // DEVICEID_CHAR_LENGTH has to be kept in sync with deviceIDCharLength // which is defined in web/utils/device-id.js const size_t DEVICEID_CHAR_LENGTH = 64; +// DEVICEID_FORMAT_REGEX has to be kept in sync with deviceIDFormatRegex +// which is defined in web/utils/device-id.js const std::regex DEVICEID_FORMAT_REGEX( "^(ks|mobile|web):[a-zA-Z0-9]{" + std::to_string(DEVICEID_CHAR_LENGTH) + "}$"); diff --git a/web/redux/device-id-reducer.js b/web/redux/device-id-reducer.js --- a/web/redux/device-id-reducer.js +++ b/web/redux/device-id-reducer.js @@ -1,16 +1,12 @@ // @flow import type { Action } from '../redux/redux-setup'; -import { deviceIDCharLength } from '../utils//device-id'; +import { deviceIDFormatRegex } from '../utils/device-id'; import { setDeviceIDActionType } from './action-types'; -const deviceIDRegex = new RegExp( - `^(ks|mobile|web):[a-zA-Z0-9]{${deviceIDCharLength.toString()}}$`, -); - export function reduceDeviceID(state: ?string, action: Action): ?string { if (action.type === setDeviceIDActionType) { - if (action.payload?.match(deviceIDRegex)) { + if (action.payload?.match(deviceIDFormatRegex)) { return action.payload; } return null; diff --git a/web/utils/device-id.js b/web/utils/device-id.js --- a/web/utils/device-id.js +++ b/web/utils/device-id.js @@ -16,6 +16,11 @@ // deviceIDCharLength has to be kept in sync with DEVICEID_CHAR_LENGTH // which is defined in services/tunnelbroker/src/Constants.h const deviceIDCharLength = 64; +// deviceIDFormatRegex has to be kept in sync with DEVICEID_FORMAT_REGEX +// which is defined in services/tunnelbroker/src/Constants.h +const deviceIDFormatRegex: RegExp = new RegExp( + `^(ks|mobile|web):[a-zA-Z0-9]{${deviceIDCharLength.toString()}}$`, +); function generateDeviceID(type: DeviceIDType): string { const suffix = generateRandomString(deviceIDCharLength, alphanumeric); @@ -30,4 +35,9 @@ invariant(false, `Unhandled device type ${type}`); } -export { generateDeviceID, deviceIDCharLength, deviceIDTypes }; +export { + generateDeviceID, + deviceIDCharLength, + deviceIDTypes, + deviceIDFormatRegex, +}; diff --git a/web/utils/device-id.test.js b/web/utils/device-id.test.js --- a/web/utils/device-id.test.js +++ b/web/utils/device-id.test.js @@ -4,17 +4,17 @@ generateDeviceID, deviceIDCharLength, deviceIDTypes, + deviceIDFormatRegex, } from './device-id'; describe('generateDeviceID', () => { - const baseRegExp = new RegExp( - `^(ks|mobile|web):[a-zA-Z0-9]{${deviceIDCharLength.toString()}}$`, - ); it( 'passed deviceIDTypes.KEYSERVER retruns a randomly generated string, ' + 'subject to ^(ks|mobile|web):[a-zA-Z0-9]{DEVICEID_CHAR_LENGTH}$', () => { - expect(generateDeviceID(deviceIDTypes.KEYSERVER)).toMatch(baseRegExp); + expect(generateDeviceID(deviceIDTypes.KEYSERVER)).toMatch( + deviceIDFormatRegex, + ); }, ); @@ -22,7 +22,7 @@ 'passed deviceIDTypes.WEB retruns a randomly generated string, ' + 'subject to ^(ks|mobile|web):[a-zA-Z0-9]{DEVICEID_CHAR_LENGTH}$', () => { - expect(generateDeviceID(deviceIDTypes.WEB)).toMatch(baseRegExp); + expect(generateDeviceID(deviceIDTypes.WEB)).toMatch(deviceIDFormatRegex); }, ); @@ -30,7 +30,9 @@ 'passed deviceIDTypes.MOBILE retruns a randomly generated string, ' + 'subject to ^(ks|mobile|web):[a-zA-Z0-9]{DEVICEID_CHAR_LENGTH}$', () => { - expect(generateDeviceID(deviceIDTypes.MOBILE)).toMatch(baseRegExp); + expect(generateDeviceID(deviceIDTypes.MOBILE)).toMatch( + deviceIDFormatRegex, + ); }, );