Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More changes to make the base SDK class reusable for EUID. #55

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
"googletag",
"initialised",
"initialising",
"IUID",
"optout",
"pbjs",
"refreshable"
],
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.tabSize": 2
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uid2/uid2-sdk",
"version": "3.2.0",
"version": "4.0.0",
"description": "UID2 Client SDK",
"main": "lib/uid2Sdk.js",
"types": "lib/uid2Sdk.d.ts",
Expand All @@ -9,6 +9,11 @@
],
"author": "The Trade Desk",
"license": "Apache 2.0",
"wallaby": {
"delays": {
"run": 1000
}
},
"scripts": {
"lint": "eslint -c .eslintrc.js . ../static/js/uid2-sdk-2.0.0.js ../static/js/uid2-sdk-1.0.0.js",
"test": "jest",
Expand Down
6 changes: 6 additions & 0 deletions src/encoding/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { bytesToBase64 } from './uid2Base64';

export async function hashAndEncodeIdentifier(value: string) {
const hash = await window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
return bytesToBase64(new Uint8Array(hash));
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/integrationTests/clientSideTokenGeneration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as mocks from '../mocks';
import { NAME_CURVE } from '../mocks';
import { base64ToBytes, bytesToBase64 } from '../uid2Base64';
import { base64ToBytes, bytesToBase64 } from '../encoding/uid2Base64';
import { EventType } from '../uid2CallbackManager';
import { sdkWindow, UID2 } from '../uid2Sdk';

Expand Down
11 changes: 6 additions & 5 deletions src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import * as jsdom from 'jsdom';
import { Cookie } from 'tough-cookie';
import { UID2 } from './uid2Sdk';
import { Uid2Identity } from './Uid2Identity';
import { localStorageKeyName } from './uid2LocalStorageManager';
import { base64ToBytes, bytesToBase64 } from './uid2Base64';
import { base64ToBytes, bytesToBase64 } from './encoding/uid2Base64';
import * as crypto from 'crypto';

const uid2LocalStorageKeyName = 'UID2-sdk-identity';

export class CookieMock {
jar: jsdom.CookieJar;
url: string;
Expand Down Expand Up @@ -311,16 +312,16 @@ export function getUid2Cookie() {
}

export function removeUid2LocalStorage() {
localStorage.removeItem(localStorageKeyName);
localStorage.removeItem(uid2LocalStorageKeyName);
}

export function setUid2LocalStorage(identity: any) {
const value = JSON.stringify(identity);
localStorage.setItem(localStorageKeyName, value);
localStorage.setItem(uid2LocalStorageKeyName, value);
}

export function getUid2LocalStorage() {
const value = localStorage.getItem(localStorageKeyName);
const value = localStorage.getItem(uid2LocalStorageKeyName);
return value !== null ? JSON.parse(value) : null;
}

Expand Down
136 changes: 0 additions & 136 deletions src/prebidModule.ts

This file was deleted.

18 changes: 10 additions & 8 deletions src/uid2ApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { UID2 } from './uid2Sdk';
import { UID2SdkBase } from './uid2Sdk';
import { isValidIdentity, Uid2Identity } from './Uid2Identity';
import { UID2CstgBox } from './uid2CstgBox';
import { exportPublicKey } from './uid2CstgCrypto';
import { ClientSideIdentityOptions, stripPublicKeyPrefix } from './uid2ClientSideIdentityOptions';
import { base64ToBytes, bytesToBase64 } from './uid2Base64';
import { base64ToBytes, bytesToBase64 } from './encoding/uid2Base64';

export type RefreshResultWithoutIdentity = {
status: ResponseStatusWithoutBody;
Expand Down Expand Up @@ -100,10 +100,12 @@ export type Uid2ApiClientOptions = {
export class Uid2ApiClient {
private _baseUrl: string;
private _clientVersion: string;
private _productName: string;
private _requestsInFlight: XMLHttpRequest[] = [];
constructor(opts: Uid2ApiClientOptions) {
this._baseUrl = opts.baseUrl ?? 'https://prod.uidapi.com';
this._clientVersion = 'uid2-sdk-' + UID2.VERSION;
constructor(opts: Uid2ApiClientOptions, defaultBaseUrl: string, productName: string) {
this._baseUrl = opts.baseUrl ?? defaultBaseUrl;
this._productName = productName;
this._clientVersion = productName.toLowerCase() + '-sdk-' + UID2SdkBase.VERSION;
}

public hasActiveRequests() {
Expand Down Expand Up @@ -133,7 +135,7 @@ export class Uid2ApiClient {
this._requestsInFlight.push(req);
req.overrideMimeType('text/plain');
req.open('POST', url, true);
req.setRequestHeader('X-UID2-Client-Version', this._clientVersion);
req.setRequestHeader('X-UID2-Client-Version', this._clientVersion); // TODO: EUID
let resolvePromise: (result: RefreshResult) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let rejectPromise: (reason?: any) => void;
Expand Down Expand Up @@ -181,10 +183,10 @@ export class Uid2ApiClient {
if (typeof result === 'string') rejectPromise(result);
else resolvePromise(result);
},
(reason) => rejectPromise(`Call to UID2 API failed: ` + reason)
(reason) => rejectPromise(`Call to ${this._productName} API failed: ` + reason)
);
},
(reason) => rejectPromise(`Call to UID2 API failed: ` + reason)
(reason) => rejectPromise(`Call to ${this._productName} API failed: ` + reason)
);
}
} catch (err) {
Expand Down
5 changes: 3 additions & 2 deletions src/uid2CookieManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ function enrichIdentity(identity: LegacyUid2SDKCookie, now: number) {

export class UID2CookieManager {
private _opts: UID2CookieOptions;
private _cookieName: string = UID2.COOKIE_NAME;
constructor(opts: UID2CookieOptions) {
private _cookieName: string;
constructor(opts: UID2CookieOptions, cookieName: string) {
this._cookieName = cookieName;
this._opts = opts;
}
public setCookie(identity: Uid2Identity) {
Expand Down
2 changes: 1 addition & 1 deletion src/uid2CstgCrypto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { base64ToBytes } from './uid2Base64';
import { base64ToBytes } from './encoding/uid2Base64';

export function generateKeyPair(namedCurve: NamedCurve): Promise<CryptoKeyPair> {
const params: EcKeyGenParams = {
Expand Down
12 changes: 7 additions & 5 deletions src/uid2LocalStorageManager.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { isValidIdentity, Uid2Identity } from './Uid2Identity';

export const localStorageKeyName = 'UID2-sdk-identity';

export class UID2LocalStorageManager {
private _storageKey: string;
constructor(storageKey: string) {
this._storageKey = storageKey;
}
public setValue(identity: Uid2Identity) {
const value = JSON.stringify(identity);
localStorage.setItem(localStorageKeyName, value);
localStorage.setItem(this._storageKey, value);
}
public removeValue() {
localStorage.removeItem(localStorageKeyName);
localStorage.removeItem(this._storageKey);
}
private getValue() {
return localStorage.getItem(localStorageKeyName);
return localStorage.getItem(this._storageKey);
}

public loadIdentityFromLocalStorage(): Uid2Identity | null {
Expand Down
Loading
Loading