@touchque/device-risk 0.0.0-stage → 3.1.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.
@@ -0,0 +1,395 @@
1
+ /** The public half of the browser key: exactly these four members (no `d`, `ext` or `key_ops`). */
2
+ interface BrowserKeyJwk {
3
+ kty: 'EC';
4
+ crv: 'P-256';
5
+ x: string;
6
+ y: string;
7
+ }
8
+ /** A device-risk report's seal (see `BrowserKey.sealDevice`). */
9
+ interface DeviceSeal {
10
+ jwk: BrowserKeyJwk;
11
+ aud: string;
12
+ /** Unix seconds. */
13
+ iat: number;
14
+ /** base64url of 16 random bytes. */
15
+ nonce: string;
16
+ /** base64url of the 64-byte r||s ECDSA signature over `deviceSealMessage(aud, iat, nonce, digest)`. */
17
+ sig: string;
18
+ }
19
+ interface BrowserKey {
20
+ /**
21
+ * A fresh proof of this browser's key, base64url JSON for the
22
+ * `X-Touchque-Browser` header. Makes the key on first use. Resolves `null`
23
+ * (never throws) when WebCrypto or IndexedDB is unavailable or too slow.
24
+ * Carries unsigned `hints` (see `BrowserHints`) when the browser has them.
25
+ */
26
+ proof(): Promise<string | null>;
27
+ /**
28
+ * RFC 7638 thumbprint of this browser's key, as the server SDKs'
29
+ * `risk.browsers` lists it (to mark "this browser"), or `null` when this
30
+ * browser has no key yet. Never makes a key and never throws.
31
+ */
32
+ thumbprint(): Promise<string | null>;
33
+ /**
34
+ * Seals a device-risk report (`deviceRisk.collect`): signs
35
+ * `deviceSealMessage(aud, iat, nonce, digest)` with this browser's key, so the
36
+ * server can tell the report came whole from a browser holding the key, and
37
+ * accept it once. A different message from the proof's, so a seal can never
38
+ * stand in for a proof. Resolves `null` (never throws) without WebCrypto or
39
+ * IndexedDB. Optional: a custom `BrowserKey` may leave it out.
40
+ */
41
+ sealDevice?(digest: string): Promise<DeviceSeal | null>;
42
+ /**
43
+ * Deletes this browser's key: the next proof uses a new key, so Touchque
44
+ * treats it as a new browser. Use it on shared computers or when the user
45
+ * signs out everywhere (and forget it on the server too). Never throws.
46
+ */
47
+ forget(): Promise<void>;
48
+ }
49
+
50
+ /** The page the report is collected on. Device risk is for these pages only. */
51
+ type DeviceRiskPurpose = 'sign_in' | 'sign_up' | 'password_reset' | 'approval';
52
+ declare const DEVICE_RISK_PURPOSES: readonly DeviceRiskPurpose[];
53
+ /** The form field a report travels in from the browser to your server. */
54
+ declare const DEVICE_RISK_FIELD = "deviceRisk";
55
+ /**
56
+ * The header guarded requests carry the page's Device risk identification in
57
+ * (`@touchque/web`'s `touchqueFetch` sends the tag's `TouchqueDevice.value()`).
58
+ * The server SDK one-liners read it after the `touchque_request_id` field.
59
+ */
60
+ declare const DEVICE_RISK_HEADER = "X-Touchque-Device";
61
+ /** Coarse traits. Raw here, never stored raw: the server keeps keyed hashes and coarse labels. */
62
+ interface DeviceRiskTraits {
63
+ /** OS family: Windows, macOS, Linux, Android, iOS, ChromeOS or Other. */
64
+ os: string;
65
+ /** Browser family: Chrome, Edge, Firefox, Safari, Opera, Samsung or Other. */
66
+ br: string;
67
+ /** Browser major version (0 when unknown). Left out of matching: an update is the same device. */
68
+ bv: number;
69
+ /** navigator.platform, at most 32 characters. */
70
+ pf: string;
71
+ /** IANA time zone, e.g. Europe/Istanbul. */
72
+ tz: string;
73
+ /** Minutes west of UTC now (Date.getTimezoneOffset). */
74
+ tzo: number;
75
+ /** navigator.languages, at most 3, each at most 16 characters. */
76
+ lang: string[];
77
+ /** Screen size as long x short side (rotation does not change it). */
78
+ scr: string;
79
+ cd: number;
80
+ /** devicePixelRatio, 2 decimals. */
81
+ dpr: number;
82
+ /** hardwareConcurrency. */
83
+ hc: number;
84
+ /** deviceMemory (Chromium), else null. */
85
+ dm: number | null;
86
+ /** maxTouchPoints. */
87
+ tp: number;
88
+ /** WebGL unmasked vendor|renderer, at most 128 characters, or null. */
89
+ gpu: string | null;
90
+ /** 16-hex digests (null when not available): canvas text drawing, the audio test, the probe fonts found, Math results, WebGL limits. */
91
+ cv: string | null;
92
+ au: string | null;
93
+ fn: string | null;
94
+ mt: string | null;
95
+ wg: string | null;
96
+ }
97
+ /** What the page found about itself. Names only: stable codes the server and the dashboard explain. */
98
+ interface DeviceRiskFindings {
99
+ /** Browser functions replaced by script, and traits that contradict each other. */
100
+ tamper: string[];
101
+ /** A virtual machine, software graphics, a headless or automated browser. */
102
+ vm: string[];
103
+ /** Which automation tool left its marks on the page (selenium, playwright, phantomjs, nightmare, headless_chrome), and cdp: the DevTools protocol is attached (a driver, or developer tools open). Since 2.3.0. */
104
+ auto: string[];
105
+ /** How a private window showed itself (chromium_quota, firefox_no_sw, safari_opfs), or null. */
106
+ priv: string | null;
107
+ /** Brave (randomises some traits on purpose: not suspicious, but the server trusts those traits less). */
108
+ brave: boolean;
109
+ /** Two drawings of the same canvas differed: noise added by an extension or an anti-detect browser. */
110
+ cvNoise: boolean;
111
+ /** Which storage the page could use. */
112
+ storage: {
113
+ ls: boolean;
114
+ idb: boolean;
115
+ cookie: boolean;
116
+ };
117
+ }
118
+ /** The signed part of a report. */
119
+ interface DeviceRiskBody {
120
+ v: 1;
121
+ p: DeviceRiskPurpose;
122
+ /** The page's origin. */
123
+ aud: string;
124
+ /** Unix seconds. */
125
+ iat: number;
126
+ /** base64url of 16 random bytes; the server accepts each once. */
127
+ nonce: string;
128
+ /** This browser's stored random id (`tq2_…`, shared with collectDeviceSignals), or null. */
129
+ sid: string | null;
130
+ /** Touchque's fresh value the page asked for just before (3.1.0), when it got one. */
131
+ ch?: string;
132
+ /** The tag's version (3.1.0). */
133
+ tv?: string;
134
+ t: DeviceRiskTraits;
135
+ s: DeviceRiskFindings;
136
+ /** Milliseconds the collection took. */
137
+ ms: number;
138
+ }
139
+ /** What `collect` encodes: `body` is the exact JSON text the seal covers. */
140
+ interface DeviceRiskReport {
141
+ v: 1;
142
+ body: string;
143
+ seal?: DeviceSeal;
144
+ }
145
+ interface DeviceRiskCollectOptions {
146
+ purpose: DeviceRiskPurpose;
147
+ /**
148
+ * The key that seals the report. Default: the shared `browserKey`. `null`
149
+ * sends it unsealed (the server trusts it less).
150
+ */
151
+ browserKey?: BrowserKey | null;
152
+ /** Give up on the slow parts (audio, private-window check) after this long. Default 1500 ms. */
153
+ timeoutMs?: number;
154
+ /** Encrypt the report for Touchque (default). `false` sends the plain report (tests, debugging). */
155
+ encrypt?: boolean;
156
+ /** Touchque's fresh value for this report (`identify` asks for it first; 3.1.0). */
157
+ challenge?: string | null;
158
+ }
159
+ interface DeviceRiskIdentifyOptions extends DeviceRiskCollectOptions {
160
+ /** Your workspace's public site key (`tqpk_…`, dashboard → Device risk → Settings). */
161
+ siteKey: string;
162
+ /** Touchque's API, default `https://api.touchque.com`. */
163
+ apiBaseUrl?: string;
164
+ /** Injectable fetch. Default: the global `fetch`. */
165
+ fetch?: typeof fetch;
166
+ /**
167
+ * Identified when the page loads, not when the form is sent (the script
168
+ * tag does this): Touchque counts it (a sign-up, a country) when your
169
+ * server checks it, not per page view.
170
+ */
171
+ pageView?: boolean;
172
+ }
173
+ /** What the page gets. Never trust it alone: your server reads the identification by `requestId`. */
174
+ interface DeviceRiskIdentification {
175
+ /** This browser in your workspace only (`tqv_…`). */
176
+ visitorId: string;
177
+ /** This identification (`dre_…`): send it to your server. Usable for one account, for 15 minutes. */
178
+ requestId: string;
179
+ /** 0.99 sealed by the device key, 0.95 its stored id, up to 0.9 a match of its traits, 0.5 to 0.6 a first sight. */
180
+ confidence: number;
181
+ }
182
+ interface DeviceRisk {
183
+ /**
184
+ * Identifies this browser with Touchque (your site key, from your own
185
+ * origin): `{ visitorId, requestId, confidence }`, or `null` (never throws)
186
+ * when Touchque could not be reached or refused (the reason is logged to
187
+ * the console). Send `requestId` to your server.
188
+ */
189
+ identify(options: DeviceRiskIdentifyOptions): Promise<DeviceRiskIdentification | null>;
190
+ /**
191
+ * The encoded report (base64url JSON) for your server to pass to Touchque
192
+ * as `deviceRisk` (the relay alternative to identify). Never throws; collects what it can.
193
+ */
194
+ collect(options: DeviceRiskCollectOptions): Promise<string>;
195
+ /** The report before encoding, for tests and debugging. */
196
+ report(options: DeviceRiskCollectOptions): Promise<DeviceRiskReport>;
197
+ }
198
+ /** Which JavaScript engine runs the page, by the wording of one TypeError (each engine words it its own way), or null. */
199
+ declare function jsEngine(): 'v8' | 'jsc' | 'gecko' | null;
200
+ /**
201
+ * Traits that cannot all be true at once: what the browser says it is against
202
+ * what its engine, graphics, fonts, touch and screen show. Anti-detect
203
+ * browsers, emulators, desktop tools that pose as an iPhone or an Android
204
+ * phone, and user-agent changers get these wrong even when they patch the
205
+ * browser natively (native checks then pass). `engine` is the page's
206
+ * (jsEngine); tests pass their own. Codes only, sorted.
207
+ */
208
+ declare function consistencyFindings(win: Window, t: Pick<DeviceRiskTraits, 'os' | 'br' | 'pf' | 'scr' | 'dpr' | 'tp' | 'gpu'>, fonts: string[] | null, engine?: 'v8' | 'jsc' | 'gecko' | null): string[];
209
+ /** Builds the report. Never throws: every part fails soft. */
210
+ declare function buildDeviceRiskReport(options: DeviceRiskCollectOptions, win?: Window): Promise<DeviceRiskReport>;
211
+ declare function createDeviceRisk(win?: Window): DeviceRisk;
212
+ /** Device risk for this page (the shared browser key seals it). */
213
+ declare const deviceRisk: DeviceRisk;
214
+
215
+ interface PointerSummary {
216
+ v: 1;
217
+ /** Presses with the main button of a mouse or a pen. */
218
+ p: number;
219
+ /** Of those, presses the pointer jumped onto: at most one move in the 500 ms before, from 30 px or more away or from nowhere. */
220
+ pt: number;
221
+ /** The longest of those jumped presses, down to up (ms); null without one, or when the browser's clock is coarse. */
222
+ pth: number | null;
223
+ /** Long mouse or pen strokes: 300 px or more, 10 samples or more, not along an axis. */
224
+ ls: number;
225
+ /** The smallest of their largest distances from a straight line (px). */
226
+ lsd: number | null;
227
+ /** Taps: one finger that moved under 10 px. */
228
+ t: number;
229
+ /** The longest tap (ms); null without one, or when the browser's clock is coarse. */
230
+ th: number | null;
231
+ /** Long swipes: one finger, 300 px or more, 10 samples or more. */
232
+ sw: number;
233
+ /** The smallest of their largest distances from a straight line (px). */
234
+ swd: number | null;
235
+ /** Mouse and pen moves seen. */
236
+ mv: number;
237
+ /** Touch moves seen. */
238
+ tm: number;
239
+ }
240
+ interface PointerSummaryProbe {
241
+ summary(): PointerSummary;
242
+ /** 18 characters (encodePointerSummary), sent after the requestId and a dot. */
243
+ encode(): string;
244
+ stop(): void;
245
+ }
246
+ interface PointerSummaryDeps {
247
+ /** Which events came from the person's own hardware (tests). Default: `isTrusted`. */
248
+ trusted?: (e: Event) => boolean;
249
+ }
250
+ /** Starts watching the pointer and the fingers on `root` (default: the document). */
251
+ declare function startPointerSummary(root?: Document | HTMLElement, deps?: PointerSummaryDeps): PointerSummaryProbe;
252
+ /**
253
+ * The summary in 18 characters (base64url digits), to fit after the
254
+ * requestId in a form field that servers cut at 64 characters: version 1,
255
+ * then p, pt, pth, ls, lsd×10, t, th, sw, swd×10, mv, tm. Counts stop at 63
256
+ * (pt below p stays below it), times and moves at 4094; `__` is none.
257
+ */
258
+ declare function encodePointerSummary(s: PointerSummary): string;
259
+
260
+ interface InputSummary {
261
+ /** Version. */
262
+ v: 1;
263
+ /** Trusted key presses of a character. */
264
+ kd: number;
265
+ /** Text insertions with no key press just before them. */
266
+ nk: number;
267
+ /** Pastes. */
268
+ pa: number;
269
+ /** Intervals between key presses within a burst. */
270
+ kn: number;
271
+ /** Their mean (ms; null when none or the clock is coarse). */
272
+ km: number | null;
273
+ /** Their standard deviation (ms; null as km). */
274
+ ks: number | null;
275
+ /** How long keys were held, on average (ms; null as km). */
276
+ hm: number | null;
277
+ /** Mouse or pen presses on an element of at least 8 px. */
278
+ cp: number;
279
+ /** Of those, presses within 1 px of the element's middle. */
280
+ cc: number;
281
+ /** Actions: presses, and the start of each burst of typing. */
282
+ ac: number;
283
+ /** Actions after a pause of 1.5 to 20 seconds. */
284
+ lg: number;
285
+ /** The median pause between actions (ms; null with fewer than two actions). */
286
+ gm: number | null;
287
+ }
288
+ interface InputSummaryProbe {
289
+ summary(): InputSummary;
290
+ /** 17 characters (encodeInputSummary), sent after the pointer summary and a dot. */
291
+ encode(): string;
292
+ stop(): void;
293
+ }
294
+ interface InputSummaryDeps {
295
+ /** Which events came from the person's own hardware (tests). Default: `isTrusted`. */
296
+ trusted?: (e: Event) => boolean;
297
+ }
298
+ /** Starts watching key presses, text input and presses on `root` (default: the document). */
299
+ declare function startInputSummary(root?: Document | HTMLElement, deps?: InputSummaryDeps): InputSummaryProbe;
300
+ /**
301
+ * The summary in 17 characters (base64url digits): version 1, then kd, nk, pa,
302
+ * kn, km, ks, hm, cp, cc, ac, lg, gm÷100. Counts stop at 63 (cc never above
303
+ * cp, lg never above ac), times at 4094 ms (gm at 409.4 s); `__` is none.
304
+ */
305
+ declare function encodeInputSummary(s: InputSummary): string;
306
+
307
+ /** The hidden form field the tag fills; your server passes it to deviceRisk.check. */
308
+ declare const REQUEST_ID_FIELD = "touchque_request_id";
309
+ /** A form's own page purpose (sign_in, sign_up, password_reset, approval). */
310
+ declare const FORM_PURPOSE_ATTRIBUTE = "data-touchque-purpose";
311
+ /** The event on document after each new identification. */
312
+ declare const DEVICE_EVENT = "touchque:device";
313
+ declare const DEVICE_TAG_VERSION = "3.1.0";
314
+ interface DeviceRiskTagOptions {
315
+ /** Your workspace's public site key (`tqpk_…`, dashboard → Device risk → Settings). */
316
+ siteKey: string;
317
+ /** The page, for forms without `data-touchque-purpose`. Default `sign_in`. */
318
+ purpose?: DeviceRiskPurpose;
319
+ /** Touchque's API, default `https://api.touchque.com`. */
320
+ apiBaseUrl?: string;
321
+ /** Send how the pointer and the fingers moved, and how the page was typed into (counts only). Default true. */
322
+ pointer?: boolean;
323
+ }
324
+ interface TouchqueDevice {
325
+ /** The first identification for the page's purpose, or null when Touchque could not be reached or refused (logged to the console). */
326
+ readonly ready: Promise<DeviceRiskIdentification | null>;
327
+ /** The current requestId for the page's purpose; null until ready, or after a failure. */
328
+ readonly requestId: string | null;
329
+ /** The current visitorId for the page's purpose. Never trust it on the page. */
330
+ readonly visitorId: string | null;
331
+ /** What to send to your server now (the form field's value): the requestId, then the pointer and input summaries after dots; null until ready. */
332
+ value(purpose?: DeviceRiskPurpose): string | null;
333
+ /** A new identification now (for the page's purpose, or the one given), and the forms updated. */
334
+ refresh(purpose?: DeviceRiskPurpose): Promise<DeviceRiskIdentification | null>;
335
+ /** Fills forms the tag cannot see (inside a shadow root you own). */
336
+ attach(root: ParentNode): void;
337
+ /** Stops watching the page (tests, a page that unmounts the sign-in). */
338
+ stop(): void;
339
+ readonly version: string;
340
+ }
341
+ interface DeviceRiskTagDeps {
342
+ win?: Window;
343
+ deviceRisk?: Pick<DeviceRisk, 'identify'>;
344
+ now?: () => number;
345
+ pointer?: PointerSummaryProbe | null;
346
+ input?: InputSummaryProbe | null;
347
+ }
348
+ /** Starts the tag's work on this page. Never throws. */
349
+ declare function installDeviceRiskTag(options: DeviceRiskTagOptions, deps?: DeviceRiskTagDeps): TouchqueDevice;
350
+
351
+ /**
352
+ * Touchque's report key: the public half only (the private half never leaves
353
+ * Touchque's servers). `kid` names it in each envelope, so a new key can come
354
+ * with a new tag version while Touchque still opens reports of the old one.
355
+ */
356
+ interface ReportKey {
357
+ kid: string;
358
+ jwk: {
359
+ kty: 'EC';
360
+ crv: 'P-256';
361
+ x: string;
362
+ y: string;
363
+ };
364
+ }
365
+ declare const REPORT_KEY: ReportKey;
366
+
367
+ /**
368
+ * The report, encrypted so only Touchque can read it: the page, its other
369
+ * scripts, extensions and the visitor see an opaque string, and a changed byte
370
+ * makes it unreadable.
371
+ *
372
+ * tqe1.<kid>.<epk>.<iv>.<ct> (base64url parts)
373
+ *
374
+ * A one-time P-256 key pair, ECDH with Touchque's report key (REPORT_KEY, its
375
+ * public half), HKDF-SHA256 (salt: the one-time public key, info
376
+ * "touchque-device-report:v1") gives an AES-256-GCM key; the additional data
377
+ * is "tqe1.<kid>". Without WebCrypto (an http page) the report goes as it is.
378
+ *
379
+ * It hides what the tag collects and keeps it from being edited on the way; a
380
+ * person who controls the browser can still change values before they are
381
+ * encrypted (the key is public by nature): the server's consistency checks and
382
+ * its own signals are there for that.
383
+ */
384
+
385
+ /** The envelope of `json` for Touchque's key, or null without WebCrypto. */
386
+ declare function sealReport(json: string, key?: ReportKey, subtle?: SubtleCrypto | undefined): Promise<string | null>;
387
+
388
+ /** What the SDK logs through: `console`, or anything with `warn`. */
389
+ interface TouchqueWebLogger {
390
+ warn(message: string): void;
391
+ }
392
+ /** Where the SDK reports its own problems; `null` (the default) logs nothing. */
393
+ declare function setTouchqueLogger(logger: TouchqueWebLogger | null): void;
394
+
395
+ export { DEVICE_EVENT, DEVICE_RISK_FIELD, DEVICE_RISK_HEADER, DEVICE_RISK_PURPOSES, DEVICE_TAG_VERSION, type DeviceRisk, type DeviceRiskBody, type DeviceRiskCollectOptions, type DeviceRiskFindings, type DeviceRiskIdentification, type DeviceRiskIdentifyOptions, type DeviceRiskPurpose, type DeviceRiskReport, type DeviceRiskTagOptions, type DeviceRiskTraits, FORM_PURPOSE_ATTRIBUTE, type InputSummary, type InputSummaryProbe, type PointerSummary, type PointerSummaryProbe, REPORT_KEY, REQUEST_ID_FIELD, type ReportKey, type TouchqueDevice, type TouchqueWebLogger, buildDeviceRiskReport, consistencyFindings, createDeviceRisk, deviceRisk, encodeInputSummary, encodePointerSummary, installDeviceRiskTag, jsEngine, sealReport, setTouchqueLogger, startInputSummary, startPointerSummary };