forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockrAIMIdSystem.js
165 lines (147 loc) · 5.79 KB
/
lockrAIMIdSystem.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
/**
* This module adds lockr AIM ID support to the User ID module
* The {@link module:modules/userId} module is required.
* @module modules/lockrAIMIdSystem
* @requires module:modules/userId
*/
import { submodule } from '../src/hook.js';
import { ajax } from '../src/ajax.js';
import { logInfo, logWarn } from '../src/utils.js';
import { getStorageManager } from '../src/storageManager.js';
import { MODULE_TYPE_UID } from '../src/activities/modules.js';
import { gppDataHandler } from '../src/adapterManager.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').lockrAIMId} lockrAIMId
*/
const MODULE_NAME = 'lockrAIMId'
const LOG_PRE_FIX = 'lockr-AIM: ';
const AIM_PROD_URL = 'https://identity.loc.kr';
export const lockrAIMCodeVersion = '1.0';
export const storage = getStorageManager({ moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME })
function createLogger(logger, prefix) {
return function (...strings) {
logger(prefix + ' ', ...strings);
}
}
const _logInfo = createLogger(logInfo, LOG_PRE_FIX);
const _logWarn = createLogger(logWarn, LOG_PRE_FIX);
/** @type {Submodule} */
export const lockrAIMSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
init() {
_logInfo('lockrAIM Initialization complete');
},
/**
* performs action to obtain id and return a value.
* @function
* @param {SubmoduleConfig} [config]
* @param {ConsentData|undefined} consentData
* @returns {lockrAIMId}
*/
getId(config, consentData) {
if (consentData?.gdprApplies === true) {
_logWarn('lockrAIM is not intended for use where GDPR applies. The lockrAIM module will not run');
return undefined;
}
const gppConsent = gppDataHandler.getConsentData();
let gppString = '';
if (gppConsent) {
gppString = gppConsent.gppString;
}
const mappedConfig = {
appID: config?.params?.appID,
email: config?.params?.email,
baseUrl: AIM_PROD_URL,
};
_logInfo('lockr AIM configurations loaded and mapped.', mappedConfig);
if (!mappedConfig.appID || !mappedConfig.email) {
return undefined;
}
const tokenGenerator = new LockrAIMApiClient(mappedConfig, _logInfo, _logWarn, storage, gppString);
const result = tokenGenerator.generateToken();
_logInfo('lockr AIM results generated');
return result;
}
}
class LockrAIMApiClient {
static expiryDateKeys = [];
static canRefreshToken = false;
constructor(opts, logInfo, logWarn, prebidStorageManager, gppString) {
this._baseUrl = opts.baseUrl;
this._appID = opts.appID;
this._email = opts.email;
this._logInfo = logInfo;
this._logWarn = logWarn;
this._gppString = gppString;
this.prebidStorageManager = prebidStorageManager;
LockrAIMApiClient.expiryDateKeys = this.prebidStorageManager.getDataFromLocalStorage('lockr_expiry_keys') ? JSON.parse(this.prebidStorageManager.getDataFromLocalStorage('lockr_expiry_keys')) : []
this.initializeRefresher();
}
async generateToken(type = 'email', value) {
const url = this._baseUrl + '/publisher/app/v1/identityLockr/generate-tokens';
let rejectPromise;
const promise = new Promise((resolve, reject) => {
rejectPromise = reject;
});
const requestBody = {
appID: this._appID,
data: {
type: type,
value: value ?? this._email,
gppString: this._gppString,
}
}
this._logInfo('Sending the token generation request')
ajax(url, {
success: (responseText) => {
try {
const response = JSON.parse(responseText);
LockrAIMApiClient.canRefreshToken = false;
const token = response.lockrMappingToken;
this.prebidStorageManager.setDataInLocalStorage('ilui', token);
response.data.forEach(cookieitem => {
const settings = cookieitem?.settings;
this.prebidStorageManager.setDataInLocalStorage(`${cookieitem.key_name}_expiry`, cookieitem.identity_expires);
if (!LockrAIMApiClient.expiryDateKeys.includes(`${cookieitem.key_name}_expiry`)) {
LockrAIMApiClient.expiryDateKeys.push(`${cookieitem.key_name}_expiry`);
}
this.prebidStorageManager.setDataInLocalStorage('lockr_expiry_keys', JSON.stringify(LockrAIMApiClient.expiryDateKeys));
if (!settings?.dropLocalStorage) {
this.prebidStorageManager.setDataInLocalStorage(cookieitem.key_name, cookieitem.advertising_token);
}
if (!settings?.dropCookie) {
this.prebidStorageManager.setCookie(cookieitem.key_name, cookieitem.advertising_token);
}
});
LockrAIMApiClient.canRefreshToken = true;
return;
} catch (_err) {
this._logWarn(_err);
rejectPromise(responseText);
LockrAIMApiClient.canRefreshToken = true;
}
}
}, JSON.stringify(requestBody), { method: 'POST', contentType: 'application/json;charset=UTF-8' });
return promise;
}
async initializeRefresher() {
setInterval(() => {
LockrAIMApiClient.expiryDateKeys.forEach(expiryItem => {
const currentMillis = new Date().getTime();
const dateMillis = this.prebidStorageManager.getDataFromLocalStorage(expiryItem);
if (currentMillis > dateMillis && dateMillis !== null && this.prebidStorageManager.getDataFromLocalStorage('ilui') && LockrAIMApiClient.canRefreshToken) {
this.generateToken('refresh', this.prebidStorageManager.getDataFromLocalStorage('ilui'));
}
})
}, 1000);
}
}
// Register submodule for userId
submodule('userId', lockrAIMSubmodule);