-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuid2Sdk.ts
476 lines (423 loc) · 14.7 KB
/
uid2Sdk.ts
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import { version } from '../package.json';
import { Uid2Identity } from './Uid2Identity';
import { IdentityStatus, notifyInitCallback } from './Uid2InitCallbacks';
import { Uid2Options, isUID2OptionsOrThrow } from './Uid2Options';
import { Logger, MakeLogger } from './sdk/logger';
import { Uid2ApiClient } from './uid2ApiClient';
import { EventType, Uid2CallbackHandler, Uid2CallbackManager } from './uid2CallbackManager';
import {
ClientSideIdentityOptions,
isClientSideIdentityOptionsOrThrow,
} from './uid2ClientSideIdentityOptions';
import { isNormalizedPhone, normalizeEmail } from './uid2DiiNormalization';
import { isBase64Hash } from './uid2HashedDii';
import { UID2PromiseHandler } from './uid2PromiseHandler';
import { UID2StorageManager } from './uid2StorageManager';
import { hashAndEncodeIdentifier } from './encoding/hash';
function hasExpired(expiry: number, now = Date.now()) {
return expiry <= now;
}
export type CallbackContainer = { callback?: () => void };
export type ProductName = 'UID2' | 'EUID';
export type ProductDetails = {
name: ProductName;
cookieName: string;
localStorageKey: string;
defaultBaseUrl: string;
};
export abstract class UID2SdkBase {
static get VERSION() {
return version;
}
static get DEFAULT_REFRESH_RETRY_PERIOD_MS() {
return 5000;
}
static IdentityStatus = IdentityStatus;
static EventType = EventType;
// Push functions to this array to receive event notifications
public callbacks: Uid2CallbackHandler[] = [];
// Dependencies initialised on construction
private _logger: Logger;
private _tokenPromiseHandler: UID2PromiseHandler;
protected _callbackManager: Uid2CallbackManager;
// Dependencies initialised on call to init due to requirement for options
private _storageManager: UID2StorageManager | undefined;
private _apiClient: Uid2ApiClient | undefined;
// State
private _product: ProductDetails;
private _opts: Uid2Options = {};
private _identity: Uid2Identity | null | undefined;
private _initComplete = false;
// Sets up nearly everything, but does not run SdkLoaded callbacks - derived classes must run them.
protected constructor(
existingCallbacks: Uid2CallbackHandler[] | undefined = undefined,
product: ProductDetails
) {
this._product = product;
this._logger = MakeLogger(console, product.name);
if (existingCallbacks) this.callbacks = existingCallbacks;
this._tokenPromiseHandler = new UID2PromiseHandler(this);
this._callbackManager = new Uid2CallbackManager(
this,
this._product.name,
() => this.getIdentity(),
this._logger
);
}
public init(opts: Uid2Options) {
this.initInternal(opts);
}
public getAdvertisingToken() {
return this.getIdentity()?.advertising_token ?? undefined;
}
public async setIdentityFromEmail(email: string, opts: ClientSideIdentityOptions) {
this.throwIfInitNotComplete('Cannot set identity before calling init.');
isClientSideIdentityOptionsOrThrow(opts);
const normalizedEmail = normalizeEmail(email);
if (normalizedEmail === undefined) {
throw new Error('Invalid email address');
}
const emailHash = await hashAndEncodeIdentifier(email);
await this.callCstgAndSetIdentity({ emailHash: emailHash }, opts);
}
public async setIdentityFromEmailHash(emailHash: string, opts: ClientSideIdentityOptions) {
this.throwIfInitNotComplete('Cannot set identity before calling init.');
isClientSideIdentityOptionsOrThrow(opts);
if (!isBase64Hash(emailHash)) {
throw new Error('Invalid hash');
}
await this.callCstgAndSetIdentity({ emailHash: emailHash }, opts);
}
public setIdentity(identity: Uid2Identity) {
if (this._apiClient) this._apiClient.abortActiveRequests();
const validatedIdentity = this.validateAndSetIdentity(identity);
if (validatedIdentity) {
this.triggerRefreshOrSetTimer(validatedIdentity);
this._callbackManager.runCallbacks(EventType.IdentityUpdated, {});
}
}
public getIdentity(): Uid2Identity | null {
return this._identity && !this.temporarilyUnavailable() ? this._identity : null;
}
// When the SDK has been initialized, this function should return the token
// from the most recent refresh request, if there is a request, wait for the
// new token. Otherwise, returns a promise which will be resolved after init.
public getAdvertisingTokenAsync() {
const token = this.getAdvertisingToken();
return this._tokenPromiseHandler.createMaybeDeferredPromise(token ?? null);
}
/**
* Deprecated
*/
public isLoginRequired() {
return this.hasIdentity();
}
public hasIdentity() {
if (!this._initComplete) return undefined;
return !(this.isLoggedIn() || this._apiClient?.hasActiveRequests());
}
public disconnect() {
this.abort(`${this._product.name} SDK disconnected.`);
// Note: This silently fails to clear the cookie if init hasn't been called and a cookieDomain is used!
if (this._storageManager) this._storageManager.removeValues();
else
new UID2StorageManager(
{},
this._product.cookieName,
this._product.localStorageKey
).removeValues();
this._identity = undefined;
this._callbackManager.runCallbacks(EventType.IdentityUpdated, {
identity: null,
});
}
// Note: This doesn't invoke callbacks. It's a hard, silent reset.
public abort(reason?: string) {
this._initComplete = true;
this._tokenPromiseHandler.rejectAllPromises(
reason ?? new Error(`${this._product.name} SDK aborted.`)
);
if (this._refreshTimerId) {
clearTimeout(this._refreshTimerId);
this._refreshTimerId = null;
}
if (this._apiClient) this._apiClient.abortActiveRequests();
}
private initInternal(opts: Uid2Options | unknown) {
if (this._initComplete) {
throw new TypeError('Calling init() more than once is not allowed');
}
if (!isUID2OptionsOrThrow(opts))
throw new TypeError(`Options provided to ${this._product.name} init couldn't be validated.`);
this._opts = opts;
this._storageManager = new UID2StorageManager(
{ ...opts },
this._product.cookieName,
this._product.localStorageKey
);
this._apiClient = new Uid2ApiClient(opts, this._product.defaultBaseUrl, this._product.name);
this._tokenPromiseHandler.registerApiClient(this._apiClient);
let identity;
if (this._opts.identity) {
identity = this._opts.identity;
} else {
identity = this._storageManager.loadIdentityWithFallback();
}
const validatedIdentity = this.validateAndSetIdentity(identity);
if (validatedIdentity) this.triggerRefreshOrSetTimer(validatedIdentity);
this._initComplete = true;
this._callbackManager?.runCallbacks(EventType.InitCompleted, {});
}
private isLoggedIn() {
return this._identity && !hasExpired(this._identity.refresh_expires);
}
private temporarilyUnavailable() {
if (!this._identity && this._apiClient?.hasActiveRequests()) return true;
if (
this._identity &&
hasExpired(this._identity.identity_expires) &&
!hasExpired(this._identity.refresh_expires)
)
return true;
return false;
}
private getIdentityStatus(identity: Uid2Identity | null):
| {
valid: true;
identity: Uid2Identity;
errorMessage: string;
status: IdentityStatus;
}
| {
valid: false;
errorMessage: string;
status: IdentityStatus;
identity: null;
} {
if (!identity) {
return {
valid: false,
errorMessage: 'Identity not available',
status: IdentityStatus.NO_IDENTITY,
identity: null,
};
}
if (!identity.advertising_token) {
return {
valid: false,
errorMessage: 'advertising_token is not available or is not valid',
status: IdentityStatus.INVALID,
identity: null,
};
}
if (!identity.refresh_token) {
return {
valid: false,
errorMessage: 'refresh_token is not available or is not valid',
status: IdentityStatus.INVALID,
identity: null,
};
}
if (hasExpired(identity.refresh_expires, Date.now())) {
return {
valid: false,
errorMessage: 'Identity expired, refresh expired',
status: IdentityStatus.REFRESH_EXPIRED,
identity: null,
};
}
if (hasExpired(identity.identity_expires, Date.now())) {
return {
valid: true,
errorMessage: 'Identity expired, refresh still valid',
status: IdentityStatus.EXPIRED,
identity,
};
}
if (typeof this._identity === 'undefined')
return {
valid: true,
identity,
status: IdentityStatus.ESTABLISHED,
errorMessage: 'Identity established',
};
return {
valid: true,
identity,
status: IdentityStatus.REFRESHED,
errorMessage: 'Identity refreshed',
};
}
private validateAndSetIdentity(
identity: Uid2Identity | null,
status?: IdentityStatus,
statusText?: string
): Uid2Identity | null {
if (!this._storageManager) throw new Error('Cannot set identity before calling init.');
const validity = this.getIdentityStatus(identity);
if (
validity.identity &&
validity.identity?.advertising_token === this._identity?.advertising_token
)
return validity.identity;
this._identity = validity.identity;
if (validity.identity) {
this._storageManager.setValue(validity.identity);
} else {
this.abort();
this._storageManager.removeValues();
}
notifyInitCallback(
this._opts,
status ?? validity.status,
statusText ?? validity.errorMessage,
this.getAdvertisingToken(),
this._logger
);
return validity.identity;
}
private triggerRefreshOrSetTimer(validIdentity: Uid2Identity) {
if (hasExpired(validIdentity.refresh_from, Date.now())) {
this.refreshToken(validIdentity);
} else {
this.setRefreshTimer();
}
}
private _refreshTimerId: ReturnType<typeof setTimeout> | null = null;
private setRefreshTimer() {
const timeout = this._opts?.refreshRetryPeriod ?? UID2.DEFAULT_REFRESH_RETRY_PERIOD_MS;
if (this._refreshTimerId) {
clearTimeout(this._refreshTimerId);
}
this._refreshTimerId = setTimeout(() => {
if (this.isLoginRequired()) return;
const validatedIdentity = this.validateAndSetIdentity(
this._storageManager?.loadIdentity() ?? null
);
if (validatedIdentity) this.triggerRefreshOrSetTimer(validatedIdentity);
this._refreshTimerId = null;
}, timeout);
}
private refreshToken(identity: Uid2Identity) {
const apiClient = this._apiClient;
if (!apiClient) throw new Error('Cannot refresh the token before calling init.');
apiClient
.callRefreshApi(identity)
.then(
(response) => {
switch (response.status) {
case 'success':
this.validateAndSetIdentity(
response.identity,
IdentityStatus.REFRESHED,
'Identity refreshed'
);
this.setRefreshTimer();
break;
case 'optout':
this.validateAndSetIdentity(null, IdentityStatus.OPTOUT, 'User opted out');
break;
case 'expired_token':
this.validateAndSetIdentity(
null,
IdentityStatus.REFRESH_EXPIRED,
'Refresh token expired'
);
break;
}
},
(reason) => {
this._logger.warn(`Encountered an error refreshing the token`, reason);
this.validateAndSetIdentity(identity);
if (!hasExpired(identity.refresh_expires, Date.now())) this.setRefreshTimer();
}
)
.then(
() => {
this._callbackManager.runCallbacks(EventType.IdentityUpdated, {});
},
(reason) => this._logger.warn(`Callbacks on identity event failed.`, reason)
);
}
protected async callCstgAndSetIdentity(
request: { emailHash: string } | { phoneHash: string },
opts: ClientSideIdentityOptions
) {
const cstgResult = await this._apiClient!.callCstgApi(request, opts);
this.setIdentity(cstgResult.identity);
}
protected throwIfInitNotComplete(message: string) {
if (!this._initComplete) {
throw new Error(message);
}
}
}
export class UID2 extends UID2SdkBase {
// Deprecated. Integrators should never access the cookie directly!
static get COOKIE_NAME() {
return '__uid_2';
}
private static get Uid2Details(): ProductDetails {
return {
name: 'UID2',
defaultBaseUrl: 'https://prod.uidapi.com',
localStorageKey: 'UID2-sdk-identity',
cookieName: UID2.COOKIE_NAME,
};
}
static setupGoogleTag() {
UID2.setupGoogleSecureSignals();
}
static setupGoogleSecureSignals() {
if (window.__uid2SecureSignalProvider)
window.__uid2SecureSignalProvider.registerSecureSignalProvider();
}
constructor(
existingCallbacks: Uid2CallbackHandler[] | undefined = undefined,
callbackContainer: CallbackContainer = {}
) {
super(existingCallbacks, UID2.Uid2Details);
const runCallbacks = () => {
this._callbackManager.runCallbacks(EventType.SdkLoaded, {});
};
if (window.__uid2 instanceof UID2) {
runCallbacks();
} else {
// Need to defer running callbacks until this is assigned to the window global
callbackContainer.callback = runCallbacks;
}
}
public async setIdentityFromPhone(phone: string, opts: ClientSideIdentityOptions) {
this.throwIfInitNotComplete('Cannot set identity before calling init.');
isClientSideIdentityOptionsOrThrow(opts);
if (!isNormalizedPhone(phone)) {
throw new Error('Invalid phone number');
}
const phoneHash = await hashAndEncodeIdentifier(phone);
await this.callCstgAndSetIdentity({ phoneHash: phoneHash }, opts);
}
public async setIdentityFromPhoneHash(phoneHash: string, opts: ClientSideIdentityOptions) {
this.throwIfInitNotComplete('Cannot set identity before calling init.');
isClientSideIdentityOptionsOrThrow(opts);
if (!isBase64Hash(phoneHash)) {
throw new Error('Invalid hash');
}
await this.callCstgAndSetIdentity({ phoneHash: phoneHash }, opts);
}
}
export type UID2Setup = {
callbacks: Uid2CallbackHandler[] | undefined;
};
declare global {
interface Window {
__uid2: UID2 | UID2Setup | undefined;
}
}
export function __uid2InternalHandleScriptLoad() {
const callbacks = window?.__uid2?.callbacks || [];
const callbackContainer: CallbackContainer = {};
window.__uid2 = new UID2(callbacks, callbackContainer);
if (callbackContainer.callback) callbackContainer.callback();
}
__uid2InternalHandleScriptLoad();
export const sdkWindow = globalThis.window;