-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
consent-info.js
470 lines (435 loc) · 12.5 KB
/
consent-info.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import {CONSENT_STRING_TYPE} from '#core/constants/consent-state';
import {isEnumValue, isObject} from '#core/types';
import {hasOwn, map} from '#core/types/object';
import {deepEquals} from '#core/types/object/json';
import {dev, user} from '#utils/log';
const TAG = 'amp-consent';
/**
* Key values for retriving/storing consent info object.
* STATE: Set when user accept or reject consent.
* STRING: Set when a consent string is used to store more granular consent info
* on vendors.
* METADATA: Set when consent metadata is passed in to store more granular consent info
* on vendors.
* DITRYBIT: Set when the stored consent info need to be revoked next time.
* PURPOSE_CONSENTS: Set when consents for purposes are passed in for client side
* granular consent. Only values ACCEPT and REJECT signals are stored.
* @enum {string}
*/
export const STORAGE_KEY = {
STATE: 's',
STRING: 'r',
IS_DIRTY: 'd',
METADATA: 'm',
PURPOSE_CONSENTS: 'pc',
};
/**
* Key values for retriving/storing metadata values within consent info
* @enum {string}
*/
export const METADATA_STORAGE_KEY = {
CONSENT_STRING_TYPE: 'cst',
ADDITIONAL_CONSENT: 'ac',
GDPR_APPLIES: 'ga',
PURPOSE_ONE: 'po',
};
/**
* Unlike the global consent state, only accepted and
* rejected values are respected and stored.
* In the future, we might consider more nuanced states.
* @enum {number}
*/
export const PURPOSE_CONSENT_STATE = {
ACCEPTED: 1,
REJECTED: 2,
};
/**
* @enum {number}
*/
export const CONSENT_ITEM_STATE = {
ACCEPTED: 1,
REJECTED: 2,
DISMISSED: 3,
NOT_REQUIRED: 4,
UNKNOWN: 5,
// TODO(@zhouyx): Seperate UI state from consent state. Add consent
// requirement state ui_state = {pending, active, complete} consent_state =
// {unknown, accepted, rejected}
};
/**
* @enum {string}
* @visibleForTesting
*/
export const TCF_POST_MESSAGE_API_COMMANDS = {
GET_TC_DATA: 'getTCData',
PING: 'ping',
ADD_EVENT_LISTENER: 'addEventListener',
REMOVE_EVENT_LISTENER: 'removeEventListener',
};
/**
* @typedef {{
* consentState: CONSENT_ITEM_STATE,
* consentString: (string|undefined),
* consentMetadata: (ConsentMetadataDef|undefined),
* purposeConsents: ({[key: string]: PURPOSE_CONSENT_STATE}|undefined),
* isDirty: (boolean|undefined),
* }}
*/
export let ConsentInfoDef;
/**
* Used in ConsentInfoDef
* @typedef {{
* consentStringType: (CONSENT_STRING_TYPE|undefined),
* additionalConsent: (string|undefined),
* gdprApplies: (boolean|undefined),
* purposeOne: (boolean|undefined),
* }}
*/
export let ConsentMetadataDef;
/**
* Convert the legacy storage value to Consent Info
* @param {boolean|Object|undefined} value
* @return {ConsentInfoDef}
*/
export function getStoredConsentInfo(value) {
if (value === undefined) {
return constructConsentInfo(CONSENT_ITEM_STATE.UNKNOWN);
}
if (typeof value === 'boolean') {
// legacy format
return getLegacyStoredConsentInfo(value);
}
if (!isObject(value)) {
throw dev().createError('Invalid stored consent value');
}
const consentState = convertValueToState(value[STORAGE_KEY.STATE]);
return constructConsentInfo(
consentState,
value[STORAGE_KEY.STRING],
convertStorageMetadata(value[STORAGE_KEY.METADATA]),
value[STORAGE_KEY.PURPOSE_CONSENTS],
value[STORAGE_KEY.IS_DIRTY] && value[STORAGE_KEY.IS_DIRTY] === 1
);
}
/**
* Helper function to detect if stored consent has dirtyBit set
* @param {?ConsentInfoDef} consentInfo
* @return {boolean}
*/
export function hasDirtyBit(consentInfo) {
if (!consentInfo) {
return false;
}
if (hasOwn(consentInfo, 'isDirty') && consentInfo['isDirty'] == true) {
return true;
}
return false;
}
/**
* Return the new consent state value based on stored state and new state
* @param {!CONSENT_ITEM_STATE} newState
* @param {!CONSENT_ITEM_STATE} previousState
* @return {!CONSENT_ITEM_STATE}
*/
export function recalculateConsentStateValue(newState, previousState) {
if (!isEnumValue(CONSENT_ITEM_STATE, newState)) {
newState = CONSENT_ITEM_STATE.UNKNOWN;
}
if (newState == CONSENT_ITEM_STATE.DISMISSED) {
return previousState || CONSENT_ITEM_STATE.UNKNOWN;
}
if (newState == CONSENT_ITEM_STATE.NOT_REQUIRED) {
if (previousState && previousState != CONSENT_ITEM_STATE.UNKNOWN) {
return previousState;
}
}
return newState;
}
/**
* Compose the value to store to localStorage based on the consentInfo
* @param {!ConsentInfoDef} consentInfo
* @return {?boolean|Object}
*/
export function composeStoreValue(consentInfo) {
const obj = map();
const consentState = consentInfo['consentState'];
if (consentState == CONSENT_ITEM_STATE.ACCEPTED) {
obj[STORAGE_KEY.STATE] = 1;
} else if (consentState == CONSENT_ITEM_STATE.REJECTED) {
obj[STORAGE_KEY.STATE] = 0;
} else {
// Only store consentString and dirtyBit with reject/accept action
return null;
}
if (consentInfo['consentString']) {
obj[STORAGE_KEY.STRING] = consentInfo['consentString'];
}
if (consentInfo['isDirty'] === true) {
obj[STORAGE_KEY.IS_DIRTY] = 1;
}
if (consentInfo['consentMetadata']) {
obj[STORAGE_KEY.METADATA] = composeMetadataStoreValue(
consentInfo['consentMetadata']
);
}
if (consentInfo['purposeConsents']) {
obj[STORAGE_KEY.PURPOSE_CONSENTS] = consentInfo['purposeConsents'];
}
if (Object.keys(obj) == 0) {
return null;
}
return obj;
}
/**
* Convert the consentState to legacy boolean stored value
* @param {!CONSENT_ITEM_STATE} consentState
* @return {?boolean}
*/
export function calculateLegacyStateValue(consentState) {
if (consentState == CONSENT_ITEM_STATE.ACCEPTED) {
return true;
}
if (consentState == CONSENT_ITEM_STATE.REJECTED) {
return false;
}
return null;
}
/**
* Compare two consentInfo.
* Return true if they can be converted to the same stored value.
* @param {?ConsentInfoDef} infoA
* @param {?ConsentInfoDef} infoB
* @param {boolean=} opt_isDirty
* @return {boolean}
*/
export function isConsentInfoStoredValueSame(infoA, infoB, opt_isDirty) {
if (!infoA && !infoB) {
return true;
}
if (infoA && infoB) {
const stateEqual =
calculateLegacyStateValue(infoA['consentState']) ===
calculateLegacyStateValue(infoB['consentState']);
const stringEqual =
(infoA['consentString'] || '') === (infoB['consentString'] || '');
let isDirtyEqual;
if (opt_isDirty) {
isDirtyEqual = !!infoA['isDirty'] === !!opt_isDirty;
} else {
isDirtyEqual = !!infoA['isDirty'] === !!infoB['isDirty'];
}
const metadataEqual = deepEquals(
infoA['consentMetadata'],
infoB['consentMetadata']
);
const purposeConsentsEqual = deepEquals(
infoA['purposeConsents'],
infoB['purposeConsents']
);
return (
stateEqual &&
stringEqual &&
metadataEqual &&
purposeConsentsEqual &&
isDirtyEqual
);
}
return false;
}
/**
* Convert the legacy boolean stored value to consentInfo object
* @param {boolean} value
* @return {!ConsentInfoDef}
*/
function getLegacyStoredConsentInfo(value) {
const state = convertValueToState(value);
return constructConsentInfo(state);
}
/**
* Construct the consentInfo object from values
*
* @param {CONSENT_ITEM_STATE} consentState
* @param {string=} opt_consentString
* @param {ConsentMetadataDef=} opt_consentMetadata
* @param {{[key: string]: PURPOSE_CONSENT_STATE}=} opt_purposeConsents
* @param {boolean=} opt_isDirty
* @return {!ConsentInfoDef}
*/
export function constructConsentInfo(
consentState,
opt_consentString,
opt_consentMetadata,
opt_purposeConsents,
opt_isDirty
) {
return {
'consentState': consentState,
'consentString': opt_consentString,
'consentMetadata': opt_consentMetadata,
'purposeConsents': opt_purposeConsents,
'isDirty': opt_isDirty,
};
}
/**
* Construct the consentMetadataDef object from values
*
* @param {CONSENT_STRING_TYPE=} opt_consentStringType
* @param {string=} opt_additionalConsent
* @param {boolean=} opt_gdprApplies
* @param {boolean=} opt_purposeOne
* @return {!ConsentMetadataDef}
*/
export function constructMetadata(
opt_consentStringType,
opt_additionalConsent,
opt_gdprApplies,
opt_purposeOne
) {
return {
'consentStringType': opt_consentStringType,
'additionalConsent': opt_additionalConsent,
'gdprApplies': opt_gdprApplies,
'purposeOne': opt_purposeOne,
};
}
/**
* Helper function to convert stored value to CONSENT_ITEM_STATE value
* @param {*} value
* @return {!CONSENT_ITEM_STATE}
*/
export function convertValueToState(value) {
if (value === true || value === 1) {
return CONSENT_ITEM_STATE.ACCEPTED;
} else if (value === false || value === 0) {
return CONSENT_ITEM_STATE.REJECTED;
}
return CONSENT_ITEM_STATE.UNKNOWN;
}
/**
* Helper function to convert response enum value to CONSENT_ITEM_STATE value
* @param {*} value
* @return {?CONSENT_ITEM_STATE}
*/
export function convertEnumValueToState(value) {
if (value === 'accepted') {
return CONSENT_ITEM_STATE.ACCEPTED;
} else if (value === 'rejected') {
return CONSENT_ITEM_STATE.REJECTED;
} else if (value === 'unknown') {
return CONSENT_ITEM_STATE.UNKNOWN;
}
return null;
}
/**
*
* @param {!ConsentInfoDef} info
* @return {boolean}
*/
export function hasStoredValue(info) {
if (info['consentString']) {
return true;
}
return (
info['consentState'] === CONSENT_ITEM_STATE.ACCEPTED ||
info['consentState'] === CONSENT_ITEM_STATE.REJECTED
);
}
/**
* Convert the CONSENT_ITEM_STATE back to readable string
* @param {!CONSENT_ITEM_STATE} enumState
* @return {string}
*/
export function getConsentStateValue(enumState) {
if (enumState === CONSENT_ITEM_STATE.ACCEPTED) {
return 'accepted';
}
if (enumState === CONSENT_ITEM_STATE.REJECTED) {
return 'rejected';
}
return 'unknown';
}
/**
* Converts ConsentMetadataDef to stroage value:
* {'gdprApplies': true, 'additionalConsent': undefined, 'consentStringType': 2} =>
* {'ga': true, 'cst': 2}
*
* @param {ConsentMetadataDef=} consentInfoMetadata
* @return {object}
*/
export function composeMetadataStoreValue(consentInfoMetadata) {
const storageMetadata = map();
if (consentInfoMetadata['consentStringType']) {
storageMetadata[METADATA_STORAGE_KEY.CONSENT_STRING_TYPE] =
consentInfoMetadata['consentStringType'];
}
if (consentInfoMetadata['additionalConsent']) {
storageMetadata[METADATA_STORAGE_KEY.ADDITIONAL_CONSENT] =
consentInfoMetadata['additionalConsent'];
}
if (consentInfoMetadata['gdprApplies'] != undefined) {
storageMetadata[METADATA_STORAGE_KEY.GDPR_APPLIES] =
consentInfoMetadata['gdprApplies'];
}
if (consentInfoMetadata['purposeOne'] != undefined) {
storageMetadata[METADATA_STORAGE_KEY.PURPOSE_ONE] =
consentInfoMetadata['purposeOne'];
}
return storageMetadata;
}
/**
* Converts stroage metadata to ConsentMetadataDef:
* {'ga': true, 'cst': 2} =>
* {'gdprApplies': true, 'additionalConsnet': undefined, 'consentStringType': 2}
*
* @param {Object|null|undefined} storageMetadata
* @return {ConsentMetadataDef}
*/
export function convertStorageMetadata(storageMetadata) {
if (!storageMetadata) {
return constructMetadata();
}
return constructMetadata(
storageMetadata[METADATA_STORAGE_KEY.CONSENT_STRING_TYPE],
storageMetadata[METADATA_STORAGE_KEY.ADDITIONAL_CONSENT],
storageMetadata[METADATA_STORAGE_KEY.GDPR_APPLIES],
storageMetadata[METADATA_STORAGE_KEY.PURPOSE_ONE]
);
}
/**
* Confirm that the metadata values are valid.
* Remove and provide user error otherwise.
* @param {JsonObject} metadata
*/
export function assertMetadataValues(metadata) {
const consentStringType = metadata['consentStringType'];
const additionalConsent = metadata['additionalConsent'];
const gdprApplies = metadata['gdprApplies'];
const purposeOne = metadata['purposeOne'];
const errorFields = [];
if (
consentStringType &&
!isEnumValue(CONSENT_STRING_TYPE, consentStringType)
) {
delete metadata['consentStringType'];
errorFields.push('consentStringType');
}
if (additionalConsent && typeof additionalConsent != 'string') {
delete metadata['additionalConsent'];
errorFields.push('additionalConsent');
}
if (gdprApplies && typeof gdprApplies != 'boolean') {
delete metadata['gdprApplies'];
errorFields.push('gdprApplies');
}
if (purposeOne && typeof purposeOne != 'boolean') {
delete metadata['purposeOne'];
errorFields.push('purposeOne');
}
for (let i = 0; i < errorFields.length; i++) {
user().error(
TAG,
'Consent metadata value "%s" is invalid.',
errorFields[i]
);
}
}