Skip to content

Commit

Permalink
Fix/uid2 id5 issue (#953)
Browse files Browse the repository at this point in the history
* IDNT-928: Email hash support in uid2

* UID2 email hash issue

* UID-SHA256 handling of both HEX and BASE64 output encoding

* UID2: Warning message added instead of throwing Error

* Merged conflicts

* UID2: Warning message changed
  • Loading branch information
pm-nitin-nimbalkar authored Oct 15, 2024
1 parent a41582c commit d3a0ce3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
34 changes: 33 additions & 1 deletion modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
});
}

Expand Down
11 changes: 9 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export const REFRESH_IDMODULES_LIST = {
'id5Id',
'publinkId',
'connectId',
'liveIntentId'
'liveIntentId',
'uid2'
],
SCRIPT_BASED_MODULES: [
'zeotapIdPlus',
Expand Down Expand Up @@ -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
}
]
};
Expand Down

0 comments on commit d3a0ce3

Please sign in to comment.