Skip to content

Commit

Permalink
Add the root domain check method as a util method added for use mod… (#…
Browse files Browse the repository at this point in the history
…6124)

* Add the root domain check method as a global method added for use modules

* Add a date suffix to keep the test cookie random

* Per review, moved the findRootDomain method to utils out of the userId modules

* More the domain method back the user module but attach it to the submodule so that userId modules can have access to it

* Remove the storageManager as it is no longer used in utils
  • Loading branch information
markaconrad authored Jan 13, 2021
1 parent 888cd97 commit 5ed4400
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
3 changes: 2 additions & 1 deletion integrationExamples/gpt/userId_example.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<script src="../../build/dev/prebid.js" async></script>

<script>
function getHashedEmail() {}
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(function() {
Expand Down Expand Up @@ -157,7 +158,7 @@
}
}]
}],
eidsFunction: getHashedEmail // any user defined function that exists in the page
eidsFunction: getHashedEmail // any user defined function that exists in the page
}
},{
name: "unifiedId",
Expand Down
69 changes: 69 additions & 0 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@
* @type {string}
*/

/**
* @property
* @summary use a predefined domain override for cookies or provide your own
* @name Submodule#domainOverride
* @type {(undefined|function)}
*/

/**
* @function
* @summary Returns the root domain
* @name Submodule#findRootDomain
* @returns {string}
*/

/**
* @typedef {Object} SubmoduleConfig
* @property {string} name - the User ID submodule name (used to link submodule with config)
Expand Down Expand Up @@ -315,6 +329,60 @@ function hasGDPRConsent(consentData) {
return true;
}

/**
* Find the root domain
* @param {string|undefined} fullDomain
* @return {string}
*/
export function findRootDomain(fullDomain = window.location.hostname) {
if (!coreStorage.cookiesAreEnabled()) {
return fullDomain;
}

const domainParts = fullDomain.split('.');
if (domainParts.length == 2) {
return fullDomain;
}
let rootDomain;
let continueSearching;
let startIndex = -2;
const TEST_COOKIE_NAME = `_rdc${Date.now()}`;
const TEST_COOKIE_VALUE = 'writeable';
do {
rootDomain = domainParts.slice(startIndex).join('.');
let expirationDate = new Date(utils.timestamp() + 10 * 1000).toUTCString();

// Write a test cookie
coreStorage.setCookie(
TEST_COOKIE_NAME,
TEST_COOKIE_VALUE,
expirationDate,
'Lax',
rootDomain,
undefined
);

// See if the write was successful
const value = coreStorage.getCookie(TEST_COOKIE_NAME, undefined);
if (value === TEST_COOKIE_VALUE) {
continueSearching = false;
// Delete our test cookie
coreStorage.setCookie(
TEST_COOKIE_NAME,
'',
'Thu, 01 Jan 1970 00:00:01 GMT',
undefined,
rootDomain,
undefined
);
} else {
startIndex += -1;
continueSearching = Math.abs(startIndex) <= domainParts.length;
}
} while (continueSearching);
return rootDomain;
}

/**
* @param {SubmoduleContainer[]} submodules
* @param {function} cb - callback for after processing is done.
Expand Down Expand Up @@ -657,6 +725,7 @@ function updateSubmodules() {
// find submodule and the matching configuration, if found create and append a SubmoduleContainer
submodules = addedSubmodules.map(i => {
const submoduleConfig = find(configs, j => j.name === i.name);
i.findRootDomain = findRootDomain;
return submoduleConfig ? {
submodule: i,
config: submoduleConfig,
Expand Down
46 changes: 45 additions & 1 deletion test/spec/modules/userId_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
setStoredValue,
setSubmoduleRegistry,
syncDelay,
PBJS_USER_ID_OPTOUT_NAME
PBJS_USER_ID_OPTOUT_NAME,
findRootDomain,
} from 'modules/userId/index.js';
import {createEidsArray} from 'modules/userId/eids.js';
import {config} from 'src/config.js';
Expand Down Expand Up @@ -2145,6 +2146,8 @@ describe('User ID', function () {
let coreStorageSpy;
beforeEach(function () {
coreStorageSpy = sinon.spy(coreStorage, 'setCookie');
setSubmoduleRegistry([pubCommonIdSubmodule]);
init(config);
});
afterEach(function () {
coreStorageSpy.restore();
Expand Down Expand Up @@ -2360,5 +2363,46 @@ describe('User ID', function () {
sinon.assert.calledOnce(mockExtendId);
});
});

describe('findRootDomain', function () {
let sandbox;

beforeEach(function () {
setSubmoduleRegistry([pubCommonIdSubmodule]);
init(config);
config.setConfig({
userSync: {
syncDelay: 0,
userIds: [
{
name: 'pubCommonId',
value: { pubcid: '11111' },
},
],
},
});
sandbox = sinon.createSandbox();
sandbox
.stub(coreStorage, 'getCookie')
.onFirstCall()
.returns(null) // .co.uk
.onSecondCall()
.returns('writeable'); // realdomain.co.uk;
});

afterEach(function () {
sandbox.restore();
});

it('should just find the root domain', function () {
var domain = findRootDomain('sub.realdomain.co.uk');
expect(domain).to.be.eq('realdomain.co.uk');
});

it('should find the full domain when no subdomain is present', function () {
var domain = findRootDomain('realdomain.co.uk');
expect(domain).to.be.eq('realdomain.co.uk');
});
});
});
});

0 comments on commit 5ed4400

Please sign in to comment.