Skip to content

Commit 4a0205d

Browse files
Move hashing and base64 encoding out to a separate module.
Pass in default base URL, cookie name, and local storage key name to relevant classes and make internal values harder to access externally. Remove unused (and out of date) Prebid.js module file. Mark isLoginRequired as deprecated and provide a new alternative (less misleading) name. Move setIdentityFromPhone (and hash version) to the UID2 class, as it's only available on UID2.
1 parent 82003f5 commit 4a0205d

18 files changed

+144
-240
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
"googletag",
55
"initialised",
66
"initialising",
7+
"IUID",
78
"optout",
89
"pbjs",
910
"refreshable"
1011
],
1112
"editor.defaultFormatter": "esbenp.prettier-vscode",
1213
"editor.formatOnSave": true,
1314
"editor.codeActionsOnSave": {
14-
"source.fixAll.eslint": true
15+
"source.fixAll.eslint": "explicit"
1516
},
1617
"editor.tabSize": 2
1718
}

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@uid2/uid2-sdk",
3-
"version": "3.2.0",
3+
"version": "4.0.0",
44
"description": "UID2 Client SDK",
55
"main": "lib/uid2Sdk.js",
66
"types": "lib/uid2Sdk.d.ts",
@@ -9,6 +9,11 @@
99
],
1010
"author": "The Trade Desk",
1111
"license": "Apache 2.0",
12+
"wallaby": {
13+
"delays": {
14+
"run": 1000
15+
}
16+
},
1217
"scripts": {
1318
"lint": "eslint -c .eslintrc.js . ../static/js/uid2-sdk-2.0.0.js ../static/js/uid2-sdk-1.0.0.js",
1419
"test": "jest",

src/encoding/hash.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { bytesToBase64 } from './uid2Base64';
2+
3+
export async function hashAndEncodeIdentifier(value: string) {
4+
const hash = await window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
5+
return bytesToBase64(new Uint8Array(hash));
6+
}
File renamed without changes.

src/integrationTests/clientSideTokenGeneration.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as mocks from '../mocks';
22
import { NAME_CURVE } from '../mocks';
3-
import { base64ToBytes, bytesToBase64 } from '../uid2Base64';
3+
import { base64ToBytes, bytesToBase64 } from '../encoding/uid2Base64';
44
import { EventType } from '../uid2CallbackManager';
55
import { sdkWindow, UID2 } from '../uid2Sdk';
66

src/mocks.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import * as jsdom from 'jsdom';
22
import { Cookie } from 'tough-cookie';
33
import { UID2 } from './uid2Sdk';
44
import { Uid2Identity } from './Uid2Identity';
5-
import { localStorageKeyName } from './uid2LocalStorageManager';
6-
import { base64ToBytes, bytesToBase64 } from './uid2Base64';
5+
import { base64ToBytes, bytesToBase64 } from './encoding/uid2Base64';
76
import * as crypto from 'crypto';
87

8+
const uid2LocalStorageKeyName = 'UID2-sdk-identity';
9+
910
export class CookieMock {
1011
jar: jsdom.CookieJar;
1112
url: string;
@@ -311,16 +312,16 @@ export function getUid2Cookie() {
311312
}
312313

313314
export function removeUid2LocalStorage() {
314-
localStorage.removeItem(localStorageKeyName);
315+
localStorage.removeItem(uid2LocalStorageKeyName);
315316
}
316317

317318
export function setUid2LocalStorage(identity: any) {
318319
const value = JSON.stringify(identity);
319-
localStorage.setItem(localStorageKeyName, value);
320+
localStorage.setItem(uid2LocalStorageKeyName, value);
320321
}
321322

322323
export function getUid2LocalStorage() {
323-
const value = localStorage.getItem(localStorageKeyName);
324+
const value = localStorage.getItem(uid2LocalStorageKeyName);
324325
return value !== null ? JSON.parse(value) : null;
325326
}
326327

src/prebidModule.ts

-136
This file was deleted.

src/uid2ApiClient.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { UID2 } from './uid2Sdk';
1+
import { UID2SdkBase } from './uid2Sdk';
22
import { isValidIdentity, Uid2Identity } from './Uid2Identity';
33
import { UID2CstgBox } from './uid2CstgBox';
44
import { exportPublicKey } from './uid2CstgCrypto';
55
import { ClientSideIdentityOptions, stripPublicKeyPrefix } from './uid2ClientSideIdentityOptions';
6-
import { base64ToBytes, bytesToBase64 } from './uid2Base64';
6+
import { base64ToBytes, bytesToBase64 } from './encoding/uid2Base64';
77

88
export type RefreshResultWithoutIdentity = {
99
status: ResponseStatusWithoutBody;
@@ -100,10 +100,12 @@ export type Uid2ApiClientOptions = {
100100
export class Uid2ApiClient {
101101
private _baseUrl: string;
102102
private _clientVersion: string;
103+
private _productName: string;
103104
private _requestsInFlight: XMLHttpRequest[] = [];
104-
constructor(opts: Uid2ApiClientOptions) {
105-
this._baseUrl = opts.baseUrl ?? 'https://prod.uidapi.com';
106-
this._clientVersion = 'uid2-sdk-' + UID2.VERSION;
105+
constructor(opts: Uid2ApiClientOptions, defaultBaseUrl: string, productName: string) {
106+
this._baseUrl = opts.baseUrl ?? defaultBaseUrl;
107+
this._productName = productName;
108+
this._clientVersion = productName.toLowerCase() + '-sdk-' + UID2SdkBase.VERSION;
107109
}
108110

109111
public hasActiveRequests() {
@@ -133,7 +135,7 @@ export class Uid2ApiClient {
133135
this._requestsInFlight.push(req);
134136
req.overrideMimeType('text/plain');
135137
req.open('POST', url, true);
136-
req.setRequestHeader('X-UID2-Client-Version', this._clientVersion);
138+
req.setRequestHeader('X-UID2-Client-Version', this._clientVersion); // TODO: EUID
137139
let resolvePromise: (result: RefreshResult) => void;
138140
// eslint-disable-next-line @typescript-eslint/no-explicit-any
139141
let rejectPromise: (reason?: any) => void;
@@ -181,10 +183,10 @@ export class Uid2ApiClient {
181183
if (typeof result === 'string') rejectPromise(result);
182184
else resolvePromise(result);
183185
},
184-
(reason) => rejectPromise(`Call to UID2 API failed: ` + reason)
186+
(reason) => rejectPromise(`Call to ${this._productName} API failed: ` + reason)
185187
);
186188
},
187-
(reason) => rejectPromise(`Call to UID2 API failed: ` + reason)
189+
(reason) => rejectPromise(`Call to ${this._productName} API failed: ` + reason)
188190
);
189191
}
190192
} catch (err) {

src/uid2CookieManager.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ function enrichIdentity(identity: LegacyUid2SDKCookie, now: number) {
3434

3535
export class UID2CookieManager {
3636
private _opts: UID2CookieOptions;
37-
private _cookieName: string = UID2.COOKIE_NAME;
38-
constructor(opts: UID2CookieOptions) {
37+
private _cookieName: string;
38+
constructor(opts: UID2CookieOptions, cookieName: string) {
39+
this._cookieName = cookieName;
3940
this._opts = opts;
4041
}
4142
public setCookie(identity: Uid2Identity) {

src/uid2CstgCrypto.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { base64ToBytes } from './uid2Base64';
1+
import { base64ToBytes } from './encoding/uid2Base64';
22

33
export function generateKeyPair(namedCurve: NamedCurve): Promise<CryptoKeyPair> {
44
const params: EcKeyGenParams = {

src/uid2LocalStorageManager.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { isValidIdentity, Uid2Identity } from './Uid2Identity';
22

3-
export const localStorageKeyName = 'UID2-sdk-identity';
4-
53
export class UID2LocalStorageManager {
4+
private _storageKey: string;
5+
constructor(storageKey: string) {
6+
this._storageKey = storageKey;
7+
}
68
public setValue(identity: Uid2Identity) {
79
const value = JSON.stringify(identity);
8-
localStorage.setItem(localStorageKeyName, value);
10+
localStorage.setItem(this._storageKey, value);
911
}
1012
public removeValue() {
11-
localStorage.removeItem(localStorageKeyName);
13+
localStorage.removeItem(this._storageKey);
1214
}
1315
private getValue() {
14-
return localStorage.getItem(localStorageKeyName);
16+
return localStorage.getItem(this._storageKey);
1517
}
1618

1719
public loadIdentityFromLocalStorage(): Uid2Identity | null {

0 commit comments

Comments
 (0)