-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuid2ApiClient.ts
295 lines (267 loc) · 10.4 KB
/
uid2ApiClient.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
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 './encoding/uid2Base64';
export type RefreshResultWithoutIdentity = {
status: ResponseStatusWithoutBody;
};
export type SuccessRefreshResult = {
status: ResponseStatusRequiringBody;
identity: Uid2Identity;
};
export type RefreshResult = SuccessRefreshResult | RefreshResultWithoutIdentity;
export type SuccessCstgResult = {
status: 'success';
identity: Uid2Identity;
};
export type CstgResult = SuccessCstgResult;
type RefreshApiResponse =
| {
status: ResponseStatusRequiringBody;
body: Uid2Identity;
}
| {
status: ResponseStatusWithoutBody;
};
export type ResponseStatus = ResponseStatusRequiringBody | ResponseStatusWithoutBody;
type ResponseStatusRequiringBody = 'success';
type ResponseStatusWithoutBody = 'optout' | 'expired_token';
type UnvalidatedRefreshResponse = RefreshApiResponse | { status: unknown };
function isValidRefreshResponse(
response: unknown | UnvalidatedRefreshResponse
): response is RefreshApiResponse {
if (isUnvalidatedRefreshResponse(response)) {
return (
response.status === 'optout' ||
response.status === 'expired_token' ||
(response.status === 'success' && 'body' in response && isValidIdentity(response.body))
);
}
return false;
}
function isUnvalidatedRefreshResponse(response: unknown): response is UnvalidatedRefreshResponse {
return typeof response === 'object' && response !== null && 'status' in response;
}
type CstgApiSuccessResponse = {
status: 'success';
body: Uid2Identity;
};
type CstgApiClientErrorResponse = {
status: 'client_error';
message: string;
};
type CstgApiForbiddenResponse = {
status: 'invalid_http_origin';
message: string;
};
export type CstgResponse = CstgApiSuccessResponse;
function isCstgApiSuccessResponse(response: unknown): response is CstgApiSuccessResponse {
if (response === null || typeof response !== 'object') {
return false;
}
const successResponse = response as CstgApiSuccessResponse;
return successResponse.status === 'success' && isValidIdentity(successResponse.body);
}
function isCstgApiClientErrorResponse(response: unknown): response is CstgApiClientErrorResponse {
if (response === null || typeof response !== 'object') {
return false;
}
const errorResponse = response as CstgApiClientErrorResponse;
return errorResponse.status === 'client_error' && typeof errorResponse.message === 'string';
}
function isCstgApiForbiddenResponse(response: unknown): response is CstgApiForbiddenResponse {
if (response === null || typeof response !== 'object') {
return false;
}
const forbiddenResponse = response as CstgApiForbiddenResponse;
return (
forbiddenResponse.status === 'invalid_http_origin' &&
typeof forbiddenResponse.message === 'string'
);
}
export type Uid2ApiClientOptions = {
baseUrl?: string;
};
export class Uid2ApiClient {
private _baseUrl: string;
private _clientVersion: string;
private _productName: string;
private _requestsInFlight: XMLHttpRequest[] = [];
constructor(opts: Uid2ApiClientOptions, defaultBaseUrl: string, productName: string) {
this._baseUrl = opts.baseUrl ?? defaultBaseUrl;
this._productName = productName;
this._clientVersion = productName.toLowerCase() + '-sdk-' + UID2SdkBase.VERSION;
}
public hasActiveRequests() {
return this._requestsInFlight.length > 0;
}
private ResponseToRefreshResult(
response: UnvalidatedRefreshResponse | unknown
): RefreshResult | string {
if (isValidRefreshResponse(response)) {
if (response.status === 'success')
return { status: response.status, identity: response.body };
return response;
} else return "Response didn't contain a valid status";
}
public abortActiveRequests() {
this._requestsInFlight.forEach((req) => {
req.abort();
});
this._requestsInFlight = [];
}
public callRefreshApi(refreshDetails: Uid2Identity): Promise<RefreshResult> {
const url = this._baseUrl + '/v2/token/refresh';
const req = new XMLHttpRequest();
this._requestsInFlight.push(req);
req.overrideMimeType('text/plain');
req.open('POST', url, true);
req.setRequestHeader('X-UID2-Client-Version', this._clientVersion); // N.B. EUID and UID2 currently both use the same header
let resolvePromise: (result: RefreshResult) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let rejectPromise: (reason?: any) => void;
const promise = new Promise<RefreshResult>((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});
req.onreadystatechange = () => {
if (req.readyState !== req.DONE) return;
this._requestsInFlight = this._requestsInFlight.filter((r) => r !== req);
try {
if (!refreshDetails.refresh_response_key || req.status !== 200) {
const response = JSON.parse(req.responseText) as unknown;
const result = this.ResponseToRefreshResult(response);
if (typeof result === 'string') rejectPromise(result);
else resolvePromise(result);
} else {
const encodeResp = base64ToBytes(req.responseText);
window.crypto.subtle
.importKey(
'raw',
base64ToBytes(refreshDetails.refresh_response_key),
{ name: 'AES-GCM' },
false,
['decrypt']
)
.then(
(key) => {
//returns the symmetric key
window.crypto.subtle
.decrypt(
{
name: 'AES-GCM',
iv: encodeResp.slice(0, 12), //The initialization vector you used to encrypt
tagLength: 128, //The tagLength you used to encrypt (if any)
},
key,
encodeResp.slice(12)
)
.then(
(decrypted) => {
const decryptedResponse = String.fromCharCode(...new Uint8Array(decrypted));
const response = JSON.parse(decryptedResponse) as unknown;
const result = this.ResponseToRefreshResult(response);
if (typeof result === 'string') rejectPromise(result);
else resolvePromise(result);
},
(reason) => rejectPromise(`Call to ${this._productName} API failed: ` + reason)
);
},
(reason) => rejectPromise(`Call to ${this._productName} API failed: ` + reason)
);
}
} catch (err) {
rejectPromise(err);
}
};
req.send(refreshDetails.refresh_token);
return promise;
}
public async callCstgApi(
data: { emailHash: string } | { phoneHash: string },
opts: ClientSideIdentityOptions
): Promise<CstgResult> {
const request =
'emailHash' in data ? { email_hash: data.emailHash } : { phone_hash: data.phoneHash };
const box = await UID2CstgBox.build(stripPublicKeyPrefix(opts.serverPublicKey));
const encoder = new TextEncoder();
const now = Date.now();
const { iv, ciphertext } = await box.encrypt(
encoder.encode(JSON.stringify(request)),
encoder.encode(JSON.stringify([now]))
);
const exportedPublicKey = await exportPublicKey(box.clientPublicKey);
const requestBody = {
payload: bytesToBase64(new Uint8Array(ciphertext)),
iv: bytesToBase64(new Uint8Array(iv)),
public_key: bytesToBase64(new Uint8Array(exportedPublicKey)),
timestamp: now,
subscription_id: opts.subscriptionId,
};
const url = this._baseUrl + '/v2/token/client-generate';
const req = new XMLHttpRequest();
this._requestsInFlight.push(req);
req.overrideMimeType('text/plain');
req.open('POST', url, true);
let resolvePromise: (result: CstgResult) => void;
let rejectPromise: (reason: unknown) => void;
const promise = new Promise<CstgResult>((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});
req.onreadystatechange = async () => {
if (req.readyState !== req.DONE) return;
this._requestsInFlight = this._requestsInFlight.filter((r) => r !== req);
try {
if (req.status === 200) {
const encodedResp = base64ToBytes(req.responseText);
const decrypted = await box.decrypt(encodedResp.slice(0, 12), encodedResp.slice(12));
const decryptedResponse = new TextDecoder().decode(decrypted);
const response = JSON.parse(decryptedResponse) as unknown;
if (isCstgApiSuccessResponse(response)) {
resolvePromise({
status: 'success',
identity: response.body,
});
} else {
// A 200 should always be a success response.
// Something has gone wrong.
rejectPromise(
`API error: Response body was invalid for HTTP status 200: ${decryptedResponse}`
);
}
} else if (req.status === 400) {
const response = JSON.parse(req.responseText);
if (isCstgApiClientErrorResponse(response)) {
rejectPromise(`Client error: ${response.message}`);
} else {
// A 400 should always be a client error.
// Something has gone wrong.
rejectPromise(
`API error: Response body was invalid for HTTP status 400: ${req.responseText}`
);
}
} else if (req.status === 403) {
const response = JSON.parse(req.responseText);
if (isCstgApiForbiddenResponse(response)) {
rejectPromise(`Forbidden: ${response.message}`);
} else {
// A 403 should always be a forbidden response.
// Something has gone wrong.
rejectPromise(
`API error: Response body was invalid for HTTP status 403: ${req.responseText}`
);
}
} else {
rejectPromise(`API error: Unexpected HTTP status ${req.status}`);
}
} catch (err) {
rejectPromise(err);
}
};
req.send(JSON.stringify(requestBody));
return await promise;
}
}