Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the root domain check method as a util method added for use mod… #6124

Merged
merged 6 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
smenzer marked this conversation as resolved.
Show resolved Hide resolved
* @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');
});
});
});
});