-
Notifications
You must be signed in to change notification settings - Fork 2k
/
sharedIdSystem.js
333 lines (311 loc) · 8.5 KB
/
sharedIdSystem.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
/**
* This module adds Shared ID support to the User ID module
* The {@link module:modules/userId} module is required.
* @module modules/sharedIdSystem
* @requires module:modules/userId
*/
import * as utils from '../src/utils.js'
import {ajax} from '../src/ajax.js';
import {submodule} from '../src/hook.js';
const MODULE_NAME = 'sharedId';
const ID_SVC = 'https://id.sharedid.org/id';
const DEFAULT_24_HOURS = 86400;
const OPT_OUT_VALUE = '00000000000000000000000000';
// These values should NEVER change. If
// they do, we're no longer making ulids!
const ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; // Crockford's Base32
const ENCODING_LEN = ENCODING.length;
const TIME_MAX = Math.pow(2, 48) - 1;
const TIME_LEN = 10;
const RANDOM_LEN = 16;
const id = factory();
/**
* Constructs cookie value
* @param value
* @param needsSync
* @returns {string}
*/
function constructCookieValue(value, needsSync) {
const cookieValue = {};
cookieValue.id = value;
cookieValue.ts = utils.timestamp();
if (needsSync) {
cookieValue.ns = true;
}
utils.logInfo('SharedId: cookie Value: ' + JSON.stringify(cookieValue));
return cookieValue;
}
/**
* Checks if id needs to be synced
* @param configParams
* @param storedId
* @returns {boolean}
*/
function isIdSynced(configParams, storedId) {
const needSync = storedId.ns;
if (needSync) {
return true;
}
if (!configParams || typeof configParams.syncTime !== 'number') {
utils.logInfo('SharedId: Sync time is not configured or is not a number');
}
let syncTime = (!configParams || typeof configParams.syncTime !== 'number') ? DEFAULT_24_HOURS : configParams.syncTime;
if (syncTime > DEFAULT_24_HOURS) {
syncTime = DEFAULT_24_HOURS;
}
const cookieTimestamp = storedId.ts;
if (cookieTimestamp) {
var secondBetweenTwoDate = timeDifferenceInSeconds(utils.timestamp(), cookieTimestamp);
return secondBetweenTwoDate >= syncTime;
}
return false;
}
/**
* Gets time difference in secounds
* @param date1
* @param date2
* @returns {number}
*/
function timeDifferenceInSeconds(date1, date2) {
const diff = (date1 - date2) / 1000;
return Math.abs(Math.round(diff));
}
/**
* id generation call back
* @param result
* @param callback
* @returns {{success: success, error: error}}
*/
function idGenerationCallback(callback) {
return {
success: function (responseBody) {
let value = {};
if (responseBody) {
try {
let responseObj = JSON.parse(responseBody);
utils.logInfo('SharedId: Generated SharedId: ' + responseObj.sharedId);
value = constructCookieValue(responseObj.sharedId, false);
} catch (error) {
utils.logError(error);
}
}
callback(value);
},
error: function (statusText, responseBody) {
const value = constructCookieValue(id(), true);
utils.logInfo('SharedId: Ulid Generated SharedId: ' + value.id);
callback(value);
}
}
}
/**
* existing id generation call back
* @param result
* @param callback
* @returns {{success: success, error: error}}
*/
function existingIdCallback(storedId, callback) {
return {
success: function (responseBody) {
utils.logInfo('SharedId: id to be synced: ' + storedId.id);
if (responseBody) {
try {
let responseObj = JSON.parse(responseBody);
storedId = constructCookieValue(responseObj.sharedId, false);
utils.logInfo('SharedId: Older SharedId: ' + storedId.id);
} catch (error) {
utils.logError(error);
}
}
callback(storedId);
},
error: function () {
utils.logInfo('SharedId: Sync error for id : ' + storedId.id);
callback(storedId);
}
}
}
/**
* Encode the id
* @param value
* @returns {string|*}
*/
function encodeId(value) {
const result = {};
const sharedId = (value && typeof value['id'] === 'string') ? value['id'] : undefined;
if (sharedId == OPT_OUT_VALUE) {
return undefined;
}
if (sharedId) {
const bidIds = {
id: sharedId,
}
const ns = (value && typeof value['ns'] === 'boolean') ? value['ns'] : undefined;
if (ns == undefined) {
bidIds.third = sharedId;
}
result.sharedid = bidIds;
utils.logInfo('SharedId: Decoded value ' + JSON.stringify(result));
return result;
}
return sharedId;
}
/**
* the factory to generate unique identifier based on time and current pseudorandom number
* @param {string} the current pseudorandom number generator
* @returns {function(*=): *}
*/
function factory(currPrng) {
if (!currPrng) {
currPrng = detectPrng();
}
return function ulid(seedTime) {
if (isNaN(seedTime)) {
seedTime = Date.now();
}
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
};
}
/**
* creates and logs the error message
* @function
* @param {string} error message
* @returns {Error}
*/
function createError(message) {
utils.logError(message);
const err = new Error(message);
err.source = 'sharedId';
return err;
}
/**
* gets a a random charcter from generated pseudorandom number
* @param {string} the generated pseudorandom number
* @returns {string}
*/
function randomChar(prng) {
let rand = Math.floor(prng() * ENCODING_LEN);
if (rand === ENCODING_LEN) {
rand = ENCODING_LEN - 1;
}
return ENCODING.charAt(rand);
}
/**
* encodes the time based on the length
* @param now
* @param len
* @returns {string} encoded time.
*/
function encodeTime (now, len) {
if (isNaN(now)) {
throw new Error(now + ' must be a number');
}
if (Number.isInteger(now) === false) {
throw createError('time must be an integer');
}
if (now > TIME_MAX) {
throw createError('cannot encode time greater than ' + TIME_MAX);
}
if (now < 0) {
throw createError('time must be positive');
}
if (Number.isInteger(len) === false) {
throw createError('length must be an integer');
}
if (len < 0) {
throw createError('length must be positive');
}
let mod;
let str = '';
for (; len > 0; len--) {
mod = now % ENCODING_LEN;
str = ENCODING.charAt(mod) + str;
now = (now - mod) / ENCODING_LEN;
}
return str;
}
/**
* encodes random character
* @param len
* @param prng
* @returns {string}
*/
function encodeRandom (len, prng) {
let str = '';
for (; len > 0; len--) {
str = randomChar(prng) + str;
}
return str;
}
/**
* detects the pseudorandom number generator and generates the random number
* @function
* @param {string} error message
* @returns {string} a random number
*/
function detectPrng(root) {
if (!root) {
root = typeof window !== 'undefined' ? window : null;
}
const browserCrypto = root && (root.crypto || root.msCrypto);
if (browserCrypto) {
return () => {
const buffer = new Uint8Array(1);
browserCrypto.getRandomValues(buffer);
return buffer[0] / 0xff;
};
}
return () => Math.random();
}
/** @type {Submodule} */
export const sharedIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* decode the stored id value for passing to bid requests
* @function
* @param {string} value
* @returns {{sharedid:{ id: string, third:string}} or undefined if value doesn't exists
*/
decode(value) {
return (value) ? encodeId(value) : undefined;
},
/**
* performs action to obtain id and return a value.
* @function
* @param {SubmoduleParams} [configParams]
* @returns {sharedId}
*/
getId(configParams) {
const resp = function (callback) {
utils.logInfo('SharedId: Sharedid doesnt exists, new cookie creation');
ajax(ID_SVC, idGenerationCallback(callback), undefined, {method: 'GET', withCredentials: true});
};
return {callback: resp};
},
/**
* performs actions even if the id exists and returns a value
* @param configParams
* @param storedId
* @returns {{callback: *}}
*/
extendId(configParams, storedId) {
utils.logInfo('SharedId: Existing shared id ' + storedId.id);
const resp = function (callback) {
const needSync = isIdSynced(configParams, storedId);
if (needSync) {
utils.logInfo('SharedId: Existing shared id ' + storedId + ' is not synced');
const sharedIdPayload = {};
sharedIdPayload.sharedId = storedId.id;
const payloadString = JSON.stringify(sharedIdPayload);
ajax(ID_SVC, existingIdCallback(storedId, callback), payloadString, {method: 'POST', withCredentials: true});
}
};
return {callback: resp};
}
};
// Register submodule for userId
submodule('userId', sharedIdSubmodule);