diff --git a/modules/userId/index.js b/modules/userId/index.js index 2cc454dbb22..7b32e940c04 100644 --- a/modules/userId/index.js +++ b/modules/userId/index.js @@ -842,6 +842,26 @@ export function getRawPDString(emailHashes, userID) { return btoa(pdString); }; +function hexToBytes(hex) { + return Uint8Array.from( + hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)) + ); +} + +// Module to convert byte array to Base64 +function bytesToBase64(bytes) { + const binaryString = String.fromCharCode(...bytes); + return btoa(binaryString); +} + +export function getHexToBase64(hex) { + if (!hex || typeof hex !== 'string' || hex.trim() === '') { + logWarn(`Invalid hex input: hex string is undefined, null, or empty. This message applies only to UID2 client-side integration.`); + return undefined; + } + return bytesToBase64(hexToBytes(hex)); // Convert byte array to Base64 +} + export function updateModuleParams(moduleToUpdate) { let params = MODULE_PARAM_TO_UPDATE_FOR_SSO[moduleToUpdate.name]; if (!params) return; @@ -850,7 +870,19 @@ export function updateModuleParams(moduleToUpdate) { let enableSSO = (window.IHPWT && window.IHPWT.ssoEnabled) || (window.PWT && window.PWT.ssoEnabled) || false; let emailHashes = enableSSO && userIdentity.emailHash ? userIdentity.emailHash : userIdentity.pubProvidedEmailHash ? userIdentity.pubProvidedEmailHash : undefined; params.forEach(function(param) { - moduleToUpdate.params[param.key] = (moduleToUpdate.name === 'id5Id' ? getRawPDString(emailHashes, userIdentity.userID) : emailHashes ? emailHashes[param.hashType] : undefined); + switch (moduleToUpdate.name) { + case 'id5Id': + moduleToUpdate.params[param.key] = getRawPDString(emailHashes, userIdentity.userID); + break; + case 'uid2': + moduleToUpdate.params[param.key] = emailHashes && emailHashes[param.hashType] + ? emailHashes[param.hashType] + : getHexToBase64(emailHashes?.SHA256); + break; + default: + moduleToUpdate.params[param.key] = emailHashes ? emailHashes[param.hashType] : undefined; + break; + } }); } diff --git a/src/constants.js b/src/constants.js index 1e30fe157e2..122217f9305 100644 --- a/src/constants.js +++ b/src/constants.js @@ -120,7 +120,8 @@ export const REFRESH_IDMODULES_LIST = { 'id5Id', 'publinkId', 'connectId', - 'liveIntentId' + 'liveIntentId', + 'uid2' ], SCRIPT_BASED_MODULES: [ 'zeotapIdPlus', @@ -150,7 +151,13 @@ export const MODULE_PARAM_TO_UPDATE_FOR_SSO = { liveIntentId: [ { key: 'emailHash', - hashType: 'SHA256' + hashType: 'SHA256' // Default Hex encoding + } + ], + uid2: [ + { + key: 'emailHash', + hashType: 'SHA256_BASE64' // SHA256 Base64 encoding } ] };