forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerkleIdSystem.js
234 lines (201 loc) · 7.24 KB
/
merkleIdSystem.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
/**
* This module adds merkleId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/merkleIdSystem
* @requires module:modules/userId
*/
import { logInfo, logError, logWarn } from '../src/utils.js';
import * as ajaxLib from '../src/ajax.js';
import {submodule} from '../src/hook.js'
import {getStorageManager} from '../src/storageManager.js';
import {MODULE_TYPE_UID} from '../src/activities/modules.js';
/**
* @typedef {import('../modules/userId/index.js').Submodule} Submodule
* @typedef {import('../modules/userId/index.js').SubmoduleConfig} SubmoduleConfig
* @typedef {import('../modules/userId/index.js').ConsentData} ConsentData
* @typedef {import('../modules/userId/index.js').IdResponse} IdResponse
*/
const MODULE_NAME = 'merkleId';
const ID_URL = 'https://prebid.sv.rkdms.com/identity/';
const DEFAULT_REFRESH = 7 * 3600;
const SESSION_COOKIE_NAME = '_svsid';
export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME});
function getSession(configParams) {
let session = null;
if (typeof configParams.sv_session === 'string') {
session = configParams.sv_session;
} else {
session = storage.getCookie(SESSION_COOKIE_NAME);
}
return session;
}
function setCookie(name, value, expires) {
let expTime = new Date();
expTime.setTime(expTime.getTime() + expires * 1000 * 60);
storage.setCookie(name, value, expTime.toUTCString(), 'Lax');
}
function setSession(storage, response) {
logInfo('Merkle setting ' + `${SESSION_COOKIE_NAME}`);
if (response && response[SESSION_COOKIE_NAME] && typeof response[SESSION_COOKIE_NAME] === 'string') {
setCookie(SESSION_COOKIE_NAME, response[SESSION_COOKIE_NAME], storage.expires);
}
}
function constructUrl(configParams) {
const session = getSession(configParams);
let url = configParams.endpoint + `?sv_domain=${configParams.sv_domain}&sv_pubid=${configParams.sv_pubid}&ssp_ids=${configParams.ssp_ids.join()}`;
if (session) {
url = `${url}&sv_session=${session}`;
}
logInfo('Merkle url :' + url);
return url;
}
function generateId(configParams, configStorage) {
const url = constructUrl(configParams);
const resp = function (callback) {
ajaxLib.ajaxBuilder()(
url,
response => {
let responseObj;
if (response) {
try {
responseObj = JSON.parse(response);
setSession(configStorage, responseObj)
logInfo('Merkle responseObj ' + JSON.stringify(responseObj));
} catch (error) {
logError(error);
}
}
const date = new Date().toUTCString();
responseObj.date = date;
logInfo('Merkle responseObj with date ' + JSON.stringify(responseObj));
callback(responseObj);
},
error => {
logError(`${MODULE_NAME}: merkleId fetch encountered an error`, error);
callback();
},
{method: 'GET', withCredentials: true}
);
};
return resp;
}
/** @type {Submodule} */
export const merkleIdSubmodule = {
/**
* 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 {{eids:arrayofields}}
*/
decode(value) {
// Legacy support for a single id
const id = (value && value.pam_id && typeof value.pam_id.id === 'string') ? value.pam_id : undefined;
logInfo('Merkle id ' + JSON.stringify(id));
if (id) {
return {'merkleId': id}
}
// Supports multiple IDs for different SSPs
const merkleIds = (value && value?.merkleId && Array.isArray(value.merkleId)) ? value.merkleId : undefined;
logInfo('merkleIds: ' + JSON.stringify(merkleIds));
return merkleIds ? {'merkleId': merkleIds} : undefined;
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleConfig} [config]
* @param {ConsentData} [consentData]
* @returns {IdResponse|undefined}
*/
getId(config, consentData) {
logInfo('User ID - merkleId generating id');
const configParams = (config && config.params) || {};
if (typeof configParams.sv_pubid !== 'string') {
logError('User ID - merkleId submodule requires a valid sv_pubid string to be defined');
return;
}
if (!Array.isArray(configParams.ssp_ids)) {
logError('User ID - merkleId submodule requires a valid ssp_ids array to be defined');
return;
}
if (consentData && typeof consentData.gdprApplies === 'boolean' && consentData.gdprApplies) {
logError('User ID - merkleId submodule does not currently handle consent strings');
return;
}
if (typeof configParams.endpoint !== 'string') {
logWarn('User ID - merkleId submodule endpoint string is not defined');
configParams.endpoint = ID_URL
}
if (typeof configParams.sv_domain !== 'string') {
configParams.sv_domain = merkleIdSubmodule.findRootDomain();
}
const configStorage = (config && config.storage) || {};
const resp = generateId(configParams, configStorage)
return {callback: resp};
},
extendId: function (config = {}, consentData, storedId) {
logInfo('User ID - stored id ' + storedId);
const configParams = (config && config.params) || {};
if (typeof configParams.endpoint !== 'string') {
logWarn('User ID - merkleId submodule endpoint string is not defined');
configParams.endpoint = ID_URL;
}
if (consentData && typeof consentData.gdprApplies === 'boolean' && consentData.gdprApplies) {
logError('User ID - merkleId submodule does not currently handle consent strings');
return;
}
if (typeof configParams.sv_domain !== 'string') {
configParams.sv_domain = merkleIdSubmodule.findRootDomain();
}
const configStorage = (config && config.storage) || {};
if (configStorage && configStorage.refreshInSeconds && typeof configParams.refreshInSeconds === 'number') {
return {id: storedId};
}
let refreshInSeconds = DEFAULT_REFRESH;
if (configParams && configParams.refreshInSeconds && typeof configParams.refreshInSeconds === 'number') {
refreshInSeconds = configParams.refreshInSeconds;
logInfo('User ID - merkleId param refreshInSeconds' + refreshInSeconds);
}
const storedDate = new Date(storedId.date);
let refreshNeeded = false;
if (storedDate) {
refreshNeeded = storedDate && (Date.now() - storedDate.getTime() > refreshInSeconds * 1000);
if (refreshNeeded) {
logInfo('User ID - merkleId needs refreshing id');
const resp = generateId(configParams, configStorage);
return {callback: resp};
}
}
logInfo('User ID - merkleId not refreshed');
return {id: storedId};
},
eids: {
'merkleId': {
atype: 3,
getSource: function(data) {
if (data?.ext?.ssp) {
return `${data.ext.ssp}.merkleinc.com`
}
return 'merkleinc.com'
},
getValue: function(data) {
return data.id;
},
getUidExt: function(data) {
if (data.keyID) {
return {
keyID: data.keyID
}
}
if (data.ext) {
return data.ext;
}
}
},
}
};
submodule('userId', merkleIdSubmodule);