/
storageManager.js
331 lines (310 loc) · 10.2 KB
/
storageManager.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
import {hook} from './hook.js';
import {hasDeviceAccess, checkCookieSupport, logError, logInfo, isPlainObject} from './utils.js';
import {bidderSettings as defaultBidderSettings} from './bidderSettings.js';
import {VENDORLESS_GVLID} from './consentHandler.js';
const moduleTypeWhiteList = ['core', 'prebid-module'];
export let storageCallbacks = [];
/**
* Storage options
* @typedef {Object} storageOptions
* @property {Number=} gvlid - Vendor id
* @property {string} moduleName? - Module name
* @property {string=} bidderCode? - Bidder code
* @property {string=} moduleType - Module type, value can be anyone of core or prebid-module
*/
/**
* Returns list of storage related functions with gvlid, module name and module type in its scope.
* All three argument are optional here. Below shows the usage of of these
* - GVL Id: Pass GVL id if you are a vendor
* - Bidder code: All bid adapters need to pass bidderCode
* - Module name: All other modules need to pass module name
* - Module type: Some modules may need these functions but are not vendor. e.g prebid core files in src and modules like currency.
* @param {storageOptions} options
*/
export function newStorageManager({gvlid, moduleName, bidderCode, moduleType} = {}, {bidderSettings = defaultBidderSettings} = {}) {
function isBidderAllowed() {
if (bidderCode == null) {
return true;
}
const storageAllowed = bidderSettings.get(bidderCode, 'storageAllowed');
return storageAllowed == null ? false : storageAllowed;
}
if (moduleTypeWhiteList.includes(moduleType)) {
gvlid = gvlid || VENDORLESS_GVLID;
}
function isValid(cb) {
if (!isBidderAllowed()) {
logInfo(`bidderSettings denied access to device storage for bidder '${bidderCode}'`);
const result = {valid: false};
return cb(result);
} else {
let value;
let hookDetails = {
hasEnforcementHook: false
}
validateStorageEnforcement(gvlid, bidderCode || moduleName, hookDetails, function(result) {
if (result && result.hasEnforcementHook) {
value = cb(result);
} else {
let result = {
hasEnforcementHook: false,
valid: hasDeviceAccess()
}
value = cb(result);
}
});
return value;
}
}
/**
* @param {string} key
* @param {string} value
* @param {string} [expires='']
* @param {string} [sameSite='/']
* @param {string} [domain] domain (e.g., 'example.com' or 'subdomain.example.com').
* If not specified, defaults to the host portion of the current document location.
* If a domain is specified, subdomains are always included.
* Domain must match the domain of the JavaScript origin. Setting cookies to foreign domains will be silently ignored.
*/
const setCookie = function (key, value, expires, sameSite, domain, done) {
let cb = function (result) {
if (result && result.valid) {
const domainPortion = (domain && domain !== '') ? ` ;domain=${encodeURIComponent(domain)}` : '';
const expiresPortion = (expires && expires !== '') ? ` ;expires=${expires}` : '';
const isNone = (sameSite != null && sameSite.toLowerCase() == 'none')
const secure = (isNone) ? '; Secure' : '';
document.cookie = `${key}=${encodeURIComponent(value)}${expiresPortion}; path=/${domainPortion}${sameSite ? `; SameSite=${sameSite}` : ''}${secure}`;
}
}
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(cb);
done(result);
});
} else {
return isValid(cb);
}
};
/**
* @param {string} name
* @returns {(string|null)}
*/
const getCookie = function(name, done) {
let cb = function (result) {
if (result && result.valid) {
let m = window.document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]*)\\s*(;|$)');
return m ? decodeURIComponent(m[2]) : null;
}
return null;
}
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(cb);
done(result);
});
} else {
return isValid(cb);
}
};
/**
* @returns {boolean}
*/
const localStorageIsEnabled = function (done) {
let cb = function (result) {
if (result && result.valid) {
try {
localStorage.setItem('prebid.cookieTest', '1');
return localStorage.getItem('prebid.cookieTest') === '1';
} catch (error) {
} finally {
try {
localStorage.removeItem('prebid.cookieTest');
} catch (error) {}
}
}
return false;
}
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(cb);
done(result);
});
} else {
return isValid(cb);
}
}
/**
* @returns {boolean}
*/
const cookiesAreEnabled = function (done) {
let cb = function (result) {
if (result && result.valid) {
return checkCookieSupport();
}
return false;
}
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(cb);
done(result);
});
} else {
return isValid(cb);
}
}
/**
* @param {string} key
* @param {string} value
*/
const setDataInLocalStorage = function (key, value, done) {
let cb = function (result) {
if (result && result.valid && hasLocalStorage()) {
window.localStorage.setItem(key, value);
}
}
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(cb);
done(result);
});
} else {
return isValid(cb);
}
}
/**
* @param {string} key
* @returns {(string|null)}
*/
const getDataFromLocalStorage = function (key, done) {
let cb = function (result) {
if (result && result.valid && hasLocalStorage()) {
return window.localStorage.getItem(key);
}
return null;
}
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(cb);
done(result);
});
} else {
return isValid(cb);
}
}
/**
* @param {string} key
*/
const removeDataFromLocalStorage = function (key, done) {
let cb = function (result) {
if (result && result.valid && hasLocalStorage()) {
window.localStorage.removeItem(key);
}
}
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(cb);
done(result);
});
} else {
return isValid(cb);
}
}
/**
* @returns {boolean}
*/
const hasLocalStorage = function (done) {
let cb = function (result) {
if (result && result.valid) {
try {
return !!window.localStorage;
} catch (e) {
logError('Local storage api disabled');
}
}
return false;
}
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(cb);
done(result);
});
} else {
return isValid(cb);
}
}
/**
* Returns all cookie values from the jar whose names contain the `keyLike`
* Needs to exist in `utils.js` as it follows the StorageHandler interface defined in live-connect-js. If that module were to be removed, this function can go as well.
* @param {string} keyLike
* @return {[]}
*/
const findSimilarCookies = function(keyLike, done) {
let cb = function (result) {
if (result && result.valid) {
const all = [];
if (hasDeviceAccess()) {
const cookies = document.cookie.split(';');
while (cookies.length) {
const cookie = cookies.pop();
let separatorIndex = cookie.indexOf('=');
separatorIndex = separatorIndex < 0 ? cookie.length : separatorIndex;
const cookieName = decodeURIComponent(cookie.slice(0, separatorIndex).replace(/^\s+/, ''));
if (cookieName.indexOf(keyLike) >= 0) {
all.push(decodeURIComponent(cookie.slice(separatorIndex + 1)));
}
}
}
return all;
}
}
if (done && typeof done === 'function') {
storageCallbacks.push(function() {
let result = isValid(cb);
done(result);
});
} else {
return isValid(cb);
}
}
return {
setCookie,
getCookie,
localStorageIsEnabled,
cookiesAreEnabled,
setDataInLocalStorage,
getDataFromLocalStorage,
removeDataFromLocalStorage,
hasLocalStorage,
findSimilarCookies
}
}
/**
* This hook validates the storage enforcement if gdprEnforcement module is included
*/
export const validateStorageEnforcement = hook('async', function(gvlid, moduleName, hookDetails, callback) {
callback(hookDetails);
}, 'validateStorageEnforcement');
/**
* This function returns storage functions to access cookies and localstorage. This function will bypass the gdpr enforcement requirement. Prebid as a software needs to use storage in some scenarios and is not a vendor so GDPR enforcement rules does not apply on Prebid.
* @param {string} moduleName Module name
*/
export function getCoreStorageManager(moduleName) {
return newStorageManager({moduleName: moduleName, moduleType: 'core'});
}
/**
* Note: Core modules or Prebid modules like Currency, SizeMapping should use getCoreStorageManager
* This function returns storage functions to access cookies and localstorage. Bidders and User id modules should import this and use it in their module if needed.
* Bid adapters should always provide `bidderCode`. GVL ID and Module name are optional param but gvl id is needed for when gdpr enforcement module is used.
* @param {Number=} gvlid? Vendor id - required for proper GDPR integration
* @param {string=} bidderCode? - required for bid adapters
* @param {string=} moduleName? module name
*/
export function getStorageManager({gvlid, moduleName, bidderCode} = {}) {
if (arguments.length > 1 || (arguments.length > 0 && !isPlainObject(arguments[0]))) {
throw new Error('Invalid invocation for getStorageManager')
}
return newStorageManager({gvlid, moduleName, bidderCode});
}
export function resetData() {
storageCallbacks = [];
}