-
Notifications
You must be signed in to change notification settings - Fork 48
/
consent-string.js
260 lines (244 loc) · 7.13 KB
/
consent-string.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
const { encodeConsentString, getMaxVendorId, encodeVendorIdsToBits, encodePurposeIdsToBits } = require('./encode');
const { decodeConsentString } = require('./decode');
const { vendorVersionMap } = require('./utils/definitions');
/**
* Regular expression for validating
*/
const consentLanguageRegexp = /^[a-z]{2}$/;
let cachedString;
class ConsentString {
constructor(baseString = null) {
this.maxVendorId = 0;
this.created = new Date();
this.lastUpdated = new Date();
this.version = 1;
this.vendorList = null;
this.vendorListVersion = null;
this.cmpId = null;
this.cmpVersion = null;
this.consentScreen = null;
this.consentLanguage = null;
this.allowedPurposeIds = [];
this.allowedVendorIds = [];
// Decode the base string
if (baseString) {
cachedString = baseString;
Object.assign(this, decodeConsentString(baseString));
}
}
getConsentString(updateDate = true) {
let retr;
/**
* check for cached string that was passed in. This avoids having to
* decode the consent string and even to have a vendorlist
*/
if (cachedString && !updateDate) {
retr = cachedString;
} else {
if (!this.vendorList) {
throw new Error('ConsentString - A vendor list is required to encode a consent string');
}
if (updateDate === true) {
this.lastUpdated = new Date();
}
retr = encodeConsentString({
version: this.getVersion(),
vendorList: this.vendorList,
allowedPurposeIds: this.allowedPurposeIds,
allowedVendorIds: this.allowedVendorIds,
created: this.created,
lastUpdated: this.lastUpdated,
cmpId: this.cmpId,
cmpVersion: this.cmpVersion,
consentScreen: this.consentScreen,
consentLanguage: this.consentLanguage,
vendorListVersion: this.vendorListVersion,
});
cachedString = retr;
}
return retr;
}
getLastUpdated() {
return this.lastUpdated;
}
setLastUpdated(date = null) {
cachedString = '';
if (date) {
this.lastUpdated = new Date(date);
} else {
this.lastUpdated = new Date();
}
}
getCreated() {
return this.created;
}
setCreated(date = null) {
cachedString = '';
if (date) {
this.created = new Date(date);
} else {
this.created = new Date();
}
}
getMaxVendorId() {
if (!this.maxVendorId) {
if (this.vendorList) {
this.maxVendorId = getMaxVendorId(this.vendorList.vendors);
}
}
return this.maxVendorId;
}
getParsedVendorConsents() {
return encodeVendorIdsToBits(getMaxVendorId(this.vendorList.vendors), this.allowedVendorIds);
}
getParsedPurposeConsents() {
return encodePurposeIdsToBits(this.vendorList.purposes, this.allowedPurposeIds);
}
getMetadataString() {
return encodeConsentString({
version: this.getVersion(),
created: this.created,
lastUpdated: this.lastUpdated,
cmpId: this.cmpId,
cmpVersion: this.cmpVersion,
consentScreen: this.consentScreen,
vendorListVersion: this.vendorListVersion,
});
}
static decodeMetadataString(encodedMetadata) {
const decodedString = decodeConsentString(encodedMetadata);
const metadata = {};
vendorVersionMap[decodedString.version]
.metadataFields.forEach((field) => {
metadata[field] = decodedString[field];
});
return metadata;
}
getVersion() {
return this.version;
}
getVendorListVersion() {
return this.vendorListVersion;
}
setGlobalVendorList(vendorList) {
if (typeof vendorList !== 'object') {
throw new Error('ConsentString - You must provide an object when setting the global vendor list');
}
if (
!vendorList.vendorListVersion
|| !Array.isArray(vendorList.purposes)
|| !Array.isArray(vendorList.vendors)
) {
// The provided vendor list does not look valid
throw new Error('ConsentString - The provided vendor list does not respect the schema from the IAB EU’s GDPR Consent and Transparency Framework');
}
// does a vendorList already exist and is it a different version
if (!this.vendorList || this.vendorListVersion !== vendorList.vendorListVersion) {
cachedString = '';
// Cloning the GVL
// It's important as we might transform it and don't want to modify objects that we do not own
this.vendorList = {
vendorListVersion: vendorList.vendorListVersion,
lastUpdated: vendorList.lastUpdated,
purposes: vendorList.purposes,
features: vendorList.features,
// Clone the list and sort the vendors by ID (it breaks our range generation algorithm if they are not sorted)
vendors: vendorList.vendors
.slice(0)
.sort((firstVendor, secondVendor) => (firstVendor.id < secondVendor.id ? -1 : 1)),
};
this.vendorListVersion = vendorList.vendorListVersion;
}
}
getGlobalVendorList() {
return this.vendorList;
}
setCmpId(id) {
if (id !== this.cmpId) {
cachedString = '';
this.cmpId = id;
}
}
getCmpId() {
return this.cmpId;
}
setCmpVersion(version) {
if (version !== this.cmpVersion) {
cachedString = '';
this.cmpVersion = version;
}
}
getCmpVersion() {
return this.cmpVersion;
}
setConsentScreen(screenId) {
if (screenId !== this.consentScreen) {
cachedString = '';
this.consentScreen = screenId;
}
}
getConsentScreen() {
return this.consentScreen;
}
setConsentLanguage(language) {
if (consentLanguageRegexp.test(language) === false) {
throw new Error('ConsentString - The consent language must be a two-letter ISO639-1 code (en, fr, de, etc.)');
}
if (language !== this.consentLanguage) {
cachedString = '';
this.consentLanguage = language;
}
}
getConsentLanguage() {
return this.consentLanguage;
}
setPurposesAllowed(purposeIds) {
cachedString = '';
this.allowedPurposeIds = purposeIds;
}
getPurposesAllowed() {
return this.allowedPurposeIds;
}
setPurposeAllowed(purposeId, value) {
const purposeIndex = this.allowedPurposeIds.indexOf(purposeId);
cachedString = '';
if (value === true) {
if (purposeIndex === -1) {
this.allowedPurposeIds.push(purposeId);
}
} else if (value === false) {
if (purposeIndex !== -1) {
this.allowedPurposeIds.splice(purposeIndex, 1);
}
}
}
isPurposeAllowed(purposeId) {
return this.allowedPurposeIds.indexOf(purposeId) !== -1;
}
setVendorsAllowed(vendorIds) {
cachedString = '';
this.allowedVendorIds = vendorIds;
}
getVendorsAllowed() {
return this.allowedVendorIds;
}
setVendorAllowed(vendorId, value) {
const vendorIndex = this.allowedVendorIds.indexOf(vendorId);
cachedString = '';
if (value === true) {
if (vendorIndex === -1) {
this.allowedVendorIds.push(vendorId);
}
} else if (value === false) {
if (vendorIndex !== -1) {
this.allowedVendorIds.splice(vendorIndex, 1);
}
}
}
isVendorAllowed(vendorId) {
return this.allowedVendorIds.indexOf(vendorId) !== -1;
}
}
module.exports = {
ConsentString,
};