@didit-protocol/sdk-web 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -11
- package/dist/didit-sdk.cjs +379 -73
- package/dist/didit-sdk.cjs.js +379 -73
- package/dist/didit-sdk.cjs.js.map +1 -1
- package/dist/didit-sdk.cjs.map +1 -1
- package/dist/didit-sdk.esm.js +857 -297
- package/dist/didit-sdk.esm.js.map +1 -1
- package/dist/didit-sdk.module.js +380 -74
- package/dist/didit-sdk.module.js.map +1 -1
- package/dist/didit-sdk.umd.js +858 -296
- package/dist/didit-sdk.umd.js.map +1 -1
- package/dist/didit-sdk.umd.min.js +2 -2
- package/dist/didit-sdk.umd.min.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/package.json +7 -2
package/dist/didit-sdk.umd.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Didit SDK for Web v0.
|
|
2
|
+
* Didit SDK for Web v0.6.0
|
|
3
3
|
* (c) 2026 Didit
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -10,24 +10,32 @@
|
|
|
10
10
|
})(this, (function (exports) { 'use strict';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
* Didit verification frontend. It collects stable browser signals, hashes them
|
|
15
|
-
* with WebCrypto SHA-256 and persists a random device id in localStorage with a
|
|
16
|
-
* cookie fallback. The payload shape matches the didit-fp-v2 schema so the Didit
|
|
17
|
-
* backend middleware parses it identically to the verification flow:
|
|
13
|
+
* What the navigator alone says about automation.
|
|
18
14
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
15
|
+
* The score is the strongest flag found, not a count: one conclusive signal is not diluted because
|
|
16
|
+
* the other checks came back clean. Only what a browser never shows to a person (a driver's
|
|
17
|
+
* `navigator.webdriver`, a headless or automation user agent) weighs 0.8 or more.
|
|
21
18
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
function detectBotFromNavigator(signals) {
|
|
20
|
+
const flags = [];
|
|
21
|
+
const userAgent = signals.userAgent;
|
|
22
|
+
let score = 0;
|
|
23
|
+
if (typeof navigator !== "undefined" && navigator.webdriver) {
|
|
24
|
+
flags.push("webdriver");
|
|
25
|
+
score = 0.9;
|
|
26
|
+
}
|
|
27
|
+
if (/HeadlessChrome|PhantomJS|Selenium|puppeteer|playwright/i.test(userAgent)) {
|
|
28
|
+
flags.push("headless-ua");
|
|
29
|
+
score = 0.9;
|
|
30
|
+
}
|
|
31
|
+
if (signals.uaDataMobile === false && /Android|iPhone|iPad/i.test(userAgent)) {
|
|
32
|
+
flags.push("ua-mobile-mismatch");
|
|
33
|
+
score = score || 0.3;
|
|
34
|
+
}
|
|
35
|
+
return { score, flags };
|
|
36
|
+
}
|
|
37
|
+
|
|
26
38
|
const BASE64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
27
|
-
let memoryPersistentId = null;
|
|
28
|
-
let cachedPayload = null;
|
|
29
|
-
let pendingPayload = null;
|
|
30
|
-
let cachedPayloadPromise = null;
|
|
31
39
|
function bytesToBase64Url(bytes) {
|
|
32
40
|
let output = "";
|
|
33
41
|
let i = 0;
|
|
@@ -51,12 +59,40 @@
|
|
|
51
59
|
}
|
|
52
60
|
return output;
|
|
53
61
|
}
|
|
62
|
+
function toBytes(input) {
|
|
63
|
+
if (typeof input === "string") {
|
|
64
|
+
return new TextEncoder().encode(input);
|
|
65
|
+
}
|
|
66
|
+
return input;
|
|
67
|
+
}
|
|
68
|
+
async function sha256Bytes(input) {
|
|
69
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
70
|
+
throw new Error("WebCrypto SubtleCrypto is not available");
|
|
71
|
+
}
|
|
72
|
+
const bytes = toBytes(input);
|
|
73
|
+
// crypto.subtle.digest needs a BufferSource; copy into a fresh ArrayBuffer-backed view
|
|
74
|
+
// so we are not handing over a SharedArrayBuffer-typed slice. The view itself is passed, not
|
|
75
|
+
// its buffer: a bare ArrayBuffer made in another realm (a test runner's sandbox on Node 20) is
|
|
76
|
+
// refused as "not an ArrayBuffer", and the hash would be left out; a view is accepted everywhere.
|
|
77
|
+
const view = new Uint8Array(bytes.byteLength);
|
|
78
|
+
view.set(bytes);
|
|
79
|
+
const digest = await crypto.subtle.digest("SHA-256", view);
|
|
80
|
+
return new Uint8Array(digest);
|
|
81
|
+
}
|
|
82
|
+
// SHA-256 truncated to the leading 128 bits, base64url-encoded (22 chars).
|
|
83
|
+
// Used as the composite fingerprint hash. 128 bits keeps the birthday boundary
|
|
84
|
+
// at 2^64 — collision-safe at any conceivable session volume.
|
|
85
|
+
async function sha256_128(input) {
|
|
86
|
+
const bytes = await sha256Bytes(input);
|
|
87
|
+
return bytesToBase64Url(bytes.slice(0, 16));
|
|
88
|
+
}
|
|
54
89
|
function randomBytes(length) {
|
|
55
90
|
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
56
91
|
const buffer = new Uint8Array(length);
|
|
57
92
|
crypto.getRandomValues(buffer);
|
|
58
93
|
return buffer;
|
|
59
94
|
}
|
|
95
|
+
// Math.random fallback — only reached in environments without WebCrypto.
|
|
60
96
|
const fallback = new Uint8Array(length);
|
|
61
97
|
for (let i = 0; i < length; i++) {
|
|
62
98
|
fallback[i] = Math.floor(Math.random() * 256);
|
|
@@ -66,27 +102,13 @@
|
|
|
66
102
|
function randomBase64Url(byteLength) {
|
|
67
103
|
return bytesToBase64Url(randomBytes(byteLength));
|
|
68
104
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const view = new Uint8Array(bytes.byteLength);
|
|
77
|
-
view.set(bytes);
|
|
78
|
-
const digest = await crypto.subtle.digest("SHA-256", view.buffer);
|
|
79
|
-
return bytesToBase64Url(new Uint8Array(digest).slice(0, 16));
|
|
80
|
-
}
|
|
81
|
-
// FNV-1a 32-bit fallback hash, only used when WebCrypto is unavailable.
|
|
82
|
-
function fnv1a32(value) {
|
|
83
|
-
let hash = 0x811c9dc5;
|
|
84
|
-
for (let i = 0; i < value.length; i++) {
|
|
85
|
-
hash ^= value.charCodeAt(i);
|
|
86
|
-
hash = Math.imul(hash, 0x01000193);
|
|
87
|
-
}
|
|
88
|
-
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
89
|
-
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Which fields the composite hash reads. It travels inside `signals`, the part of the payload
|
|
108
|
+
* Didit stores, so a stored hash can be told apart from one of an earlier version. A change to
|
|
109
|
+
* `canonicalNavigator` or to the full canonical form changes every device's hash once: raise it.
|
|
110
|
+
*/
|
|
111
|
+
const COMPOSITE_HASH_VERSION = 4;
|
|
90
112
|
function stableStringify(value) {
|
|
91
113
|
if (value === null || value === undefined)
|
|
92
114
|
return JSON.stringify(value ?? null);
|
|
@@ -99,200 +121,391 @@
|
|
|
99
121
|
}
|
|
100
122
|
return JSON.stringify(value);
|
|
101
123
|
}
|
|
102
|
-
|
|
103
|
-
|
|
124
|
+
/**
|
|
125
|
+
* The user agent without its version numbers: `Chrome/147.0.0.0`, `Version/17.5`,
|
|
126
|
+
* `Build/UP1A.231005.007`, `iPhone OS 17_5_1`, `Android 14`. They change with every browser and
|
|
127
|
+
* system update while the device stays the same; what is left names the browser, the system and,
|
|
128
|
+
* where the browser still gives it, the device model (`SM-S918B`, `2312DRA50G`).
|
|
129
|
+
*/
|
|
130
|
+
function userAgentWithoutVersions(userAgent) {
|
|
131
|
+
return userAgent
|
|
132
|
+
.replace(/\/[\w.]+/g, "")
|
|
133
|
+
.replace(/\d+(?:[._]\d+)+/g, "")
|
|
134
|
+
.replace(/\bAndroid \d+/g, "Android");
|
|
104
135
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
136
|
+
/**
|
|
137
|
+
* The browser's brand names without their versions. The placeholder brand Chromium adds
|
|
138
|
+
* ("Not_A Brand", "Not;A=Brand", ...) is left out: its spelling changes from one version to the next.
|
|
139
|
+
*/
|
|
140
|
+
function brandNames(brands) {
|
|
141
|
+
return brands
|
|
142
|
+
.map(brandName)
|
|
143
|
+
.filter((name) => !isPlaceholderBrand(name))
|
|
144
|
+
.sort();
|
|
115
145
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
146
|
+
/**
|
|
147
|
+
* `Google Chrome:147` is `Google Chrome`. The version follows the last colon, not the first: the
|
|
148
|
+
* placeholder has a colon of its own in two Chromium versions out of eleven (`Not:A-Brand:99`,
|
|
149
|
+
* `Not(A:Brand:8`). Cut at the first one it left `Not` or `Not(A` in the list as a brand of its
|
|
150
|
+
* own, and the hash changed with the browser's next update (version 3 did that).
|
|
151
|
+
*/
|
|
152
|
+
function brandName(brand) {
|
|
153
|
+
const colon = brand.lastIndexOf(":");
|
|
154
|
+
return colon < 0 ? brand : brand.slice(0, colon);
|
|
155
|
+
}
|
|
156
|
+
/** "not" with "brand" somewhere after it, in any case. Two searches, so a long name costs its length once. */
|
|
157
|
+
function isPlaceholderBrand(name) {
|
|
158
|
+
const lower = name.toLowerCase();
|
|
159
|
+
const not = lower.indexOf("not");
|
|
160
|
+
return not >= 0 && lower.indexOf("brand", not + 3) >= 0;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The navigator part of the canonical form: what describes the device and its browser and reads
|
|
164
|
+
* the same on every visit. No version numbers (a browser update is not another device) and the
|
|
165
|
+
* screen shorter side first (a phone turned on its side is not another device).
|
|
166
|
+
*
|
|
167
|
+
* The light fingerprint hashes this and the webview kind; the full one adds the graphics, font,
|
|
168
|
+
* math and API families (`canonicalize` in compose.ts). Both go through this one function.
|
|
169
|
+
*/
|
|
170
|
+
function canonicalNavigator(navigator) {
|
|
171
|
+
const screen = [navigator.screenWidth, navigator.screenHeight].sort((a, b) => (a ?? 0) - (b ?? 0));
|
|
172
|
+
return {
|
|
173
|
+
ua: userAgentWithoutVersions(navigator.userAgent),
|
|
174
|
+
platform: navigator.platform,
|
|
175
|
+
lang: navigator.language.split("-")[0],
|
|
176
|
+
languages: [...navigator.languages].sort(),
|
|
177
|
+
concurrency: navigator.hardwareConcurrencyQuantized,
|
|
178
|
+
memory: navigator.deviceMemory,
|
|
179
|
+
maxTouch: navigator.maxTouchPoints,
|
|
180
|
+
dpr: navigator.devicePixelRatio,
|
|
181
|
+
screen,
|
|
182
|
+
colorDepth: navigator.colorDepth,
|
|
183
|
+
timeZone: navigator.timeZone,
|
|
184
|
+
uaDataPlatform: navigator.uaDataPlatform,
|
|
185
|
+
uaDataBrands: brandNames(navigator.uaDataBrands),
|
|
186
|
+
};
|
|
125
187
|
}
|
|
126
|
-
|
|
188
|
+
/**
|
|
189
|
+
* The composite hash of a canonical form: SHA-256, first 128 bits, base64url. Empty where the
|
|
190
|
+
* browser has no WebCrypto (a page served without TLS): the hash is then left out, never replaced
|
|
191
|
+
* by a weaker one that would read as another device.
|
|
192
|
+
*/
|
|
193
|
+
async function compositeHashOf(canonical) {
|
|
127
194
|
try {
|
|
128
|
-
|
|
129
|
-
return null;
|
|
130
|
-
const prefix = `${PERSISTENT_ID_COOKIE_NAME}=`;
|
|
131
|
-
for (const part of document.cookie.split(";")) {
|
|
132
|
-
const trimmed = part.trim();
|
|
133
|
-
if (trimmed.startsWith(prefix)) {
|
|
134
|
-
const value = decodeURIComponent(trimmed.substring(prefix.length));
|
|
135
|
-
return isValidPersistentId(value) ? value : null;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return null;
|
|
195
|
+
return await sha256_128(stableStringify(canonical));
|
|
139
196
|
}
|
|
140
197
|
catch {
|
|
141
|
-
return
|
|
198
|
+
return "";
|
|
142
199
|
}
|
|
143
200
|
}
|
|
144
|
-
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The device headers a Didit API reads. They carry the identifiers only; the signals themselves
|
|
204
|
+
* travel in the request body (`fingerprint_v2`), which has no size limit a proxy would enforce.
|
|
205
|
+
*/
|
|
206
|
+
function payloadToHeaders(payload) {
|
|
207
|
+
if (!payload)
|
|
208
|
+
return {};
|
|
209
|
+
const headers = { "X-Didit-FP-V2": "1" };
|
|
210
|
+
if (payload.persistentId)
|
|
211
|
+
headers["X-Didit-PID"] = payload.persistentId;
|
|
212
|
+
if (payload.compositeHash)
|
|
213
|
+
headers["X-Didit-FP-Hash"] = payload.compositeHash;
|
|
214
|
+
if (payload.legacyCompatibilityHash)
|
|
215
|
+
headers["X-Didit-FP-Legacy"] = payload.legacyCompatibilityHash;
|
|
216
|
+
if (payload.signals?.webview?.kind)
|
|
217
|
+
headers["X-Didit-FP-Webview"] = payload.signals.webview.kind;
|
|
218
|
+
if (payload.bot?.score)
|
|
219
|
+
headers["X-Didit-FP-Bot-Score"] = payload.bot.score.toFixed(2);
|
|
220
|
+
return headers;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function safe(read, fallback) {
|
|
145
224
|
try {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const isSecure = typeof location !== "undefined" && location.protocol === "https:";
|
|
149
|
-
const attributes = [
|
|
150
|
-
`${PERSISTENT_ID_COOKIE_NAME}=${encodeURIComponent(id)}`,
|
|
151
|
-
"Path=/",
|
|
152
|
-
`Max-Age=${PERSISTENT_ID_COOKIE_MAX_AGE_SECONDS}`,
|
|
153
|
-
"SameSite=Lax"
|
|
154
|
-
];
|
|
155
|
-
if (isSecure)
|
|
156
|
-
attributes.push("Secure");
|
|
157
|
-
document.cookie = attributes.join("; ");
|
|
225
|
+
const value = read();
|
|
226
|
+
return value === undefined ? fallback : value;
|
|
158
227
|
}
|
|
159
228
|
catch {
|
|
160
|
-
|
|
229
|
+
return fallback;
|
|
161
230
|
}
|
|
162
231
|
}
|
|
163
|
-
function
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
memoryPersistentId = id;
|
|
187
|
-
// Self-heal any layer that was missing or mismatched.
|
|
188
|
-
if (fromLocalStorage !== id)
|
|
189
|
-
writeLocalStorageId(id);
|
|
190
|
-
if (fromCookie !== id)
|
|
191
|
-
writeCookieId(id);
|
|
192
|
-
return { id, sources, wasCreated };
|
|
232
|
+
function quantizeConcurrency(value) {
|
|
233
|
+
if (value === null)
|
|
234
|
+
return null;
|
|
235
|
+
if (value <= 1)
|
|
236
|
+
return 1;
|
|
237
|
+
if (value <= 2)
|
|
238
|
+
return 2;
|
|
239
|
+
if (value <= 4)
|
|
240
|
+
return 4;
|
|
241
|
+
if (value <= 6)
|
|
242
|
+
return 6;
|
|
243
|
+
if (value <= 8)
|
|
244
|
+
return 8;
|
|
245
|
+
if (value <= 12)
|
|
246
|
+
return 12;
|
|
247
|
+
if (value <= 16)
|
|
248
|
+
return 16;
|
|
249
|
+
return 20;
|
|
250
|
+
}
|
|
251
|
+
function normalizeUserAgent(ua) {
|
|
252
|
+
// Collapse fine-grained version numbers (147.0.7727.137 → 147) so two installs of the same major version match.
|
|
253
|
+
return ua.replace(/(\d+)(\.\d+)+/g, "$1");
|
|
193
254
|
}
|
|
194
255
|
function collectNavigatorSignals() {
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
const uaData = nav
|
|
205
|
-
? nav
|
|
206
|
-
.userAgentData
|
|
207
|
-
: undefined;
|
|
256
|
+
// Outside a browser there is no navigator: every read below then falls back to its empty value.
|
|
257
|
+
const present = typeof navigator !== "undefined";
|
|
258
|
+
const nav = (present ? navigator : {});
|
|
259
|
+
const userAgent = safe(() => nav.userAgent || "", "");
|
|
260
|
+
const concurrency = safe(() => nav.hardwareConcurrency ?? null, null);
|
|
261
|
+
const connection = safe(() => nav.connection || nav.mozConnection || nav.webkitConnection || null, null);
|
|
262
|
+
const screenObj = present && typeof screen !== "undefined" ? screen : null;
|
|
263
|
+
const uaData = safe(() => nav.userAgentData, undefined);
|
|
208
264
|
return {
|
|
209
|
-
userAgent
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
265
|
+
userAgent,
|
|
266
|
+
userAgentNormalized: normalizeUserAgent(userAgent),
|
|
267
|
+
platform: safe(() => nav.platform || "", ""),
|
|
268
|
+
language: safe(() => nav.language || "", ""),
|
|
269
|
+
languages: safe(() => (nav.languages ? [...nav.languages] : []), []),
|
|
270
|
+
hardwareConcurrency: concurrency,
|
|
271
|
+
hardwareConcurrencyQuantized: quantizeConcurrency(concurrency),
|
|
272
|
+
deviceMemory: safe(() => nav.deviceMemory ?? null, null),
|
|
273
|
+
maxTouchPoints: safe(() => nav.maxTouchPoints ?? null, null),
|
|
274
|
+
devicePixelRatio: safe(() => (present && typeof window !== "undefined" ? (window.devicePixelRatio ?? null) : null), null),
|
|
275
|
+
screenWidth: screenObj?.width ?? null,
|
|
276
|
+
screenHeight: screenObj?.height ?? null,
|
|
277
|
+
availWidth: screenObj?.availWidth ?? null,
|
|
278
|
+
availHeight: screenObj?.availHeight ?? null,
|
|
279
|
+
colorDepth: screenObj?.colorDepth ?? null,
|
|
280
|
+
pixelDepth: screenObj?.pixelDepth ?? null,
|
|
281
|
+
orientation: screenObj && "orientation" in screenObj ? (screenObj.orientation?.type ?? null) : null,
|
|
282
|
+
timeZone: safe(() => (present && typeof Intl !== "undefined" && Intl.DateTimeFormat ? Intl.DateTimeFormat().resolvedOptions().timeZone || "" : ""), ""),
|
|
283
|
+
connectionEffectiveType: connection?.effectiveType ?? null,
|
|
284
|
+
connectionDownlink: connection?.downlink ?? null,
|
|
285
|
+
uaDataMobile: uaData?.mobile ?? null,
|
|
224
286
|
uaDataPlatform: uaData?.platform ?? null,
|
|
225
|
-
|
|
226
|
-
|
|
287
|
+
uaDataBrands: uaData?.brands ? uaData.brands.map((b) => `${b.brand}:${b.version}`).sort() : [],
|
|
288
|
+
cookieEnabled: safe(() => nav.cookieEnabled ?? null, null),
|
|
289
|
+
doNotTrack: safe(() => nav.doNotTrack ?? null, null),
|
|
227
290
|
};
|
|
228
291
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
292
|
+
|
|
293
|
+
function detectWebViewContext(userAgent) {
|
|
294
|
+
if (!userAgent) {
|
|
295
|
+
return { kind: "browser", hostAppVersion: null, isStandaloneBrowser: true };
|
|
296
|
+
}
|
|
297
|
+
const ua = userAgent;
|
|
298
|
+
const waMatch = ua.match(/WA(?:4[A]|4iOS)\/([\d.]+)/);
|
|
299
|
+
if (waMatch)
|
|
300
|
+
return { kind: "whatsapp", hostAppVersion: waMatch[1], isStandaloneBrowser: false };
|
|
301
|
+
// Google app (iOS/Android) in-app browser. On iOS its WKWebView exposes no
|
|
302
|
+
// navigator.mediaDevices at all, so no capture is ever possible there.
|
|
303
|
+
const gsaMatch = ua.match(/GSA\/([\d.]+)/);
|
|
304
|
+
if (gsaMatch)
|
|
305
|
+
return { kind: "gsa", hostAppVersion: gsaMatch[1], isStandaloneBrowser: false };
|
|
306
|
+
const igMatch = ua.match(/Instagram\s+([\d.]+)/);
|
|
307
|
+
if (igMatch)
|
|
308
|
+
return { kind: "instagram", hostAppVersion: igMatch[1], isStandaloneBrowser: false };
|
|
309
|
+
if (/FBAN|FBAV/.test(ua)) {
|
|
310
|
+
const fbVer = ua.match(/FBAV\/([\d.]+)/);
|
|
311
|
+
return { kind: "facebook", hostAppVersion: fbVer?.[1] ?? null, isStandaloneBrowser: false };
|
|
312
|
+
}
|
|
313
|
+
if (/MessengerLiteForiOS|MessengerForiOS|FB_IAB\/Messenger/.test(ua)) {
|
|
314
|
+
return { kind: "messenger", hostAppVersion: null, isStandaloneBrowser: false };
|
|
315
|
+
}
|
|
316
|
+
const ttMatch = ua.match(/(?:musical_ly|TikTok|trill)_(\d+)/i);
|
|
317
|
+
if (ttMatch)
|
|
318
|
+
return { kind: "tiktok", hostAppVersion: ttMatch[1], isStandaloneBrowser: false };
|
|
319
|
+
const lineMatch = ua.match(/Line\/([\d.]+)/);
|
|
320
|
+
if (lineMatch)
|
|
321
|
+
return { kind: "line", hostAppVersion: lineMatch[1], isStandaloneBrowser: false };
|
|
322
|
+
if (/MicroMessenger/.test(ua)) {
|
|
323
|
+
const wcVer = ua.match(/MicroMessenger\/([\d.]+)/);
|
|
324
|
+
return { kind: "wechat", hostAppVersion: wcVer?.[1] ?? null, isStandaloneBrowser: false };
|
|
325
|
+
}
|
|
326
|
+
if (/Telegram/i.test(ua)) {
|
|
327
|
+
return { kind: "telegram", hostAppVersion: null, isStandaloneBrowser: false };
|
|
328
|
+
}
|
|
329
|
+
if (/Twitter|TwitterAndroid/i.test(ua)) {
|
|
330
|
+
return { kind: "twitter", hostAppVersion: null, isStandaloneBrowser: false };
|
|
331
|
+
}
|
|
332
|
+
if (/LinkedInApp/i.test(ua)) {
|
|
333
|
+
return { kind: "linkedin", hostAppVersion: null, isStandaloneBrowser: false };
|
|
334
|
+
}
|
|
335
|
+
if (/Snapchat/i.test(ua)) {
|
|
336
|
+
return { kind: "snapchat", hostAppVersion: null, isStandaloneBrowser: false };
|
|
337
|
+
}
|
|
338
|
+
if (/\bwv\)|\bWebView\b/i.test(ua)) {
|
|
339
|
+
return { kind: "generic-wv", hostAppVersion: null, isStandaloneBrowser: false };
|
|
340
|
+
}
|
|
341
|
+
return { kind: "browser", hostAppVersion: null, isStandaloneBrowser: true };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const STORAGE_KEY = "didit-pid-v1";
|
|
345
|
+
const COOKIE_NAME = "didit_pid";
|
|
346
|
+
const COOKIE_MAX_AGE_SECONDS = 60 * 60 * 24 * 365 * 2;
|
|
347
|
+
const PERSISTENT_ID_BYTE_LENGTH = 16;
|
|
348
|
+
let memoryFallback = null;
|
|
349
|
+
function isValidPersistentId(value) {
|
|
350
|
+
return typeof value === "string" && /^[A-Za-z0-9_-]{20,32}$/.test(value);
|
|
351
|
+
}
|
|
352
|
+
const localStorageLayer = {
|
|
353
|
+
source: "localStorage",
|
|
354
|
+
read() {
|
|
355
|
+
try {
|
|
356
|
+
if (typeof localStorage === "undefined")
|
|
357
|
+
return null;
|
|
358
|
+
const value = localStorage.getItem(STORAGE_KEY);
|
|
359
|
+
return isValidPersistentId(value) ? value : null;
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
// Storage may be blocked (private mode, storage policies): the other layers still apply.
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
write(id) {
|
|
367
|
+
try {
|
|
368
|
+
if (typeof localStorage === "undefined")
|
|
369
|
+
return false;
|
|
370
|
+
localStorage.setItem(STORAGE_KEY, id);
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
catch {
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
},
|
|
377
|
+
};
|
|
378
|
+
const cookieLayer = {
|
|
379
|
+
source: "cookie",
|
|
380
|
+
read() {
|
|
381
|
+
try {
|
|
382
|
+
if (typeof document === "undefined" || !document.cookie)
|
|
383
|
+
return null;
|
|
384
|
+
const prefix = `${COOKIE_NAME}=`;
|
|
385
|
+
for (const part of document.cookie.split(";")) {
|
|
386
|
+
const trimmed = part.trim();
|
|
387
|
+
if (trimmed.startsWith(prefix)) {
|
|
388
|
+
const value = decodeURIComponent(trimmed.substring(prefix.length));
|
|
389
|
+
return isValidPersistentId(value) ? value : null;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return null;
|
|
393
|
+
}
|
|
394
|
+
catch {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
write(id) {
|
|
399
|
+
try {
|
|
400
|
+
if (typeof document === "undefined")
|
|
401
|
+
return false;
|
|
402
|
+
const isSecure = typeof location !== "undefined" && location.protocol === "https:";
|
|
403
|
+
const attributes = [
|
|
404
|
+
`${COOKIE_NAME}=${encodeURIComponent(id)}`,
|
|
405
|
+
"Path=/",
|
|
406
|
+
`Max-Age=${COOKIE_MAX_AGE_SECONDS}`,
|
|
407
|
+
"SameSite=Lax",
|
|
408
|
+
];
|
|
409
|
+
if (isSecure)
|
|
410
|
+
attributes.push("Secure");
|
|
411
|
+
document.cookie = attributes.join("; ");
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
catch {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
},
|
|
418
|
+
};
|
|
419
|
+
/** The layers every browser answers at once. `storage-layers.ts` adds the ones that have to wait. */
|
|
420
|
+
const SYNC_STORAGE_LAYERS = [localStorageLayer, cookieLayer];
|
|
421
|
+
/** The value most layers agree on; on a tie, the layer listed first. */
|
|
422
|
+
function pickWinner(candidates) {
|
|
423
|
+
const tally = new Map();
|
|
424
|
+
for (const { id, source } of candidates) {
|
|
425
|
+
const existing = tally.get(id);
|
|
426
|
+
if (existing) {
|
|
427
|
+
existing.push(source);
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
tally.set(id, [source]);
|
|
431
|
+
}
|
|
235
432
|
}
|
|
236
|
-
|
|
237
|
-
|
|
433
|
+
let winner = candidates[0].id;
|
|
434
|
+
let winnerCount = 0;
|
|
435
|
+
for (const [id, sources] of tally) {
|
|
436
|
+
if (sources.length > winnerCount) {
|
|
437
|
+
winnerCount = sources.length;
|
|
438
|
+
winner = id;
|
|
439
|
+
}
|
|
238
440
|
}
|
|
239
|
-
return
|
|
441
|
+
return { id: winner, sources: tally.get(winner) || [] };
|
|
240
442
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
443
|
+
/**
|
|
444
|
+
* From what each layer answered: the id this browser profile is known by (the value most layers
|
|
445
|
+
* agree on, the one kept in memory when storage is blocked, or a new random one), and the layers
|
|
446
|
+
* that did not have it, for the caller to write it back to.
|
|
447
|
+
*/
|
|
448
|
+
function choosePersistentId(layers, values, embedderId) {
|
|
449
|
+
const candidates = [];
|
|
450
|
+
values.forEach((value, index) => {
|
|
451
|
+
if (value)
|
|
452
|
+
candidates.push({ id: value, source: layers[index].source });
|
|
453
|
+
});
|
|
454
|
+
// No layer has an id, and the page that embeds this one passed its own: this visit goes by it.
|
|
455
|
+
// It is not written to any layer and not kept in memory: the embedding page is where it lives,
|
|
456
|
+
// and a value another page supplies never becomes this origin's stored id.
|
|
457
|
+
if (!candidates.length && isValidPersistentId(embedderId)) {
|
|
458
|
+
return { result: { id: embedderId, sources: ["embedder"], wasCreated: false }, stale: [] };
|
|
246
459
|
}
|
|
247
|
-
if (
|
|
248
|
-
|
|
460
|
+
if (!candidates.length && memoryFallback && isValidPersistentId(memoryFallback)) {
|
|
461
|
+
candidates.push({ id: memoryFallback, source: "memory" });
|
|
249
462
|
}
|
|
250
|
-
|
|
251
|
-
|
|
463
|
+
let result;
|
|
464
|
+
if (candidates.length) {
|
|
465
|
+
const winner = pickWinner(candidates);
|
|
466
|
+
result = { id: winner.id, sources: winner.sources, wasCreated: false };
|
|
252
467
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
function canonicalizeSignals(signals) {
|
|
256
|
-
// Only drift-resistant fields, serialized with stable key order so the same
|
|
257
|
-
// device always produces the same composite hash.
|
|
258
|
-
const stable = {
|
|
259
|
-
platform: signals.platform,
|
|
260
|
-
navigator: {
|
|
261
|
-
ua: signals.navigator.userAgent,
|
|
262
|
-
platform: signals.navigator.platform,
|
|
263
|
-
lang: signals.navigator.language.split("-")[0],
|
|
264
|
-
languages: [...signals.navigator.languages].sort(),
|
|
265
|
-
concurrency: signals.navigator.hardwareConcurrency,
|
|
266
|
-
memory: signals.navigator.deviceMemory,
|
|
267
|
-
maxTouch: signals.navigator.maxTouchPoints,
|
|
268
|
-
dpr: signals.navigator.devicePixelRatio,
|
|
269
|
-
screen: [signals.navigator.screenWidth, signals.navigator.screenHeight],
|
|
270
|
-
colorDepth: signals.navigator.colorDepth,
|
|
271
|
-
timeZone: signals.navigator.timeZone,
|
|
272
|
-
uaDataPlatform: signals.navigator.uaDataPlatform,
|
|
273
|
-
uaDataBrands: signals.navigator.uaDataBrands
|
|
274
|
-
},
|
|
275
|
-
webview: signals.webview.kind
|
|
276
|
-
};
|
|
277
|
-
return stableStringify(stable);
|
|
278
|
-
}
|
|
279
|
-
async function computeCompositeHash(canonical) {
|
|
280
|
-
try {
|
|
281
|
-
return await sha256_128(canonical);
|
|
468
|
+
else {
|
|
469
|
+
result = { id: randomBase64Url(PERSISTENT_ID_BYTE_LENGTH), sources: ["fresh"], wasCreated: true };
|
|
282
470
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
471
|
+
memoryFallback = result.id;
|
|
472
|
+
return { result, stale: layers.filter((_, index) => values[index] !== result.id) };
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* The stored id from the layers that answer at once (localStorage and a cookie), written back to
|
|
476
|
+
* the one that missed it. `loadOrCreatePersistentIdFromBrowserStorage` (storage-layers.ts) reads
|
|
477
|
+
* the slow layers too.
|
|
478
|
+
*/
|
|
479
|
+
function loadOrCreatePersistentId() {
|
|
480
|
+
const layers = SYNC_STORAGE_LAYERS;
|
|
481
|
+
const { result, stale } = choosePersistentId(layers, layers.map((layer) => layer.read()));
|
|
482
|
+
stale.forEach((layer) => layer.write(result.id));
|
|
483
|
+
return result;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* The id this page already keeps, or null. It only reads: nothing is created and nothing is
|
|
487
|
+
* written, so asking never adds a cookie or a storage entry to a site that had none.
|
|
488
|
+
*/
|
|
489
|
+
function readStoredPersistentId() {
|
|
490
|
+
for (const layer of SYNC_STORAGE_LAYERS) {
|
|
491
|
+
const value = layer.read();
|
|
492
|
+
if (value)
|
|
493
|
+
return value;
|
|
286
494
|
}
|
|
495
|
+
return null;
|
|
287
496
|
}
|
|
497
|
+
|
|
498
|
+
let provider = null;
|
|
499
|
+
let cachedPayload = null;
|
|
500
|
+
let pendingPayload = null;
|
|
501
|
+
let cachedPayloadPromise = null;
|
|
502
|
+
/** Raised when the source of the payload changes, so a collection started before it is recognised. */
|
|
503
|
+
let generation = 0;
|
|
288
504
|
async function buildPayload() {
|
|
505
|
+
// These layers answer at once, so the id is in the payload before anything is awaited.
|
|
289
506
|
const persistent = loadOrCreatePersistentId();
|
|
290
507
|
const navigatorSignals = collectNavigatorSignals();
|
|
291
|
-
const
|
|
292
|
-
platform: "web",
|
|
293
|
-
navigator: navigatorSignals,
|
|
294
|
-
webview: { kind: detectWebViewKind(navigatorSignals.userAgent) }
|
|
295
|
-
};
|
|
508
|
+
const webview = detectWebViewContext(navigatorSignals.userAgent);
|
|
296
509
|
const payload = {
|
|
297
510
|
version: 2,
|
|
298
511
|
schema: "didit-fp-v2",
|
|
@@ -301,14 +514,24 @@
|
|
|
301
514
|
persistentId: persistent.id,
|
|
302
515
|
persistentIdSources: persistent.sources,
|
|
303
516
|
persistentIdWasCreated: persistent.wasCreated,
|
|
304
|
-
signals
|
|
305
|
-
|
|
306
|
-
|
|
517
|
+
signals: {
|
|
518
|
+
platform: "web",
|
|
519
|
+
tier: "lite",
|
|
520
|
+
compositeHashVersion: COMPOSITE_HASH_VERSION,
|
|
521
|
+
navigator: navigatorSignals,
|
|
522
|
+
webview,
|
|
523
|
+
},
|
|
524
|
+
bot: detectBotFromNavigator(navigatorSignals),
|
|
525
|
+
compositeHash: "",
|
|
307
526
|
};
|
|
308
527
|
// Keep all synchronously collected context available during the native hash.
|
|
309
528
|
// An empty hash is omitted from headers, never replaced with a different identity.
|
|
310
529
|
pendingPayload = payload;
|
|
311
|
-
|
|
530
|
+
const compositeHash = await compositeHashOf({
|
|
531
|
+
navigator: canonicalNavigator(navigatorSignals),
|
|
532
|
+
webview: webview.kind,
|
|
533
|
+
});
|
|
534
|
+
return { ...payload, compositeHash };
|
|
312
535
|
}
|
|
313
536
|
function isBrowserEnvironment() {
|
|
314
537
|
return typeof window !== "undefined" && typeof navigator !== "undefined" && typeof document !== "undefined";
|
|
@@ -325,10 +548,18 @@
|
|
|
325
548
|
if (cachedPayload)
|
|
326
549
|
return cachedPayload;
|
|
327
550
|
if (!cachedPayloadPromise) {
|
|
328
|
-
|
|
551
|
+
const started = generation;
|
|
552
|
+
cachedPayloadPromise = (provider ? provider.get() : buildPayload()).then((payload) => {
|
|
553
|
+
// The source changed while this was being collected (`enableDeviceIntelligence()` called
|
|
554
|
+
// with the lite collection still running): its result must not become the cached payload,
|
|
555
|
+
// and whoever waited for it gets the payload of the source that replaced it.
|
|
556
|
+
if (started !== generation)
|
|
557
|
+
return getDeviceFingerprintPayload();
|
|
329
558
|
cachedPayload = payload;
|
|
330
559
|
return payload;
|
|
331
560
|
}, (error) => {
|
|
561
|
+
if (started !== generation)
|
|
562
|
+
return getDeviceFingerprintPayload();
|
|
332
563
|
// Do not cache failures; a later call may succeed.
|
|
333
564
|
cachedPayloadPromise = null;
|
|
334
565
|
throw error;
|
|
@@ -338,22 +569,27 @@
|
|
|
338
569
|
}
|
|
339
570
|
/** Internal exit snapshot: never waits for WebCrypto or changes the cached payload. */
|
|
340
571
|
function getAvailableDeviceFingerprintPayload() {
|
|
341
|
-
return cachedPayload ?? pendingPayload;
|
|
342
|
-
}
|
|
343
|
-
function buildFingerprintHeaders(payload) {
|
|
344
|
-
if (!payload)
|
|
345
|
-
return {};
|
|
346
|
-
const headers = { "X-Didit-FP-V2": "1" };
|
|
347
|
-
if (payload.persistentId)
|
|
348
|
-
headers["X-Didit-PID"] = payload.persistentId;
|
|
349
|
-
if (payload.compositeHash)
|
|
350
|
-
headers["X-Didit-FP-Hash"] = payload.compositeHash;
|
|
351
|
-
if (payload.signals.webview.kind)
|
|
352
|
-
headers["X-Didit-FP-Webview"] = payload.signals.webview.kind;
|
|
353
|
-
if (payload.bot.score)
|
|
354
|
-
headers["X-Didit-FP-Bot-Score"] = payload.bot.score.toFixed(2);
|
|
355
|
-
return headers;
|
|
572
|
+
return cachedPayload ?? (provider ? provider.peek() : pendingPayload);
|
|
356
573
|
}
|
|
574
|
+
/**
|
|
575
|
+
* Makes another collector the source of the fingerprint this entry sends (null: back to the lite
|
|
576
|
+
* one). `enableDeviceIntelligence()` of `@didit-protocol/signals/device` calls it; nothing else should.
|
|
577
|
+
*/
|
|
578
|
+
function setDeviceSignalsProvider(next) {
|
|
579
|
+
provider = next;
|
|
580
|
+
generation += 1;
|
|
581
|
+
cachedPayload = null;
|
|
582
|
+
pendingPayload = null;
|
|
583
|
+
cachedPayloadPromise = null;
|
|
584
|
+
}
|
|
585
|
+
/** The device headers a Didit API reads from the payload. */
|
|
586
|
+
const buildFingerprintHeaders = payloadToHeaders;
|
|
587
|
+
/**
|
|
588
|
+
* The device id this page already keeps for Didit, or null when it keeps none. It never creates
|
|
589
|
+
* one: `@didit-protocol/sdk-web` hands it to the verification it opens, so a site that sends no
|
|
590
|
+
* events, transactions or sign-ins gets no cookie and no storage entry from being asked.
|
|
591
|
+
*/
|
|
592
|
+
const getStoredDeviceId = readStoredPersistentId;
|
|
357
593
|
|
|
358
594
|
const DEFAULT_EVENTS_BASE_URL = "https://verification.didit.me";
|
|
359
595
|
const BATCH_PATH = "/v1/events/batch/";
|
|
@@ -367,6 +603,8 @@
|
|
|
367
603
|
const NEVER_DROPPED = /^(security|payment|dispute|responsible_gambling)\.|^gameplay\.(bet_placed|bet_settled|p2p_settlement)$/;
|
|
368
604
|
const LOW_VALUE = /^session\.(page_view|sdk_heartbeat|sdk_loaded|device_seen|bot_signal_observed)$|^bonus\.promotion_viewed$|^affiliate\.referral_clicked$/;
|
|
369
605
|
const PRIORITY_NEVER_DROPPED = 2;
|
|
606
|
+
/** What `fetch` accepts as a header value: no control character but the tab, nothing above U+00FF. */
|
|
607
|
+
const HEADER_VALUE = /^[\t\x20-\x7e\x80-\xff]+$/;
|
|
370
608
|
/**
|
|
371
609
|
* Typed error for events Didit refused or the SDK had to drop. Thrown by
|
|
372
610
|
* submitEvent for a missing token or event, otherwise delivered to the
|
|
@@ -397,6 +635,16 @@
|
|
|
397
635
|
let listening = false;
|
|
398
636
|
let exiting = false;
|
|
399
637
|
let keepaliveBytes = 0;
|
|
638
|
+
/**
|
|
639
|
+
* `value` without the slashes it ends with. A loop, not a regular expression: `/\/+$/` rescans a
|
|
640
|
+
* long run of slashes from every one of them.
|
|
641
|
+
*/
|
|
642
|
+
function withoutTrailingSlashes(value) {
|
|
643
|
+
let end = value.length;
|
|
644
|
+
while (end > 0 && value.charCodeAt(end - 1) === 47)
|
|
645
|
+
end -= 1;
|
|
646
|
+
return value.slice(0, end);
|
|
647
|
+
}
|
|
400
648
|
function isRecord(value) {
|
|
401
649
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
402
650
|
}
|
|
@@ -704,7 +952,7 @@
|
|
|
704
952
|
*/
|
|
705
953
|
function configureEvents(configuration) {
|
|
706
954
|
const next = { ...settings, ...compact$1({ ...configuration }) };
|
|
707
|
-
next.baseUrl = next.baseUrl
|
|
955
|
+
next.baseUrl = withoutTrailingSlashes(next.baseUrl);
|
|
708
956
|
next.batchSize = Math.min(Math.max(1, Math.floor(next.batchSize)), MAX_BATCH_EVENTS);
|
|
709
957
|
next.flushIntervalMs = Math.max(0, next.flushIntervalMs);
|
|
710
958
|
next.maxQueueSize = Math.max(1, Math.floor(next.maxQueueSize));
|
|
@@ -724,11 +972,17 @@
|
|
|
724
972
|
* environment has no fetch.
|
|
725
973
|
*/
|
|
726
974
|
function submitEvent(options) {
|
|
727
|
-
|
|
975
|
+
// Without the white space around it, as `fetch` sends a header value.
|
|
976
|
+
const token = typeof options?.monitoringToken === "string" ? options.monitoringToken.trim() : "";
|
|
728
977
|
const event = options?.event;
|
|
729
|
-
if (!token
|
|
978
|
+
if (!token) {
|
|
730
979
|
throw new DiditEventError("invalid_token", "monitoringToken is required.");
|
|
731
980
|
}
|
|
981
|
+
// A line break or a NUL inside it, or a character above U+00FF, makes `fetch` throw before any
|
|
982
|
+
// request is made. Queued, such an event looked like a network failure and was retried forever.
|
|
983
|
+
if (!HEADER_VALUE.test(token)) {
|
|
984
|
+
throw new DiditEventError("invalid_token", "monitoringToken is not a value a header can carry.");
|
|
985
|
+
}
|
|
732
986
|
if (!isRecord(event) || !event.category || typeof event.actionType !== "string" || !event.actionType.trim()) {
|
|
733
987
|
throw new DiditEventError("validation", "event.category and event.actionType are required.");
|
|
734
988
|
}
|
|
@@ -747,7 +1001,7 @@
|
|
|
747
1001
|
// UTF-8 length; JSON.stringify never leaves a lone surrogate for encodeURIComponent to reject.
|
|
748
1002
|
bytes: byteLength(json),
|
|
749
1003
|
token,
|
|
750
|
-
url: `${(options.baseUrl ?? settings.baseUrl)
|
|
1004
|
+
url: `${withoutTrailingSlashes(options.baseUrl ?? settings.baseUrl)}${BATCH_PATH}`,
|
|
751
1005
|
priority: NEVER_DROPPED.test(kind) ? PRIORITY_NEVER_DROPPED : LOW_VALUE.test(kind) ? 0 : 1,
|
|
752
1006
|
sending: false
|
|
753
1007
|
});
|
|
@@ -793,12 +1047,14 @@
|
|
|
793
1047
|
closeIcon: "didit-close-icon",
|
|
794
1048
|
closeIconLegacy: "didit-close-icon-legacy",
|
|
795
1049
|
flowSized: "didit-flow-sized",
|
|
1050
|
+
sizeAnimated: "didit-size-animated",
|
|
796
1051
|
loading: "didit-loading",
|
|
797
1052
|
confirmOverlay: "didit-confirm-overlay",
|
|
798
1053
|
confirmBox: "didit-confirm-box",
|
|
799
|
-
embedded: "didit-embedded"
|
|
1054
|
+
embedded: "didit-embedded",
|
|
1055
|
+
curtain: "didit-modal-curtain"
|
|
800
1056
|
};
|
|
801
|
-
const SDK_VERSION = "0.
|
|
1057
|
+
const SDK_VERSION = "0.6.0";
|
|
802
1058
|
/**
|
|
803
1059
|
* The permissions the verification iframe gets, in a modal and embedded:
|
|
804
1060
|
* camera and microphone for capture, clipboard-write for the flow's copy
|
|
@@ -824,9 +1080,16 @@
|
|
|
824
1080
|
presentation: "sdk_presentation",
|
|
825
1081
|
closeButton: "sdk_close_button"
|
|
826
1082
|
};
|
|
1083
|
+
/**
|
|
1084
|
+
* The verification-URL query parameter that carries the device id this page
|
|
1085
|
+
* already keeps for Didit, when it keeps one. A browser gives the flow in the
|
|
1086
|
+
* frame storage of its own, apart from this page's, so without it a
|
|
1087
|
+
* verification and this site's events never share a device id.
|
|
1088
|
+
*/
|
|
1089
|
+
const DEVICE_ID_QUERY_PARAM = "sdk_device_id";
|
|
827
1090
|
/**
|
|
828
1091
|
* Posted by the hosted flow when it lays itself out for the modal, with
|
|
829
|
-
* `data: { contentHeight, colorScheme?, dir? }
|
|
1092
|
+
* `data: { contentHeight, colorScheme?, dir?, kind? }` (`kind` is `handoff` or `step`, `step` when missing). The modal then grows to the
|
|
830
1093
|
* handoff card on a computer and follows the content height in a narrow
|
|
831
1094
|
* window. Handled by the modal and never delivered to `onEvent`.
|
|
832
1095
|
*/
|
|
@@ -950,15 +1213,21 @@
|
|
|
950
1213
|
* `showDocumentCameraSwitchButton` -> `document_camera_switch=true|false`,
|
|
951
1214
|
* `showLivenessCameraSwitchButton` -> `liveness_camera_switch=true|false`.
|
|
952
1215
|
* How the SDK presents the flow is always added: `sdk_presentation=modal|embedded`
|
|
953
|
-
* and, in a modal, `sdk_close_button=true|false`.
|
|
1216
|
+
* and, in a modal, `sdk_close_button=true|false`. `storedDeviceId`, when the
|
|
1217
|
+
* page has one, is added as `sdk_device_id`.
|
|
954
1218
|
* Each parameter is set (not appended) so the configuration wins over a value
|
|
955
1219
|
* already on the URL; existing query parameters and the hash are kept. A
|
|
956
1220
|
* value of the wrong shape is ignored with a warning rather than forwarded,
|
|
957
1221
|
* and a URL the browser cannot parse is returned as it came (the modal
|
|
958
1222
|
* reports it when it loads).
|
|
959
1223
|
*/
|
|
960
|
-
function buildVerificationUrl(url, configuration = {}) {
|
|
1224
|
+
function buildVerificationUrl(url, configuration = {}, storedDeviceId = null) {
|
|
961
1225
|
const params = presentationParams(configuration);
|
|
1226
|
+
// The id this page already keeps for Didit (see getStoredDeviceId): the flow in the frame, whose
|
|
1227
|
+
// own storage the browser keeps apart from this page's, can then be told apart as the same
|
|
1228
|
+
// device as this site's events. Absent when the page keeps none; nothing is created for it.
|
|
1229
|
+
if (storedDeviceId)
|
|
1230
|
+
params.push([DEVICE_ID_QUERY_PARAM, storedDeviceId]);
|
|
962
1231
|
for (const option of CAMERA_LENS_OPTIONS) {
|
|
963
1232
|
const value = configuration[option];
|
|
964
1233
|
if (value === undefined)
|
|
@@ -1460,6 +1729,12 @@
|
|
|
1460
1729
|
return translationsMap[language] ?? EN;
|
|
1461
1730
|
}
|
|
1462
1731
|
|
|
1732
|
+
/** The length of the one animation when the flow moves from the handoff card to a step or back. */
|
|
1733
|
+
const SIZE_ANIMATION_MS = 300;
|
|
1734
|
+
/** How long the modal waits for the first layout before it shows at step size, so it can never hang. */
|
|
1735
|
+
const FIRST_LAYOUT_TIMEOUT_MS = 3000;
|
|
1736
|
+
/** The flow's ground (site-ground in the design system), which a full-screen modal paints the page with. */
|
|
1737
|
+
const FLOW_GROUND = { light: "#ffffff", dark: "#0b0b0d" };
|
|
1463
1738
|
class VerificationModal {
|
|
1464
1739
|
constructor(configuration, callbacks) {
|
|
1465
1740
|
this.state = {
|
|
@@ -1477,11 +1752,14 @@
|
|
|
1477
1752
|
this.boundHandleKeydown = null;
|
|
1478
1753
|
this.boundHandleFocusChange = null;
|
|
1479
1754
|
this.confirmationFocusTimeout = null;
|
|
1755
|
+
this.firstLayoutTimeout = null;
|
|
1756
|
+
this.sizeTimeout = null;
|
|
1480
1757
|
this.embedded = false;
|
|
1481
1758
|
this.embeddedContainer = null;
|
|
1482
1759
|
this.language = "en";
|
|
1483
|
-
/**
|
|
1484
|
-
this.
|
|
1760
|
+
/** The ground below the layout viewport (see showCurtain), while this modal is open. */
|
|
1761
|
+
this.curtain = null;
|
|
1762
|
+
this.boundPlaceCurtain = null;
|
|
1485
1763
|
this.modalId = generateModalId();
|
|
1486
1764
|
this.config = {
|
|
1487
1765
|
zIndex: configuration?.zIndex ?? DEFAULT_CONFIG.zIndex,
|
|
@@ -1524,9 +1802,9 @@
|
|
|
1524
1802
|
.${CSS_CLASSES.container} {
|
|
1525
1803
|
position: relative;
|
|
1526
1804
|
width: 100%;
|
|
1527
|
-
max-width:
|
|
1528
|
-
|
|
1529
|
-
|
|
1805
|
+
max-width: 440px;
|
|
1806
|
+
height: min(860px, calc(100vh - 2rem));
|
|
1807
|
+
height: min(860px, calc(100dvh - 2rem));
|
|
1530
1808
|
border-radius: 16px;
|
|
1531
1809
|
overflow: hidden;
|
|
1532
1810
|
background: transparent;
|
|
@@ -1538,9 +1816,7 @@
|
|
|
1538
1816
|
|
|
1539
1817
|
.${CSS_CLASSES.iframe} {
|
|
1540
1818
|
width: 100%;
|
|
1541
|
-
height:
|
|
1542
|
-
height: 90dvh;
|
|
1543
|
-
max-height: 700px;
|
|
1819
|
+
height: 100%;
|
|
1544
1820
|
border: none;
|
|
1545
1821
|
display: block;
|
|
1546
1822
|
}
|
|
@@ -1577,10 +1853,17 @@
|
|
|
1577
1853
|
display: flex;
|
|
1578
1854
|
align-items: center;
|
|
1579
1855
|
justify-content: center;
|
|
1580
|
-
|
|
1856
|
+
/* The flow's ground, so a phone's loader meets the ground below the viewport without a seam. */
|
|
1857
|
+
background: ${FLOW_GROUND.light};
|
|
1581
1858
|
z-index: 5;
|
|
1582
1859
|
}
|
|
1583
1860
|
|
|
1861
|
+
@media (prefers-color-scheme: dark) {
|
|
1862
|
+
.${CSS_CLASSES.loading} {
|
|
1863
|
+
background: ${FLOW_GROUND.dark};
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1584
1867
|
.${CSS_CLASSES.loading}.hidden {
|
|
1585
1868
|
display: none;
|
|
1586
1869
|
}
|
|
@@ -1702,6 +1985,58 @@
|
|
|
1702
1985
|
outline-offset: 2px;
|
|
1703
1986
|
}
|
|
1704
1987
|
|
|
1988
|
+
/*
|
|
1989
|
+
* Steps are phone-sized (440 x 860, above). On a computer the handoff card is wider
|
|
1990
|
+
* (board Start-Embedded): 960 x 660 on a window of 1000px or more, never wider than the
|
|
1991
|
+
* window less its margin and never taller than 90% of it. In a narrower window the
|
|
1992
|
+
* page stacks and the height follows its content (--didit-content-height, set from the
|
|
1993
|
+
* flow's layout message). Until the flow reports what it shows, the modal is the handoff
|
|
1994
|
+
* card at its final size with the spinner inside it (the flow stays hidden and lays itself
|
|
1995
|
+
* out for that size): it never opens small and grows. The first change of kind (a step
|
|
1996
|
+
* instead of the handoff, or the person continues on this computer) animates once.
|
|
1997
|
+
*/
|
|
1998
|
+
@media (min-width: 541px) {
|
|
1999
|
+
.${CSS_CLASSES.container}[data-didit-kind="handoff"],
|
|
2000
|
+
.${CSS_CLASSES.container}[data-didit-kind="pending"] {
|
|
2001
|
+
max-width: min(960px, calc(100vw - 2rem));
|
|
2002
|
+
height: min(var(--didit-content-height, 660px), calc(100vh * 0.9));
|
|
2003
|
+
height: min(var(--didit-content-height, 660px), calc(100dvh * 0.9));
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
.${CSS_CLASSES.container}.${CSS_CLASSES.sizeAnimated} {
|
|
2007
|
+
transition: max-width 0.25s ease, height 0.25s ease;
|
|
2008
|
+
}
|
|
2009
|
+
|
|
2010
|
+
.${CSS_CLASSES.container}[data-didit-kind="pending"] .${CSS_CLASSES.iframe} {
|
|
2011
|
+
visibility: hidden;
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
/*
|
|
2016
|
+
* While pending the card already looks like the first screen (ground, radius, shadow and
|
|
2017
|
+
* close button come from the flow-sized rules below, with the device theme); the loader
|
|
2018
|
+
* sits inside it on the card's own ground.
|
|
2019
|
+
*/
|
|
2020
|
+
.${CSS_CLASSES.container}[data-didit-kind="pending"] .${CSS_CLASSES.loading} {
|
|
2021
|
+
background: transparent;
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
.${CSS_CLASSES.container}[data-didit-kind="pending"][data-didit-theme="dark"] .${CSS_CLASSES.loading} circle {
|
|
2025
|
+
stroke: rgba(255, 255, 255, 0.16);
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
.${CSS_CLASSES.container}[data-didit-kind="pending"][data-didit-theme="dark"] .${CSS_CLASSES.loading} path {
|
|
2029
|
+
stroke: #f5f5f7;
|
|
2030
|
+
}
|
|
2031
|
+
|
|
2032
|
+
@media (min-width: 1000px) {
|
|
2033
|
+
.${CSS_CLASSES.container}[data-didit-kind="handoff"],
|
|
2034
|
+
.${CSS_CLASSES.container}[data-didit-kind="pending"] {
|
|
2035
|
+
height: min(660px, calc(100vh * 0.9));
|
|
2036
|
+
height: min(660px, calc(100dvh * 0.9));
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
|
|
1705
2040
|
@media (max-width: 540px) {
|
|
1706
2041
|
.${CSS_CLASSES.overlay} {
|
|
1707
2042
|
padding: 0;
|
|
@@ -1709,23 +2044,50 @@
|
|
|
1709
2044
|
|
|
1710
2045
|
.${CSS_CLASSES.container} {
|
|
1711
2046
|
max-width: 100%;
|
|
1712
|
-
|
|
1713
|
-
|
|
2047
|
+
height: 100vh;
|
|
2048
|
+
height: 100dvh;
|
|
1714
2049
|
border-radius: 0;
|
|
1715
2050
|
}
|
|
2051
|
+
}
|
|
1716
2052
|
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
2053
|
+
/*
|
|
2054
|
+
* The flow's ground right below the layout viewport, in the page itself (see showCurtain):
|
|
2055
|
+
* on a phone, iOS browsers with their toolbars collapsed show a band of the page there,
|
|
2056
|
+
* below every fixed element, the full-screen modal included.
|
|
2057
|
+
*/
|
|
2058
|
+
.${CSS_CLASSES.curtain} {
|
|
2059
|
+
display: none;
|
|
2060
|
+
position: absolute;
|
|
2061
|
+
top: 0;
|
|
2062
|
+
left: 0;
|
|
2063
|
+
width: 100vw;
|
|
2064
|
+
height: 100vh;
|
|
2065
|
+
margin: 0;
|
|
2066
|
+
pointer-events: none;
|
|
2067
|
+
/* Under the modal where they meet for a frame while the toolbars move, above the page. */
|
|
2068
|
+
z-index: ${this.config.zIndex - 1};
|
|
2069
|
+
background: ${FLOW_GROUND.light};
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
.${CSS_CLASSES.curtain}[data-didit-theme="dark"] {
|
|
2073
|
+
background: ${FLOW_GROUND.dark};
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2076
|
+
/* The exit confirmation's dimming continues over the ground below the viewport. */
|
|
2077
|
+
.${CSS_CLASSES.curtain}[data-didit-dimmed] {
|
|
2078
|
+
background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5));
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2081
|
+
@media (max-width: 540px) {
|
|
2082
|
+
.${CSS_CLASSES.curtain} {
|
|
2083
|
+
display: block;
|
|
1721
2084
|
}
|
|
1722
2085
|
}
|
|
1723
2086
|
|
|
1724
2087
|
/*
|
|
1725
|
-
*
|
|
1726
|
-
*
|
|
1727
|
-
*
|
|
1728
|
-
* window. Colours are the design system tokens named beside them.
|
|
2088
|
+
* The hosted flow's layout message (didit:layout) brings the modal's size
|
|
2089
|
+
* (see above), theme and direction. Colours are the design system tokens
|
|
2090
|
+
* named beside them.
|
|
1729
2091
|
*/
|
|
1730
2092
|
.${CSS_CLASSES.container}.${CSS_CLASSES.flowSized} {
|
|
1731
2093
|
--didit-ground: #ffffff; /* site-ground */
|
|
@@ -1735,7 +2097,6 @@
|
|
|
1735
2097
|
--didit-close-fill: rgba(0, 0, 0, 0.06);
|
|
1736
2098
|
--didit-drop: rgba(17, 24, 39, 0.35);
|
|
1737
2099
|
--didit-ring: rgba(0, 0, 0, 0.06);
|
|
1738
|
-
max-width: 500px;
|
|
1739
2100
|
background: var(--didit-ground);
|
|
1740
2101
|
box-shadow: 0 30px 80px -24px var(--didit-drop), 0 0 0 1px var(--didit-ring);
|
|
1741
2102
|
}
|
|
@@ -1750,12 +2111,6 @@
|
|
|
1750
2111
|
--didit-ring: rgba(255, 255, 255, 0.08); /* c-shadow-pop, dark */
|
|
1751
2112
|
}
|
|
1752
2113
|
|
|
1753
|
-
.${CSS_CLASSES.flowSized} .${CSS_CLASSES.iframe} {
|
|
1754
|
-
height: var(--didit-flow-height);
|
|
1755
|
-
max-height: 90vh;
|
|
1756
|
-
max-height: 90dvh;
|
|
1757
|
-
}
|
|
1758
|
-
|
|
1759
2114
|
.${CSS_CLASSES.closeIcon} {
|
|
1760
2115
|
display: none;
|
|
1761
2116
|
}
|
|
@@ -1829,28 +2184,11 @@
|
|
|
1829
2184
|
flex-wrap: wrap;
|
|
1830
2185
|
}
|
|
1831
2186
|
|
|
1832
|
-
@media (min-width: 1024px) {
|
|
1833
|
-
.${CSS_CLASSES.container}.${CSS_CLASSES.flowSized} {
|
|
1834
|
-
width: 90vw;
|
|
1835
|
-
max-width: 960px;
|
|
1836
|
-
}
|
|
1837
|
-
|
|
1838
|
-
.${CSS_CLASSES.flowSized} .${CSS_CLASSES.iframe} {
|
|
1839
|
-
height: 660px;
|
|
1840
|
-
}
|
|
1841
|
-
}
|
|
1842
|
-
|
|
1843
2187
|
@media (max-width: 540px) {
|
|
1844
2188
|
.${CSS_CLASSES.container}.${CSS_CLASSES.flowSized} {
|
|
1845
2189
|
max-width: 100%;
|
|
1846
2190
|
box-shadow: none;
|
|
1847
2191
|
}
|
|
1848
|
-
|
|
1849
|
-
.${CSS_CLASSES.flowSized} .${CSS_CLASSES.iframe} {
|
|
1850
|
-
height: 100vh;
|
|
1851
|
-
height: 100dvh;
|
|
1852
|
-
max-height: none;
|
|
1853
|
-
}
|
|
1854
2192
|
}
|
|
1855
2193
|
|
|
1856
2194
|
.${CSS_CLASSES.embedded} {
|
|
@@ -2083,12 +2421,13 @@
|
|
|
2083
2421
|
*/
|
|
2084
2422
|
readFlowLayout(data) {
|
|
2085
2423
|
const fields = typeof data === "object" && data !== null ? data : {};
|
|
2086
|
-
const { contentHeight, colorScheme, dir } = fields;
|
|
2424
|
+
const { contentHeight, colorScheme, dir, kind } = fields;
|
|
2087
2425
|
if (typeof contentHeight !== "number" || !Number.isFinite(contentHeight) || contentHeight <= 0) {
|
|
2088
2426
|
return null;
|
|
2089
2427
|
}
|
|
2090
2428
|
const prefersDark = window.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false;
|
|
2091
2429
|
return {
|
|
2430
|
+
kind: kind === "handoff" ? "handoff" : "step",
|
|
2092
2431
|
contentHeight: Math.ceil(contentHeight),
|
|
2093
2432
|
colorScheme: colorScheme === "light" || colorScheme === "dark" ? colorScheme : prefersDark ? "dark" : "light",
|
|
2094
2433
|
dir: dir === "ltr" || dir === "rtl" ? dir : isRtlLanguage(this.language) ? "rtl" : "ltr"
|
|
@@ -2100,18 +2439,222 @@
|
|
|
2100
2439
|
return;
|
|
2101
2440
|
SDKLogger.log("Flow layout:", layout);
|
|
2102
2441
|
this.container.classList.add(CSS_CLASSES.flowSized);
|
|
2103
|
-
this.container.style.setProperty("--didit-flow-height", `${layout.contentHeight}px`);
|
|
2104
2442
|
this.container.dataset.diditTheme = layout.colorScheme;
|
|
2105
2443
|
this.container.dataset.diditDir = layout.dir;
|
|
2444
|
+
// The height the person sees now, before the reported content height can change it.
|
|
2445
|
+
const shownHeight = this.container.offsetHeight;
|
|
2446
|
+
this.container.style.setProperty("--didit-content-height", `${layout.contentHeight}px`);
|
|
2447
|
+
this.matchPageBackground(layout.colorScheme);
|
|
2448
|
+
if (this.curtain)
|
|
2449
|
+
this.curtain.dataset.diditTheme = layout.colorScheme;
|
|
2450
|
+
this.setModalKind(layout.kind, layout.kind === "handoff" && this.handoffHeight(layout) === shownHeight);
|
|
2451
|
+
}
|
|
2452
|
+
/** Whether the window is a computer-sized one: a phone is full screen and never waits for a layout. */
|
|
2453
|
+
isPhoneWindow() {
|
|
2454
|
+
return window.matchMedia?.("(max-width: 540px)").matches ?? false;
|
|
2455
|
+
}
|
|
2456
|
+
/** The height the handoff card takes for a layout, as the size rules above compute it (rounded like offsetHeight). */
|
|
2457
|
+
handoffHeight(layout) {
|
|
2458
|
+
const cap = window.innerHeight * 0.9;
|
|
2459
|
+
return Math.round(Math.min(window.innerWidth >= 1000 ? 660 : layout.contentHeight, cap));
|
|
2460
|
+
}
|
|
2461
|
+
/**
|
|
2462
|
+
* Sets what the modal is sized for. Leaving "pending" shows the modal at its final size with no
|
|
2463
|
+
* transition; any later change animates once.
|
|
2464
|
+
*/
|
|
2465
|
+
setModalKind(kind, sizeUnchanged = false) {
|
|
2466
|
+
if (!this.container)
|
|
2467
|
+
return;
|
|
2468
|
+
const previous = this.container.dataset.diditKind;
|
|
2469
|
+
if (previous === kind)
|
|
2470
|
+
return;
|
|
2471
|
+
if (this.firstLayoutTimeout !== null && kind !== "pending") {
|
|
2472
|
+
window.clearTimeout(this.firstLayoutTimeout);
|
|
2473
|
+
this.firstLayoutTimeout = null;
|
|
2474
|
+
}
|
|
2475
|
+
// Every change of kind animates, the pending card included (it is on screen), and only for the
|
|
2476
|
+
// length of the change, so resizing the window never lags behind.
|
|
2477
|
+
// Pending already is the handoff card: a handoff first layout of the same size moves nothing,
|
|
2478
|
+
// while a different reported height (a narrow window) animates the correction once.
|
|
2479
|
+
const staysPut = previous === "pending" && kind === "handoff" && sizeUnchanged;
|
|
2480
|
+
if (previous !== undefined && !staysPut)
|
|
2481
|
+
this.animateSizeOnce();
|
|
2482
|
+
this.container.dataset.diditKind = kind;
|
|
2483
|
+
// The final size is in place before the loader goes; a flow still loading keeps its loader.
|
|
2484
|
+
if (previous === "pending" && !this.state.isLoading)
|
|
2485
|
+
this.loadingEl?.classList.add("hidden");
|
|
2106
2486
|
}
|
|
2107
|
-
|
|
2487
|
+
animateSizeOnce() {
|
|
2488
|
+
this.container?.classList.add(CSS_CLASSES.sizeAnimated);
|
|
2489
|
+
if (this.sizeTimeout !== null)
|
|
2490
|
+
window.clearTimeout(this.sizeTimeout);
|
|
2491
|
+
this.sizeTimeout = window.setTimeout(() => {
|
|
2492
|
+
this.sizeTimeout = null;
|
|
2493
|
+
this.container?.classList.remove(CSS_CLASSES.sizeAnimated);
|
|
2494
|
+
}, SIZE_ANIMATION_MS);
|
|
2495
|
+
}
|
|
2496
|
+
/** A flow that reports no layout (an older one) still shows, at step size, after a short wait. */
|
|
2497
|
+
waitForFirstLayout() {
|
|
2498
|
+
if (!this.container)
|
|
2499
|
+
return;
|
|
2500
|
+
// The card looks like the first screen from the first frame, in the device's theme.
|
|
2501
|
+
this.container.classList.add(CSS_CLASSES.flowSized);
|
|
2502
|
+
this.container.dataset.diditTheme = window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
2503
|
+
this.setModalKind("pending");
|
|
2504
|
+
this.firstLayoutTimeout = window.setTimeout(() => {
|
|
2505
|
+
this.firstLayoutTimeout = null;
|
|
2506
|
+
// A flow that never reported is an older one that paints itself: no card look around it.
|
|
2507
|
+
this.container?.classList.remove(CSS_CLASSES.flowSized);
|
|
2508
|
+
delete this.container?.dataset.diditTheme;
|
|
2509
|
+
this.setModalKind("step");
|
|
2510
|
+
}, FIRST_LAYOUT_TIMEOUT_MS);
|
|
2511
|
+
}
|
|
2512
|
+
/**
|
|
2513
|
+
* On a phone Safari paints the bottom edge of the screen with the page's own background, past
|
|
2514
|
+
* any fixed overlay, and shows it when the keyboard moves the page. A full-screen modal lends
|
|
2515
|
+
* the page its ground colour while it is open, so no strip of the host page shows around it.
|
|
2516
|
+
*/
|
|
2517
|
+
matchPageBackground(colorScheme) {
|
|
2518
|
+
if (!window.matchMedia?.("(max-width: 540px)").matches)
|
|
2519
|
+
return;
|
|
2520
|
+
VerificationModal.pageBackground ?? (VerificationModal.pageBackground = {
|
|
2521
|
+
saved: { root: document.documentElement.style.backgroundColor, body: document.body.style.backgroundColor },
|
|
2522
|
+
grounds: new Map()
|
|
2523
|
+
});
|
|
2524
|
+
VerificationModal.pageBackground.grounds.set(this, colorScheme);
|
|
2525
|
+
VerificationModal.paintPageBackground();
|
|
2526
|
+
}
|
|
2527
|
+
restorePageBackground() {
|
|
2528
|
+
const background = VerificationModal.pageBackground;
|
|
2529
|
+
if (!background?.grounds.delete(this))
|
|
2530
|
+
return;
|
|
2531
|
+
if (background.grounds.size > 0) {
|
|
2532
|
+
VerificationModal.paintPageBackground();
|
|
2533
|
+
return;
|
|
2534
|
+
}
|
|
2535
|
+
VerificationModal.pageBackground = null;
|
|
2536
|
+
document.documentElement.style.backgroundColor = background.saved.root;
|
|
2537
|
+
document.body.style.backgroundColor = background.saved.body;
|
|
2538
|
+
}
|
|
2539
|
+
/** The page shows the ground of the topmost open modal that lends one. */
|
|
2540
|
+
static paintPageBackground() {
|
|
2541
|
+
const background = VerificationModal.pageBackground;
|
|
2542
|
+
if (!background)
|
|
2543
|
+
return;
|
|
2544
|
+
const lender = [...VerificationModal.openModals].reverse().find((modal) => background.grounds.has(modal));
|
|
2545
|
+
const colorScheme = lender ? background.grounds.get(lender) : [...background.grounds.values()].pop();
|
|
2546
|
+
if (!colorScheme)
|
|
2547
|
+
return;
|
|
2548
|
+
document.documentElement.style.backgroundColor = FLOW_GROUND[colorScheme];
|
|
2549
|
+
document.body.style.backgroundColor = FLOW_GROUND[colorScheme];
|
|
2550
|
+
}
|
|
2551
|
+
/**
|
|
2552
|
+
* Stops the page scrolling under the modal. The lock is on the root element: Chrome for iOS keeps
|
|
2553
|
+
* scrolling a page whose body alone is `overflow: hidden` (a swipe over the modal scrolls the page
|
|
2554
|
+
* and toggles the browser's toolbars), and stops only when the root element is. A page that
|
|
2555
|
+
* scrolls its body instead of the window has its body locked too. The first modal to open keeps
|
|
2556
|
+
* the page's own values and the last one to close gives them back.
|
|
2557
|
+
*/
|
|
2558
|
+
lockPageScroll() {
|
|
2559
|
+
const lock = VerificationModal.pageScrollLock;
|
|
2560
|
+
if (lock) {
|
|
2561
|
+
lock.holders.add(this);
|
|
2562
|
+
return;
|
|
2563
|
+
}
|
|
2564
|
+
const root = document.documentElement;
|
|
2565
|
+
const body = document.body;
|
|
2566
|
+
// The body's overflow is the viewport's only while the root's is visible; otherwise a body with
|
|
2567
|
+
// an overflow of its own is a scroll container. Locking a body that is not one would clip the page
|
|
2568
|
+
// to the body's box.
|
|
2569
|
+
const clips = (overflow) => overflow !== "" && overflow !== "visible";
|
|
2570
|
+
const bodyScrollsItself = clips(window.getComputedStyle(root).overflowY) && clips(window.getComputedStyle(body).overflowY);
|
|
2571
|
+
VerificationModal.pageScrollLock = {
|
|
2572
|
+
holders: new Set([this]),
|
|
2573
|
+
root: { overflow: root.style.overflow, overscrollBehavior: root.style.overscrollBehavior },
|
|
2574
|
+
body: bodyScrollsItself ? { overflow: body.style.overflow } : null
|
|
2575
|
+
};
|
|
2576
|
+
root.style.overflow = "hidden";
|
|
2577
|
+
root.style.overscrollBehavior = "none";
|
|
2578
|
+
if (bodyScrollsItself)
|
|
2579
|
+
body.style.overflow = "hidden";
|
|
2580
|
+
}
|
|
2581
|
+
unlockPageScroll() {
|
|
2582
|
+
const lock = VerificationModal.pageScrollLock;
|
|
2583
|
+
if (!lock || !lock.holders.delete(this) || lock.holders.size > 0)
|
|
2584
|
+
return;
|
|
2585
|
+
VerificationModal.pageScrollLock = null;
|
|
2586
|
+
const root = document.documentElement;
|
|
2587
|
+
root.style.overflow = lock.root.overflow;
|
|
2588
|
+
root.style.overscrollBehavior = lock.root.overscrollBehavior;
|
|
2589
|
+
if (lock.body)
|
|
2590
|
+
document.body.style.overflow = lock.body.overflow;
|
|
2591
|
+
}
|
|
2592
|
+
/**
|
|
2593
|
+
* On a phone, Chrome for iOS (and the other iOS browsers built on WebKit's web view) shows a band
|
|
2594
|
+
* of the page below the layout viewport while its toolbars are collapsed: the home indicator's
|
|
2595
|
+
* area, and more while the toolbars move. Fixed elements end at the layout viewport, so the
|
|
2596
|
+
* full-screen modal cannot cover it and the host page's own content showed under the flow. The
|
|
2597
|
+
* curtain is part of the page instead: right below the viewport, in the flow's ground, so that band
|
|
2598
|
+
* shows the modal's ground. The page cannot scroll while the modal is open (lockPageScroll), and the
|
|
2599
|
+
* curtain follows the viewport when it resizes. Only phone-sized windows draw it (see the styles).
|
|
2600
|
+
*/
|
|
2601
|
+
showCurtain() {
|
|
2602
|
+
if (!this.curtain) {
|
|
2603
|
+
const curtain = document.createElement("div");
|
|
2604
|
+
curtain.className = CSS_CLASSES.curtain;
|
|
2605
|
+
curtain.setAttribute("aria-hidden", "true");
|
|
2606
|
+
document.body.appendChild(curtain);
|
|
2607
|
+
this.curtain = curtain;
|
|
2608
|
+
this.boundPlaceCurtain = () => this.placeCurtain();
|
|
2609
|
+
window.addEventListener("resize", this.boundPlaceCurtain);
|
|
2610
|
+
window.addEventListener("scroll", this.boundPlaceCurtain);
|
|
2611
|
+
window.visualViewport?.addEventListener("resize", this.boundPlaceCurtain);
|
|
2612
|
+
}
|
|
2613
|
+
// The device's theme until the newly loaded flow reports its own (applyFlowLayout).
|
|
2614
|
+
this.curtain.dataset.diditTheme = window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
2615
|
+
this.placeCurtain();
|
|
2616
|
+
}
|
|
2617
|
+
/** Puts the curtain's top at the bottom of the layout viewport, whatever its containing block is. */
|
|
2618
|
+
placeCurtain() {
|
|
2619
|
+
const curtain = this.curtain;
|
|
2620
|
+
if (!curtain)
|
|
2621
|
+
return;
|
|
2622
|
+
curtain.style.top = "0px";
|
|
2623
|
+
curtain.style.left = "0px";
|
|
2624
|
+
// At 0, 0 the curtain's box starts at its containing block's corner (the page's, or a positioned
|
|
2625
|
+
// body's), in viewport coordinates: the offset that brings its top to the viewport's bottom edge.
|
|
2626
|
+
const origin = curtain.getBoundingClientRect();
|
|
2627
|
+
curtain.style.top = `${window.innerHeight - origin.top}px`;
|
|
2628
|
+
curtain.style.left = `${-origin.left}px`;
|
|
2629
|
+
}
|
|
2630
|
+
removeCurtain() {
|
|
2631
|
+
if (this.boundPlaceCurtain) {
|
|
2632
|
+
window.removeEventListener("resize", this.boundPlaceCurtain);
|
|
2633
|
+
window.removeEventListener("scroll", this.boundPlaceCurtain);
|
|
2634
|
+
window.visualViewport?.removeEventListener("resize", this.boundPlaceCurtain);
|
|
2635
|
+
this.boundPlaceCurtain = null;
|
|
2636
|
+
}
|
|
2637
|
+
this.curtain?.remove();
|
|
2638
|
+
this.curtain = null;
|
|
2639
|
+
}
|
|
2640
|
+
/** Back to the neutral look until the newly loaded flow reports its theme and direction. */
|
|
2108
2641
|
resetFlowLayout() {
|
|
2109
2642
|
if (this.embedded || !this.container)
|
|
2110
2643
|
return;
|
|
2111
|
-
this.
|
|
2112
|
-
this.container.
|
|
2644
|
+
this.restorePageBackground();
|
|
2645
|
+
this.container.classList.remove(CSS_CLASSES.flowSized, CSS_CLASSES.sizeAnimated);
|
|
2113
2646
|
delete this.container.dataset.diditTheme;
|
|
2114
2647
|
delete this.container.dataset.diditDir;
|
|
2648
|
+
delete this.container.dataset.diditKind;
|
|
2649
|
+
this.container.style.removeProperty("--didit-content-height");
|
|
2650
|
+
if (this.firstLayoutTimeout !== null) {
|
|
2651
|
+
window.clearTimeout(this.firstLayoutTimeout);
|
|
2652
|
+
this.firstLayoutTimeout = null;
|
|
2653
|
+
}
|
|
2654
|
+
if (this.sizeTimeout !== null) {
|
|
2655
|
+
window.clearTimeout(this.sizeTimeout);
|
|
2656
|
+
this.sizeTimeout = null;
|
|
2657
|
+
}
|
|
2115
2658
|
}
|
|
2116
2659
|
handleKeydown(event) {
|
|
2117
2660
|
if (!this.state.isOpen)
|
|
@@ -2147,7 +2690,9 @@
|
|
|
2147
2690
|
handleIframeLoad() {
|
|
2148
2691
|
if (this.iframe?.src && this.iframe.src !== "about:blank") {
|
|
2149
2692
|
this.state.isLoading = false;
|
|
2150
|
-
|
|
2693
|
+
// A modal still waiting for its first layout keeps the loader over the hidden flow.
|
|
2694
|
+
if (this.container?.dataset.diditKind !== "pending")
|
|
2695
|
+
this.loadingEl?.classList.add("hidden");
|
|
2151
2696
|
this.callbacks.onIframeLoad();
|
|
2152
2697
|
}
|
|
2153
2698
|
}
|
|
@@ -2168,6 +2713,7 @@
|
|
|
2168
2713
|
: this.container?.querySelector(`.${CSS_CLASSES.closeButton}`) ?? this.iframe);
|
|
2169
2714
|
this.state.showConfirmation = true;
|
|
2170
2715
|
this.confirmOverlay.classList.add("active");
|
|
2716
|
+
this.curtain?.setAttribute("data-didit-dimmed", "");
|
|
2171
2717
|
if (this.iframe)
|
|
2172
2718
|
this.iframe.inert = true;
|
|
2173
2719
|
const close = this.container?.querySelector(`.${CSS_CLASSES.closeButton}`);
|
|
@@ -2191,6 +2737,7 @@
|
|
|
2191
2737
|
return;
|
|
2192
2738
|
this.state.showConfirmation = false;
|
|
2193
2739
|
this.confirmOverlay?.classList.remove("active");
|
|
2740
|
+
this.curtain?.removeAttribute("data-didit-dimmed");
|
|
2194
2741
|
if (this.iframe)
|
|
2195
2742
|
this.iframe.inert = false;
|
|
2196
2743
|
const close = this.container?.querySelector(`.${CSS_CLASSES.closeButton}`);
|
|
@@ -2218,11 +2765,6 @@
|
|
|
2218
2765
|
this.callbacks.onCloseConfirmed();
|
|
2219
2766
|
}
|
|
2220
2767
|
open(verificationUrl) {
|
|
2221
|
-
// Whether this modal already held its scroll lock before this call. Re-opening
|
|
2222
|
-
// an already-open instance must not re-capture previousBodyOverflow, or it
|
|
2223
|
-
// would record this modal's own "hidden" lock and close() could never restore
|
|
2224
|
-
// the real pre-lock value.
|
|
2225
|
-
const wasAlreadyOpen = this.state.isOpen;
|
|
2226
2768
|
this.language = detectLanguageFromUrl(verificationUrl);
|
|
2227
2769
|
if (!this.overlay && !this.container) {
|
|
2228
2770
|
this.createDOM();
|
|
@@ -2236,6 +2778,8 @@
|
|
|
2236
2778
|
this.hideConfirmation();
|
|
2237
2779
|
this.loadingEl?.classList.remove("hidden");
|
|
2238
2780
|
this.resetFlowLayout();
|
|
2781
|
+
if (!this.embedded && !this.isPhoneWindow())
|
|
2782
|
+
this.waitForFirstLayout();
|
|
2239
2783
|
if (this.iframe) {
|
|
2240
2784
|
this.iframe.src = verificationUrl;
|
|
2241
2785
|
}
|
|
@@ -2245,16 +2789,10 @@
|
|
|
2245
2789
|
return;
|
|
2246
2790
|
}
|
|
2247
2791
|
this.overlay?.classList.add("active");
|
|
2248
|
-
//
|
|
2249
|
-
//
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
// in effect is this modal's own lock, and previousBodyOverflow already
|
|
2253
|
-
// holds the correct pre-lock value.
|
|
2254
|
-
if (!wasAlreadyOpen) {
|
|
2255
|
-
this.previousBodyOverflow = document.body.style.overflow;
|
|
2256
|
-
}
|
|
2257
|
-
document.body.style.overflow = "hidden";
|
|
2792
|
+
// Idempotent: a re-open of a modal that is still open keeps its one hold on the lock, so
|
|
2793
|
+
// close() still gives the page back the values it had before any modal opened.
|
|
2794
|
+
this.lockPageScroll();
|
|
2795
|
+
this.showCurtain();
|
|
2258
2796
|
}
|
|
2259
2797
|
close() {
|
|
2260
2798
|
SDKLogger.log("Closing");
|
|
@@ -2272,8 +2810,8 @@
|
|
|
2272
2810
|
return;
|
|
2273
2811
|
}
|
|
2274
2812
|
this.overlay?.classList.remove("active");
|
|
2275
|
-
|
|
2276
|
-
this.
|
|
2813
|
+
this.removeCurtain();
|
|
2814
|
+
this.unlockPageScroll();
|
|
2277
2815
|
}
|
|
2278
2816
|
destroy() {
|
|
2279
2817
|
SDKLogger.log("Destroying");
|
|
@@ -2305,6 +2843,16 @@
|
|
|
2305
2843
|
* a transaction action modal stacked over the verification modal).
|
|
2306
2844
|
*/
|
|
2307
2845
|
VerificationModal.openModals = [];
|
|
2846
|
+
/**
|
|
2847
|
+
* The page's own background colours, kept while full-screen modals lend it their ground, shared by
|
|
2848
|
+
* every open modal: the topmost one's ground shows and the last one to give it back restores them.
|
|
2849
|
+
*/
|
|
2850
|
+
VerificationModal.pageBackground = null;
|
|
2851
|
+
/**
|
|
2852
|
+
* The page's scroll styles from before the first open modal locked them, shared by every open
|
|
2853
|
+
* modal: the last one to close gives them back.
|
|
2854
|
+
*/
|
|
2855
|
+
VerificationModal.pageScrollLock = null;
|
|
2308
2856
|
|
|
2309
2857
|
const DEFAULT_TRANSACTION_BASE_URL = "https://verification.didit.me";
|
|
2310
2858
|
const ACTION_POLL_INTERVAL_MS = 1500;
|
|
@@ -2624,6 +3172,18 @@
|
|
|
2624
3172
|
throw new Error(`Embedded container "#${containerId}" was not found in the page.`);
|
|
2625
3173
|
}
|
|
2626
3174
|
}
|
|
3175
|
+
/**
|
|
3176
|
+
* The device id this page already keeps for Didit (it has sent events, transactions or sign-ins),
|
|
3177
|
+
* or null. Reading it never creates one, and a browser that refuses the read is not an error.
|
|
3178
|
+
*/
|
|
3179
|
+
function storedDeviceId() {
|
|
3180
|
+
try {
|
|
3181
|
+
return getStoredDeviceId();
|
|
3182
|
+
}
|
|
3183
|
+
catch {
|
|
3184
|
+
return null;
|
|
3185
|
+
}
|
|
3186
|
+
}
|
|
2627
3187
|
class DiditSdk {
|
|
2628
3188
|
static get shared() {
|
|
2629
3189
|
if (!DiditSdk._instance) {
|
|
@@ -2697,7 +3257,7 @@
|
|
|
2697
3257
|
onMessage: (event) => this.handleVerificationEvent(event),
|
|
2698
3258
|
onIframeLoad: () => this.handleIframeLoad()
|
|
2699
3259
|
});
|
|
2700
|
-
this._url = buildVerificationUrl(url, config);
|
|
3260
|
+
this._url = buildVerificationUrl(url, config, storedDeviceId());
|
|
2701
3261
|
this.setState("loading");
|
|
2702
3262
|
this.emitInternalEvent("didit:started", {});
|
|
2703
3263
|
this._modal?.open(this._url);
|
|
@@ -3094,6 +3654,8 @@
|
|
|
3094
3654
|
exports.default = DiditSdk;
|
|
3095
3655
|
exports.flush = flush;
|
|
3096
3656
|
exports.getDeviceFingerprintPayload = getDeviceFingerprintPayload;
|
|
3657
|
+
exports.getStoredDeviceId = getStoredDeviceId;
|
|
3658
|
+
exports.setDeviceSignalsProvider = setDeviceSignalsProvider;
|
|
3097
3659
|
exports.submitEvent = submitEvent;
|
|
3098
3660
|
|
|
3099
3661
|
Object.defineProperty(exports, '__esModule', { value: true });
|