forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gppControl_usstates.js
176 lines (168 loc) · 5.46 KB
/
gppControl_usstates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {config} from '../src/config.js';
import {setupRules} from '../libraries/mspa/activityControls.js';
import {deepSetValue, prefixLog} from '../src/utils.js';
const FIELDS = {
Version: 0,
Gpc: 0,
SharingNotice: 0,
SaleOptOutNotice: 0,
SharingOptOutNotice: 0,
TargetedAdvertisingOptOutNotice: 0,
SensitiveDataProcessingOptOutNotice: 0,
SensitiveDataLimitUseNotice: 0,
SaleOptOut: 0,
SharingOptOut: 0,
TargetedAdvertisingOptOut: 0,
SensitiveDataProcessing: 12,
KnownChildSensitiveDataConsents: 2,
PersonalDataConsents: 0,
MspaCoveredTransaction: 0,
MspaOptOutOptionMode: 0,
MspaServiceProviderMode: 0,
};
/**
* Generate a normalization function for converting US state strings to the usnat format.
*
* Scalar fields are copied over if they exist in the input (state) data, or set to null otherwise.
* List fields are also copied, but forced to the "correct" length (by truncating or padding with nulls);
* additionally, elements within them can be moved around using the `move` argument.
*
* @param {Array[String]} nullify? list of fields to force to null
* @param {{}} move? Map from list field name to an index remapping for elements within that field (using 1 as the first index).
* For example, {SensitiveDataProcessing: {1: 2, 2: [1, 3]}} means "rearrange SensitiveDataProcessing by moving
* the first element to the second position, and the second element to both the first and third position."
* @param {({}, {}) => void} fn? an optional function to run once all the processing described above is complete;
* it's passed two arguments, the original (state) data, and its normalized (usnat) version.
* @param fields
* @returns {function({}): {}}
*/
export function normalizer({nullify = [], move = {}, fn}, fields = FIELDS) {
move = Object.fromEntries(Object.entries(move).map(([k, map]) => [k,
Object.fromEntries(Object.entries(map)
.map(([k, v]) => [k, Array.isArray(v) ? v : [v]])
.map(([k, v]) => [--k, v.map(el => --el)])
)])
);
return function (cd) {
const norm = Object.fromEntries(Object.entries(fields)
.map(([field, len]) => {
let val = null;
if (len > 0) {
val = Array(len).fill(null);
if (Array.isArray(cd[field])) {
const remap = move[field] || {};
const done = [];
cd[field].forEach((el, i) => {
const [dest, moved] = remap.hasOwnProperty(i) ? [remap[i], true] : [[i], false];
dest.forEach(d => {
if (d < len && !done.includes(d)) {
val[d] = el;
moved && done.push(d);
}
});
});
}
} else if (cd[field] != null) {
val = Array.isArray(cd[field]) ? null : cd[field];
}
return [field, val];
}));
nullify.forEach(path => deepSetValue(norm, path, null));
fn && fn(cd, norm);
return norm;
};
}
function scalarMinorsAreChildren(original, normalized) {
normalized.KnownChildSensitiveDataConsents = original.KnownChildSensitiveDataConsents === 0 ? [0, 0] : [1, 1];
}
export const NORMALIZATIONS = {
// normalization rules - convert state consent into usnat consent
// https://docs.prebid.org/features/mspa-usnat.html
7: (consent) => consent,
8: normalizer({
move: {
SensitiveDataProcessing: {
1: 9,
2: 10,
3: 8,
4: [1, 2],
5: 12,
8: 3,
9: 4,
}
},
fn(original, normalized) {
if (original.KnownChildSensitiveDataConsents.some(el => el !== 0)) {
normalized.KnownChildSensitiveDataConsents = [1, 1];
}
}
}),
9: normalizer({fn: scalarMinorsAreChildren}),
10: normalizer({fn: scalarMinorsAreChildren}),
11: normalizer({
move: {
SensitiveDataProcessing: {
3: 4,
4: 5,
5: 3,
}
},
fn: scalarMinorsAreChildren
}),
12: normalizer({
fn(original, normalized) {
const cc = original.KnownChildSensitiveDataConsents;
let repl;
if (!cc.some(el => el !== 0)) {
repl = [0, 0];
} else if (cc[1] === 2 && cc[2] === 2) {
repl = [2, 1];
} else {
repl = [1, 1];
}
normalized.KnownChildSensitiveDataConsents = repl;
}
})
};
export const DEFAULT_SID_MAPPING = {
8: 'usca',
9: 'usva',
10: 'usco',
11: 'usut',
12: 'usct'
};
export const getSections = (() => {
const allSIDs = Object.keys(DEFAULT_SID_MAPPING).map(Number);
return function ({sections = {}, sids = allSIDs} = {}) {
return sids.map(sid => {
const logger = prefixLog(`Cannot set up MSPA controls for SID ${sid}:`);
const ov = sections[sid] || {};
const normalizeAs = ov.normalizeAs || sid;
if (!NORMALIZATIONS.hasOwnProperty(normalizeAs)) {
logger.logError(`no normalization rules are known for SID ${normalizeAs}`)
return;
}
const api = ov.name || DEFAULT_SID_MAPPING[sid];
if (typeof api !== 'string') {
logger.logError(`cannot determine GPP section name`)
return;
}
return [
api,
[sid],
NORMALIZATIONS[normalizeAs]
]
}).filter(el => el != null);
}
})();
const handles = [];
config.getConfig('consentManagement', (cfg) => {
const gppConf = cfg.consentManagement?.gpp;
if (gppConf) {
while (handles.length) {
handles.pop()();
}
getSections(gppConf?.mspa || {})
.forEach(([api, sids, normalize]) => handles.push(setupRules(api, sids, normalize)));
}
});