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,9 +43,13 @@ // DeviceID // DEVICEID_CHAR_LENGTH has to be kept in sync with deviceIDCharLength // which is defined in web/utils/device-id.js +// and with DEVICE_ID_CHAR_LENGTH +// defined in native/native_rust_library/src/crypto_tools.rs 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 +// and with DEVICE_ID_FORMAT_REGEX +// defined in native/native_rust_library/src/crypto_tools.rs 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-updater.js b/web/redux/device-id-updater.js --- a/web/redux/device-id-updater.js +++ b/web/redux/device-id-updater.js @@ -2,7 +2,7 @@ import * as React from 'react'; import { useDispatch, useSelector } from 'react-redux'; -import { generateDeviceID, deviceIDTypes } from '../utils/device-id'; +import { generateDeviceID, deviceTypes } from '../utils/device-id'; import { setDeviceIDActionType } from './action-types'; function DeviceIDUpdater(): null { @@ -13,7 +13,7 @@ React.useEffect(() => { if (hadDeviceIDRef.current !== false && !hasDeviceID) { - const newDeviceID = generateDeviceID(deviceIDTypes.WEB); + const newDeviceID = generateDeviceID(deviceTypes.WEB); dispatch({ type: setDeviceIDActionType, payload: newDeviceID, 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 @@ -4,32 +4,38 @@ import { generateRandomString } from './text-utils'; -const deviceIDTypes = Object.freeze({ +const deviceTypes = Object.freeze({ KEYSERVER: 0, WEB: 1, MOBILE: 2, }); -type DeviceIDType = $Values; +type DeviceType = $Values; const alphanumeric = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // deviceIDCharLength has to be kept in sync with DEVICEID_CHAR_LENGTH // which is defined in services/tunnelbroker/src/Constants.h +// and with DEVICE_ID_CHAR_LENGTH +// defined in native/native_rust_library/src/crypto_tools.rs const deviceIDCharLength = 64; // deviceIDFormatRegex has to be kept in sync with DEVICEID_FORMAT_REGEX // which is defined in services/tunnelbroker/src/Constants.h +// and with DEVICE_ID_FORMAT_REGEX +// defined in native/native_rust_library/src/crypto_tools.rs const deviceIDFormatRegex: RegExp = new RegExp( `^(ks|mobile|web):[a-zA-Z0-9]{${deviceIDCharLength.toString()}}$`, ); -function generateDeviceID(type: DeviceIDType): string { +// generateDeviceID has to be kept in sync with generate_device_id +// which is defined in native/native_rust_library/src/crypto_tools.rs +function generateDeviceID(type: DeviceType): string { const suffix = generateRandomString(deviceIDCharLength, alphanumeric); - if (type === deviceIDTypes.KEYSERVER) { + if (type === deviceTypes.KEYSERVER) { return `ks:${suffix}`; - } else if (type === deviceIDTypes.WEB) { + } else if (type === deviceTypes.WEB) { return `web:${suffix}`; - } else if (type === deviceIDTypes.MOBILE) { + } else if (type === deviceTypes.MOBILE) { return `mobile:${suffix}`; } invariant(false, `Unhandled device type ${type}`); @@ -38,6 +44,6 @@ export { generateDeviceID, deviceIDCharLength, - deviceIDTypes, + deviceTypes, 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 @@ -3,64 +3,62 @@ import { generateDeviceID, deviceIDCharLength, - deviceIDTypes, + deviceTypes, deviceIDFormatRegex, } from './device-id'; describe('generateDeviceID', () => { it( - 'passed deviceIDTypes.KEYSERVER retruns a randomly generated string, ' + + 'passed deviceTypes.KEYSERVER retruns a randomly generated string, ' + 'subject to ^(ks|mobile|web):[a-zA-Z0-9]{DEVICEID_CHAR_LENGTH}$', () => { - expect(generateDeviceID(deviceIDTypes.KEYSERVER)).toMatch( + expect(generateDeviceID(deviceTypes.KEYSERVER)).toMatch( deviceIDFormatRegex, ); }, ); it( - 'passed deviceIDTypes.WEB retruns a randomly generated string, ' + + 'passed deviceTypes.WEB retruns a randomly generated string, ' + 'subject to ^(ks|mobile|web):[a-zA-Z0-9]{DEVICEID_CHAR_LENGTH}$', () => { - expect(generateDeviceID(deviceIDTypes.WEB)).toMatch(deviceIDFormatRegex); + expect(generateDeviceID(deviceTypes.WEB)).toMatch(deviceIDFormatRegex); }, ); it( - 'passed deviceIDTypes.MOBILE retruns a randomly generated string, ' + + 'passed deviceTypes.MOBILE retruns a randomly generated string, ' + 'subject to ^(ks|mobile|web):[a-zA-Z0-9]{DEVICEID_CHAR_LENGTH}$', () => { - expect(generateDeviceID(deviceIDTypes.MOBILE)).toMatch( - deviceIDFormatRegex, - ); + expect(generateDeviceID(deviceTypes.MOBILE)).toMatch(deviceIDFormatRegex); }, ); it( - 'passed deviceIDTypes.KEYSERVER retruns a randomly generated string, ' + + 'passed deviceTypes.KEYSERVER retruns a randomly generated string, ' + 'subject to ^(ks):[a-zA-Z0-9]{DEVICEID_CHAR_LENGTH}$', () => { - expect(generateDeviceID(deviceIDTypes.KEYSERVER)).toMatch( + expect(generateDeviceID(deviceTypes.KEYSERVER)).toMatch( new RegExp(`^(ks):[a-zA-Z0-9]{${deviceIDCharLength.toString()}}$`), ); }, ); it( - 'passed deviceIDTypes.WEB retruns a randomly generated string, ' + + 'passed deviceTypes.WEB retruns a randomly generated string, ' + 'subject to ^(web):[a-zA-Z0-9]{DEVICEID_CHAR_LENGTH}$', () => { - expect(generateDeviceID(deviceIDTypes.WEB)).toMatch( + expect(generateDeviceID(deviceTypes.WEB)).toMatch( new RegExp(`^(web):[a-zA-Z0-9]{${deviceIDCharLength.toString()}}$`), ); }, ); it( - 'passed deviceIDTypes.MOBILE retruns a randomly generated string, ' + + 'passed deviceTypes.MOBILE retruns a randomly generated string, ' + 'subject to ^(mobile):[a-zA-Z0-9]{DEVICEID_CHAR_LENGTH}$', () => { - expect(generateDeviceID(deviceIDTypes.MOBILE)).toMatch( + expect(generateDeviceID(deviceTypes.MOBILE)).toMatch( new RegExp(`^(mobile):[a-zA-Z0-9]{${deviceIDCharLength.toString()}}$`), ); },