Skip to content

Commit

Permalink
Add core publisher restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayFrantsev committed Apr 2, 2020
1 parent 0638053 commit eb13482
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
68 changes: 60 additions & 8 deletions tc-string-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@
}, {
key: "vendorLegitimateInterests",
queue: queueVendors
}, {
key: "publisherRestrictions",
queue: [{
key: "numPubRestrictions",
size: 12
}]
});

var queueSegment = [{
Expand Down Expand Up @@ -241,7 +247,28 @@

return function (string) {
var reduceQueue = function (queue, schema, value, result) {
var reduceEntries = function () {
var reduceNumPubEntries = function () {
if (result.pubRestrictionEntry && result.rangeEntry) {
result.pubRestrictionEntry = Object.assign(result.pubRestrictionEntry, result.rangeEntry);
}

if (result.numPubRestrictions) {
result.numPubRestrictions -= 1;

queue.push({
key: "purposeId",
size: 6
}, {
key: "restrictionType",
size: 2
}, {
key: "numEntries",
size: 12
});
}
};

var reduceNumEntries = function () {
if (result.numEntries) {
result.numEntries -= 1;

Expand All @@ -253,7 +280,22 @@
key: "startVendorId",
size: 16
});
} else {
reduceNumPubEntries();
}
};

var getRangeResult = function () {
if (result.purposeId) {
return [{
purpose: result.purposeId,
isAllowed: result.restrictionType !== 0,
isConsentRequired: result.restrictionType === 1,
isLegitimateInterestRequired: result.restrictionType === 2
}];
}

return true;
};

if (schema.key === "isRangeEncoding") {
Expand All @@ -269,7 +311,7 @@

if (schema.key === "numEntries") {
result.rangeEntry = {};
reduceEntries();
reduceNumEntries();
} else

if (schema.key === "isRange") {
Expand All @@ -283,16 +325,16 @@

if (schema.key === "startVendorId") {
if (!result.isRange) {
result.rangeEntry[value] = true;
reduceEntries();
result.rangeEntry[value] = getRangeResult();
reduceNumEntries();
}
} else

if (schema.key === "endVendorId") {
for (var vendorId = result.startVendorId; vendorId <= result.endVendorId; vendorId += 1) {
result.rangeEntry[vendorId] = true;
result.rangeEntry[vendorId] = getRangeResult();
}
reduceEntries();
reduceNumEntries();
} else

if (schema.key === "numCustomPurposes") {
Expand All @@ -305,10 +347,20 @@
size: result.numCustomPurposes,
decoder: decodeFlags
});
} else

if (schema.key === "numPubRestrictions") {
result.pubRestrictionEntry = {};
reduceNumPubEntries();
}
};

var reduceResult = function (result) {
return result.pubRestrictionEntry || result.rangeEntry || result.bitField || result;
};

var offset = 0;

var getSchemaResult = function (schema, bits) {
var value = bits.slice(offset, offset + schema.size);
offset += schema.size;
Expand All @@ -331,7 +383,7 @@
reduceQueue(sectionSchema.queue, schema, value, result);
}

return result.bitField || result.rangeEntry || result;
return reduceResult(result);
};

var getBlockResult = function (blockSchema, bits) {
Expand All @@ -346,7 +398,7 @@
reduceQueue(blockSchema.queue, schema, value, result);
}

return result.bitField || result.rangeEntry || result;
return reduceResult(result);
};

var getResult = function () {
Expand Down
12 changes: 6 additions & 6 deletions tc-string-parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ava.before((test) => {
const emptyConsent = "COwxIfeOwxIfeALAWhDAAcCAAAAAAAAAAAAAAAAAAAAA.IF0EWSQgAYWwho0QUBzBAIYAfJgSCAMgSAAQIoCkFQICERBAEKiAQHAEQJAAAGBAAkACAAQAoHCBMCQABgAARiRCEQECIDRNABIBAggAKYQFAAARmikHC3ZCY702yOmQ.YAAAAAAAAAAAAAAAAAA";
test.context.empty = parse(emptyConsent);

const givenConsent = "COuDmHAOuG5DAALAWhDAAcCIAEAAACAAACgADFQAoAEAASAYoABAgAA.IF0EWSQgAYWwho0QUBzBAIYAfJgSCAMgSAAQIoCkFQICERBAEKiAQHAEQJAAAGBAAkACAAQAoHCBMCQABgAARiRCEQECIDRNABIBAggAKYQFAAARmikHC3ZCY702yOmQ.YAAAAAAAAAAAAAAAAAA";
const givenConsent = "COuDmHAOuG5DAALAWhENAfCIAEAAACAAACgAAFgGgAIEAMEABAAQEgAgAJDgAQAFgA.IF1kXyQoGY2lho0QUBzBAIYQfJwSCgMgSAAQIsSkNQIWEBBAGPiAQHAGYJAQAGBAEkACBAQAoHGBMCQABgAgRiRCMQEGIDzNIBIBAggkaI0FACCVmmkHC3ZCY70-6u_-";
test.context.given = parse(givenConsent);

const exampleConsent = "COw4XqLOw4XqLAAAAAENAXCAAAAAAAAAAAAAAAAAAAAA.IFukWSQgAIQwgI0QEByFAAAAeIAACAIgSAAQAIAgEQACEABAAAgAQFAEAIAAAGBAAgAAAAQAIFAAMCQAAgAAQiRAEQAAAAANAAIAAggAIYQFAAARmggBC3ZCYzU2yIA.QFukWSQgAIQwgI0QEByFAAAAeIAACAIgSAAQAIAgEQACEABAAAgAQFAEAIAAAGBAAgAAAAQAIFAAMCQAAgAAQiRAEQAAAAANAAIAAggAIYQFAAARmggBC3ZCYzU2yIA.YAAAAAAAAAAAAAAAAAA";
Expand Down Expand Up @@ -56,11 +56,11 @@ ava("core consent screen", (test) => {
});

ava("core consent language", (test) => {
test.is(test.context.given.core.consentLanguage, "DA");
test.is(test.context.given.core.consentLanguage, "EN");
});

ava("core vendor list version", (test) => {
test.is(test.context.given.core.vendorListVersion, 28);
test.is(test.context.given.core.vendorListVersion, 31);
});

ava("core policy version", (test) => {
Expand All @@ -76,8 +76,8 @@ ava("core use non standard stacks", (test) => {
});

ava("core special feature opt ins", (test) => {
test.deepEqual(test.context.empty.core.specialFeatureOptIns, {});
test.deepEqual(test.context.given.core.specialFeatureOptIns, {
test.deepEqual(test.context.empty.core.specialFeatureOptins, {});
test.deepEqual(test.context.given.core.specialFeatureOptins, {
1: true
});
});
Expand Down Expand Up @@ -109,7 +109,7 @@ ava("core vendor consents", (test) => {
test.deepEqual(test.context.given.core.vendorConsents, {
8: true,
9: true,
394: true
11: true
});
});

Expand Down

0 comments on commit eb13482

Please sign in to comment.