biometry-react-components 2.0.0 → 3.0.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 +29 -10
- package/dist/index.d.ts +127 -61
- package/dist/index.esm.js +131 -21
- package/dist/index.js +130 -20
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -91,22 +91,34 @@ function FaceCapture() {
|
|
|
91
91
|
|
|
92
92
|
Records video with facial guidance overlay and displays challenge digits for the user to read aloud. Use it for [Process Video](https://developer.biometrysolutions.com/api/process-video/).
|
|
93
93
|
|
|
94
|
+
The digits the user reads should be a **liveness challenge issued by your backend**. We recommend 8 distinct digits from `1`–`9`, from a cryptographically secure random source, stored against the user's login attempt, valid for at most 5 minutes and used once. Pass it as `challengeDigits` with `recordingSeconds` around 10, then upload the video to your backend, which calls the Biometry API with **its own stored copy** of the challenge as `phrase`.
|
|
95
|
+
|
|
96
|
+
Why 8 digits from 1–9: the voice engine's quality check needs at least 7 recognised words, so one misheard digit in a 7-digit phrase already fails; 8 leaves a margin, and the Biometry console uses 8 too. `0` is left out because people say it as "zero" or "oh", which hurts voice matching.
|
|
97
|
+
|
|
94
98
|
```tsx
|
|
95
99
|
import { FaceRecorder, type FaceRecording } from 'biometry-react-components';
|
|
96
100
|
|
|
97
|
-
function Verify() {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
//
|
|
101
|
-
//
|
|
102
|
-
//
|
|
103
|
-
//
|
|
101
|
+
function Verify({ challenge }: { challenge: string }) {
|
|
102
|
+
// `challenge` came from your backend, e.g. "48719352".
|
|
103
|
+
const handleConfirm = async ({ file }: FaceRecording) => {
|
|
104
|
+
// Send only the video. Your backend looks up the challenge it stored for
|
|
105
|
+
// this login attempt and sends that as `phrase` to /v2/liveness (and
|
|
106
|
+
// /v2/voice-verify; /v2/face-verify takes no phrase). The voice is extracted server-side
|
|
107
|
+
// from the same file that passed the liveness check, so there is no
|
|
108
|
+
// separate audio to send.
|
|
109
|
+
const body = new FormData();
|
|
110
|
+
body.append('video', file);
|
|
111
|
+
await fetch('/api/login/liveness', { method: 'POST', body });
|
|
104
112
|
};
|
|
105
113
|
|
|
106
|
-
return <FaceRecorder onConfirmRecording={handleConfirm} />;
|
|
114
|
+
return <FaceRecorder challengeDigits={challenge} recordingSeconds={10} onConfirmRecording={handleConfirm} />;
|
|
107
115
|
}
|
|
108
116
|
```
|
|
109
117
|
|
|
118
|
+
**Why your backend's copy, not the emitted `phrase`:** a phrase chosen or returned by the browser doesn't stop replays. Someone who controls the browser, or calls the API directly, can pair a pre-recorded video of the victim with whatever digits that video happens to say, and every speech check agrees because the phrase was picked to fit the video. Only a challenge your backend chose, kept and checked proves the video was recorded just now.
|
|
119
|
+
|
|
120
|
+
> **Deprecated:** if you leave `challengeDigits` out (or pass `null`), the component still generates digits in the browser and emits them as `phrase`, so existing integrations keep working, and it logs a one-time `console.warn` per component instance that device-generated challenges give no replay protection. Move to a backend-issued challenge: see [Liveness challenge](https://developer.biometrysolutions.com/concepts/liveness-challenge/). An empty or invalid value is not treated as omitted; it throws.
|
|
121
|
+
|
|
110
122
|
> **Breaking change in 2.0.0:** both callbacks now receive a single object,
|
|
111
123
|
> `{ file, audio?, phrase }`, instead of the positional `(video, audio, phrase)`.
|
|
112
124
|
> The old middle `audio` argument could not simply be dropped — doing so would
|
|
@@ -125,9 +137,9 @@ function Verify() {
|
|
|
125
137
|
|
|
126
138
|
| Prop | Type | Default | Description |
|
|
127
139
|
|------|------|---------|-------------|
|
|
128
|
-
| `onConfirmRecording` | `(recording: FaceRecording) => void` | **required** | Triggered when the user confirms the recording. Receives `{ file, audio?, phrase }
|
|
140
|
+
| `onConfirmRecording` | `(recording: FaceRecording) => void` | **required** | Triggered when the user confirms the recording. Receives `{ file, audio?, phrase, challenge }`: `phrase` is the digits in words, `challenge` the same digits as shown (exactly your `challengeDigits` when given). Use them for display or logging only; your backend sends its own stored copy. |
|
|
129
141
|
| `onRecorded` | `(recording: FaceRecording) => void` | *optional* | Triggered right after recording finishes, before confirmation. |
|
|
130
|
-
| `challengeDigits` | `number[] \| null` | `undefined` |
|
|
142
|
+
| `challengeDigits` | `string \| number[] \| null` | `undefined` | The liveness challenge your backend issued, e.g. `"48719352"` or `[4, 8, 7, 1, 9, 3, 5, 2]`. Shown and emitted exactly as given. 7 to 12 digits, each `0–9` (8 from `1–9` recommended); a string may contain nothing else. An invalid value (including an empty one) **throws during render** instead of being replaced. A new value is applied only while idle. Leaving it out (or `null`) falls back to browser-generated digits, which is deprecated and logs a one-time `console.warn` per instance. |
|
|
131
143
|
| `recordingSeconds` | `number` | `7` | Recording duration. The window is fixed — capture stops when it expires, and a clipped final digit fails the engine's quality check before any comparison. Widen it when the challenge is longer than the default. |
|
|
132
144
|
| `countdownSeconds` | `number` | `3` | Lead-in before recording starts. |
|
|
133
145
|
| `emitAudio` | `boolean` | `false` | Also record a separate audio track and expose it as `audio`. See the note above. |
|
|
@@ -313,6 +325,13 @@ function AudioPermissionManager() {
|
|
|
313
325
|
| `requestPermissions` | `() => Promise<void>` | Request microphone permissions |
|
|
314
326
|
| `isLoading` | `boolean` | Loading state during permission request |
|
|
315
327
|
|
|
328
|
+
## Development
|
|
329
|
+
|
|
330
|
+
```bash
|
|
331
|
+
npm test # unit and component tests (vitest + jsdom), Node 18 or newer
|
|
332
|
+
npm run build:clean # dist/ with types
|
|
333
|
+
```
|
|
334
|
+
|
|
316
335
|
## License
|
|
317
336
|
|
|
318
337
|
MIT License - see the [LICENSE](LICENSE) file for details.
|
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,100 @@ interface FaceCaptureProps {
|
|
|
22
22
|
}
|
|
23
23
|
declare const FaceCapture: React.FC<FaceCaptureProps>;
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Digit challenge and phrase helpers, kept in step with
|
|
27
|
+
* biometry-angular-components `src/lib/utils/numbers.ts`. The two packages must
|
|
28
|
+
* produce the same `phrase` for the same digits: it is compared against what the
|
|
29
|
+
* voice engine recognised, so a divergence here is a verification failure with no
|
|
30
|
+
* obvious cause.
|
|
31
|
+
*/
|
|
32
|
+
/**
|
|
33
|
+
* Digits a generated challenge may contain.
|
|
34
|
+
*
|
|
35
|
+
* `0` is deliberately absent. A server-issued challenge may still contain one
|
|
36
|
+
* while the backend rolls out, and numbersToPhrase tolerates that — but nothing
|
|
37
|
+
* we generate emits it.
|
|
38
|
+
*/
|
|
39
|
+
declare const CHALLENGE_DIGITS: readonly number[];
|
|
40
|
+
/**
|
|
41
|
+
* Enrolment prompts, taken verbatim from the vendor guide's published set.
|
|
42
|
+
*
|
|
43
|
+
* These are fixed, not random, and that is the point: random prompting is only
|
|
44
|
+
* required at verification time. Presenting every user the same sequences is
|
|
45
|
+
* also what makes cross-user calibration accurate — and calibration is what
|
|
46
|
+
* sets our acceptance threshold (BM-449).
|
|
47
|
+
*
|
|
48
|
+
* The guide selects these specifically because they contain no duplicate
|
|
49
|
+
* 2-digit contexts: biometric performance depends on co-articulation between
|
|
50
|
+
* adjacent digits, and "321" and "123" are different contexts for "2". Drawing
|
|
51
|
+
* enrolment prompts at random gives each user an arbitrary, unmeasured slice of
|
|
52
|
+
* that space.
|
|
53
|
+
*
|
|
54
|
+
* Order matters: the first two are count-up and count-down, which the guide
|
|
55
|
+
* uses to open its enrolment flow because they are the easiest to comply with.
|
|
56
|
+
*/
|
|
57
|
+
declare const ENROLMENT_DIGIT_SEQUENCES: readonly (readonly number[])[];
|
|
58
|
+
/**
|
|
59
|
+
* Render digits as the spoken words the engine expects in `phrase`.
|
|
60
|
+
*
|
|
61
|
+
* Lowercase, matching the Angular package and therefore the only casing that
|
|
62
|
+
* has been exercised in production. The gateway forwards `phrase` to the engine
|
|
63
|
+
* untouched, so the client's casing is what gets compared.
|
|
64
|
+
*
|
|
65
|
+
* `zero` is retained even though we no longer generate it: a server-issued
|
|
66
|
+
* challenge may contain a 0, and rendering it as "0" would send a phrase the
|
|
67
|
+
* engine cannot match at all. A digit outside 0–9 is passed through as-is
|
|
68
|
+
* rather than silently becoming a word that was never displayed.
|
|
69
|
+
*/
|
|
70
|
+
declare function numbersToPhrase(digits: number[]): string;
|
|
71
|
+
/**
|
|
72
|
+
* `count` distinct digits drawn from CHALLENGE_DIGITS, in random order.
|
|
73
|
+
*
|
|
74
|
+
* Used for the face recorder's fallback challenge when the server issued none.
|
|
75
|
+
* Enrolment does not call this — see ENROLMENT_DIGIT_SEQUENCES.
|
|
76
|
+
*
|
|
77
|
+
* @deprecated as a source of liveness challenges: digits generated in the
|
|
78
|
+
* browser don't stop replays, because whoever controls the browser can pair a
|
|
79
|
+
* pre-recorded video with the digits it happens to say. Have your backend
|
|
80
|
+
* generate the challenge, pass it to the face recorder as `challengeDigits`,
|
|
81
|
+
* and send your backend's own stored copy as `phrase`. See the dev portal's
|
|
82
|
+
* "Liveness challenge" guide. The face recorder still calls this for its
|
|
83
|
+
* deprecated fallback when no challenge is given.
|
|
84
|
+
*/
|
|
85
|
+
declare function generateRandomDigits(count: number): number[];
|
|
86
|
+
/**
|
|
87
|
+
* Shortest liveness challenge accepted from your backend.
|
|
88
|
+
*
|
|
89
|
+
* The voice engine's quality check needs at least 7 recognised words, so a
|
|
90
|
+
* shorter phrase can't be used for voice enrollment (voice auto-enrollment
|
|
91
|
+
* skips it). We recommend issuing 8 distinct digits from 1-9.
|
|
92
|
+
*/
|
|
93
|
+
declare const MIN_CHALLENGE_DIGITS = 7;
|
|
94
|
+
/**
|
|
95
|
+
* Longest liveness challenge accepted from your backend. Longer phrases don't
|
|
96
|
+
* fit a sensible recording window.
|
|
97
|
+
*/
|
|
98
|
+
declare const MAX_CHALLENGE_DIGITS = 12;
|
|
99
|
+
/**
|
|
100
|
+
* A liveness challenge issued by your backend: either the digit string your
|
|
101
|
+
* backend stored (e.g. `"48719352"`) or the same digits as numbers.
|
|
102
|
+
*/
|
|
103
|
+
type ChallengeInput = string | readonly number[];
|
|
104
|
+
/**
|
|
105
|
+
* Validate a challenge issued by your backend and return its digits, in order
|
|
106
|
+
* and unchanged.
|
|
107
|
+
*
|
|
108
|
+
* Returns `null` only when no challenge was given (`null` / `undefined`); the
|
|
109
|
+
* face recorder then falls back to generating digits on the device, which is
|
|
110
|
+
* deprecated. Anything else that isn't MIN_CHALLENGE_DIGITS to
|
|
111
|
+
* MAX_CHALLENGE_DIGITS digits 0–9 throws, rather than being replaced by generated digits: a silently
|
|
112
|
+
* swapped challenge would have the user speak digits your backend never issued.
|
|
113
|
+
*
|
|
114
|
+
* A string must contain only the ASCII digits `0`–`9`, with no spaces or
|
|
115
|
+
* separators. An array must contain only integers 0–9.
|
|
116
|
+
*/
|
|
117
|
+
declare function parseChallenge(challenge: ChallengeInput | null | undefined): number[] | null;
|
|
118
|
+
|
|
25
119
|
/**
|
|
26
120
|
* What the recorder hands back. `audio` is present only when `emitAudio` is set.
|
|
27
121
|
*
|
|
@@ -32,18 +126,44 @@ declare const FaceCapture: React.FC<FaceCaptureProps>;
|
|
|
32
126
|
interface FaceRecording {
|
|
33
127
|
file: File;
|
|
34
128
|
audio?: File;
|
|
129
|
+
/** The digits the user was asked to read, spelled out in words. */
|
|
35
130
|
phrase: string;
|
|
131
|
+
/**
|
|
132
|
+
* The same digits as shown on screen, e.g. `"48719352"`. When you passed
|
|
133
|
+
* `challengeDigits`, this is exactly that challenge.
|
|
134
|
+
*
|
|
135
|
+
* For display and logging only. Neither this nor `phrase` is proof of what
|
|
136
|
+
* your backend issued: when you check liveness, send your backend's own
|
|
137
|
+
* stored copy of the challenge as `phrase`.
|
|
138
|
+
*/
|
|
139
|
+
challenge: string;
|
|
36
140
|
}
|
|
37
141
|
interface FaceRecorderProps {
|
|
38
142
|
rectWidth?: number;
|
|
39
143
|
rectHeight?: number;
|
|
40
144
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
145
|
+
* The liveness challenge your backend issued, which the user reads aloud: a
|
|
146
|
+
* digit string such as `"48719352"` or the same digits as numbers. It is shown
|
|
147
|
+
* and emitted exactly as given.
|
|
148
|
+
*
|
|
149
|
+
* Must be 7 to 12 digits, each 0–9 (a string may contain nothing else). We
|
|
150
|
+
* recommend 8 distinct digits from 1–9, with `recordingSeconds` around 10.
|
|
151
|
+
* An invalid value throws during render instead of being replaced, so wrap
|
|
152
|
+
* the recorder in an error boundary if the value isn't trusted. Pass `null`
|
|
153
|
+
* or leave it out only while there is no challenge; render the recorder once
|
|
154
|
+
* the challenge has arrived so the user never starts with generated digits.
|
|
155
|
+
* A new value is adopted while idle, never mid-capture.
|
|
156
|
+
*
|
|
157
|
+
* When you check liveness, send your backend's own stored copy of the
|
|
158
|
+
* challenge as `phrase`, never the one this component emits.
|
|
159
|
+
*
|
|
160
|
+
* Deprecated fallback: leaving this out makes the component generate digits
|
|
161
|
+
* in the browser, which doesn't stop a pre-recorded video being replayed with
|
|
162
|
+
* a matching phrase, and logs a one-time `console.warn` per component
|
|
163
|
+
* instance. Have your backend issue the challenge instead. An empty or
|
|
164
|
+
* invalid value is not treated as omitted: it throws.
|
|
45
165
|
*/
|
|
46
|
-
challengeDigits?:
|
|
166
|
+
challengeDigits?: ChallengeInput | null;
|
|
47
167
|
/**
|
|
48
168
|
* Recording duration in seconds.
|
|
49
169
|
*
|
|
@@ -107,60 +227,6 @@ interface VoiceRecorderProps {
|
|
|
107
227
|
}
|
|
108
228
|
declare const VoiceRecorder: React.FC<VoiceRecorderProps>;
|
|
109
229
|
|
|
110
|
-
/**
|
|
111
|
-
* Digit challenge and phrase helpers, kept in step with
|
|
112
|
-
* biometry-angular-components `src/lib/utils/numbers.ts`. The two packages must
|
|
113
|
-
* produce the same `phrase` for the same digits: it is compared against what the
|
|
114
|
-
* voice engine recognised, so a divergence here is a verification failure with no
|
|
115
|
-
* obvious cause.
|
|
116
|
-
*/
|
|
117
|
-
/**
|
|
118
|
-
* Digits a generated challenge may contain.
|
|
119
|
-
*
|
|
120
|
-
* `0` is deliberately absent. A server-issued challenge may still contain one
|
|
121
|
-
* while the backend rolls out, and numbersToPhrase tolerates that — but nothing
|
|
122
|
-
* we generate emits it.
|
|
123
|
-
*/
|
|
124
|
-
declare const CHALLENGE_DIGITS: readonly number[];
|
|
125
|
-
/**
|
|
126
|
-
* Enrolment prompts, taken verbatim from the vendor guide's published set.
|
|
127
|
-
*
|
|
128
|
-
* These are fixed, not random, and that is the point: random prompting is only
|
|
129
|
-
* required at verification time. Presenting every user the same sequences is
|
|
130
|
-
* also what makes cross-user calibration accurate — and calibration is what
|
|
131
|
-
* sets our acceptance threshold (BM-449).
|
|
132
|
-
*
|
|
133
|
-
* The guide selects these specifically because they contain no duplicate
|
|
134
|
-
* 2-digit contexts: biometric performance depends on co-articulation between
|
|
135
|
-
* adjacent digits, and "321" and "123" are different contexts for "2". Drawing
|
|
136
|
-
* enrolment prompts at random gives each user an arbitrary, unmeasured slice of
|
|
137
|
-
* that space.
|
|
138
|
-
*
|
|
139
|
-
* Order matters: the first two are count-up and count-down, which the guide
|
|
140
|
-
* uses to open its enrolment flow because they are the easiest to comply with.
|
|
141
|
-
*/
|
|
142
|
-
declare const ENROLMENT_DIGIT_SEQUENCES: readonly (readonly number[])[];
|
|
143
|
-
/**
|
|
144
|
-
* Render digits as the spoken words the engine expects in `phrase`.
|
|
145
|
-
*
|
|
146
|
-
* Lowercase, matching the Angular package and therefore the only casing that
|
|
147
|
-
* has been exercised in production. The gateway forwards `phrase` to the engine
|
|
148
|
-
* untouched, so the client's casing is what gets compared.
|
|
149
|
-
*
|
|
150
|
-
* `zero` is retained even though we no longer generate it: a server-issued
|
|
151
|
-
* challenge may contain a 0, and rendering it as "0" would send a phrase the
|
|
152
|
-
* engine cannot match at all. A digit outside 0–9 is passed through as-is
|
|
153
|
-
* rather than silently becoming a word that was never displayed.
|
|
154
|
-
*/
|
|
155
|
-
declare function numbersToPhrase(digits: number[]): string;
|
|
156
|
-
/**
|
|
157
|
-
* `count` distinct digits drawn from CHALLENGE_DIGITS, in random order.
|
|
158
|
-
*
|
|
159
|
-
* Used for the face recorder's fallback challenge when the server issued none.
|
|
160
|
-
* Enrolment does not call this — see ENROLMENT_DIGIT_SEQUENCES.
|
|
161
|
-
*/
|
|
162
|
-
declare function generateRandomDigits(count: number): number[];
|
|
163
|
-
|
|
164
230
|
type OnStopRecordingFunc = (blob: Blob) => void;
|
|
165
231
|
declare const useRecorder: (stream: MediaStream | null, type: "audio" | "video", onStopRecording: OnStopRecordingFunc, lengthMs?: number) => {
|
|
166
232
|
isRecording: boolean;
|
|
@@ -189,5 +255,5 @@ declare const useAudioPermissions: () => {
|
|
|
189
255
|
isLoading: boolean;
|
|
190
256
|
};
|
|
191
257
|
|
|
192
|
-
export { CHALLENGE_DIGITS, DocScan, ENROLMENT_DIGIT_SEQUENCES, FaceCapture, FaceRecorder, VoiceRecorder, generateRandomDigits, numbersToPhrase, useAudioPermissions, usePermissions, useRecorder };
|
|
193
|
-
export type { FaceRecorderProps, FaceRecording, VoiceEnrollment, VoiceRecorderProps, VoiceTake };
|
|
258
|
+
export { CHALLENGE_DIGITS, DocScan, ENROLMENT_DIGIT_SEQUENCES, FaceCapture, FaceRecorder, MAX_CHALLENGE_DIGITS, MIN_CHALLENGE_DIGITS, VoiceRecorder, generateRandomDigits, numbersToPhrase, parseChallenge, useAudioPermissions, usePermissions, useRecorder };
|
|
259
|
+
export type { ChallengeInput, FaceRecorderProps, FaceRecording, VoiceEnrollment, VoiceRecorderProps, VoiceTake };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,useEffect}from'react';const DocScan = ({ rectWidth = 640, rectHeight = 400, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
|
|
1
|
+
import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,useEffect,useMemo}from'react';const DocScan = ({ rectWidth = 640, rectHeight = 400, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
|
|
2
2
|
var _a;
|
|
3
3
|
const [isConfirming, setIsConfirming] = useState(false);
|
|
4
4
|
const [capturedUrl, setCapturedUrl] = useState(null);
|
|
@@ -7278,6 +7278,14 @@ function numbersToPhrase(digits) {
|
|
|
7278
7278
|
*
|
|
7279
7279
|
* Used for the face recorder's fallback challenge when the server issued none.
|
|
7280
7280
|
* Enrolment does not call this — see ENROLMENT_DIGIT_SEQUENCES.
|
|
7281
|
+
*
|
|
7282
|
+
* @deprecated as a source of liveness challenges: digits generated in the
|
|
7283
|
+
* browser don't stop replays, because whoever controls the browser can pair a
|
|
7284
|
+
* pre-recorded video with the digits it happens to say. Have your backend
|
|
7285
|
+
* generate the challenge, pass it to the face recorder as `challengeDigits`,
|
|
7286
|
+
* and send your backend's own stored copy as `phrase`. See the dev portal's
|
|
7287
|
+
* "Liveness challenge" guide. The face recorder still calls this for its
|
|
7288
|
+
* deprecated fallback when no challenge is given.
|
|
7281
7289
|
*/
|
|
7282
7290
|
function generateRandomDigits(count) {
|
|
7283
7291
|
const digits = [...CHALLENGE_DIGITS];
|
|
@@ -7286,6 +7294,56 @@ function generateRandomDigits(count) {
|
|
|
7286
7294
|
[digits[i], digits[j]] = [digits[j], digits[i]];
|
|
7287
7295
|
}
|
|
7288
7296
|
return digits.slice(0, count);
|
|
7297
|
+
}
|
|
7298
|
+
/**
|
|
7299
|
+
* Shortest liveness challenge accepted from your backend.
|
|
7300
|
+
*
|
|
7301
|
+
* The voice engine's quality check needs at least 7 recognised words, so a
|
|
7302
|
+
* shorter phrase can't be used for voice enrollment (voice auto-enrollment
|
|
7303
|
+
* skips it). We recommend issuing 8 distinct digits from 1-9.
|
|
7304
|
+
*/
|
|
7305
|
+
const MIN_CHALLENGE_DIGITS = 7;
|
|
7306
|
+
/**
|
|
7307
|
+
* Longest liveness challenge accepted from your backend. Longer phrases don't
|
|
7308
|
+
* fit a sensible recording window.
|
|
7309
|
+
*/
|
|
7310
|
+
const MAX_CHALLENGE_DIGITS = 12;
|
|
7311
|
+
/**
|
|
7312
|
+
* Validate a challenge issued by your backend and return its digits, in order
|
|
7313
|
+
* and unchanged.
|
|
7314
|
+
*
|
|
7315
|
+
* Returns `null` only when no challenge was given (`null` / `undefined`); the
|
|
7316
|
+
* face recorder then falls back to generating digits on the device, which is
|
|
7317
|
+
* deprecated. Anything else that isn't MIN_CHALLENGE_DIGITS to
|
|
7318
|
+
* MAX_CHALLENGE_DIGITS digits 0–9 throws, rather than being replaced by generated digits: a silently
|
|
7319
|
+
* swapped challenge would have the user speak digits your backend never issued.
|
|
7320
|
+
*
|
|
7321
|
+
* A string must contain only the ASCII digits `0`–`9`, with no spaces or
|
|
7322
|
+
* separators. An array must contain only integers 0–9.
|
|
7323
|
+
*/
|
|
7324
|
+
function parseChallenge(challenge) {
|
|
7325
|
+
if (challenge === null || challenge === undefined)
|
|
7326
|
+
return null;
|
|
7327
|
+
let digits;
|
|
7328
|
+
if (typeof challenge === 'string') {
|
|
7329
|
+
if (!/^[0-9]+$/.test(challenge)) {
|
|
7330
|
+
throw new Error(`Invalid liveness challenge ${JSON.stringify(challenge)}: use only the digits 0-9, with no spaces or separators.`);
|
|
7331
|
+
}
|
|
7332
|
+
digits = Array.from(challenge, c => c.charCodeAt(0) - 48);
|
|
7333
|
+
}
|
|
7334
|
+
else if (Array.isArray(challenge)) {
|
|
7335
|
+
if (!challenge.every(d => Number.isInteger(d) && d >= 0 && d <= 9)) {
|
|
7336
|
+
throw new Error(`Invalid liveness challenge ${JSON.stringify(challenge)}: every element must be an integer from 0 to 9.`);
|
|
7337
|
+
}
|
|
7338
|
+
digits = [...challenge];
|
|
7339
|
+
}
|
|
7340
|
+
else {
|
|
7341
|
+
throw new Error('Invalid liveness challenge: pass a digit string such as "48719352" or an array of digits.');
|
|
7342
|
+
}
|
|
7343
|
+
if (digits.length < MIN_CHALLENGE_DIGITS || digits.length > MAX_CHALLENGE_DIGITS) {
|
|
7344
|
+
throw new Error(`Invalid liveness challenge ${JSON.stringify(challenge)}: it has ${digits.length} digits, ${MIN_CHALLENGE_DIGITS} to ${MAX_CHALLENGE_DIGITS} are required.`);
|
|
7345
|
+
}
|
|
7346
|
+
return digits;
|
|
7289
7347
|
}function getSupportedMimeType() {
|
|
7290
7348
|
if (MediaRecorder.isTypeSupported('video/webm;codecs=vp9')) {
|
|
7291
7349
|
return 'video/webm;codecs=vp9';
|
|
@@ -7401,7 +7459,21 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7401
7459
|
}
|
|
7402
7460
|
};
|
|
7403
7461
|
return { isRecording, startRecording, stopRecording, cancelRecording };
|
|
7404
|
-
}
|
|
7462
|
+
};// Video constraints are IDEAL-ONLY - never `min` (BM-530).
|
|
7463
|
+
//
|
|
7464
|
+
// Without a deviceId, getUserMedia considers every camera and returns one that
|
|
7465
|
+
// satisfies all required (min/max/exact) constraints. A hard `min` therefore
|
|
7466
|
+
// filters cameras: with `width.min: 1280` a 640x480 laptop webcam is silently
|
|
7467
|
+
// excluded and the browser falls back to whatever passes - typically a virtual
|
|
7468
|
+
// camera (OBS) that accepts any resolution - or, with no such camera, throws
|
|
7469
|
+
// OverconstrainedError, which used to surface as "Camera Permission Required".
|
|
7470
|
+
// Chrome's camera picker for the site is narrowed the same way.
|
|
7471
|
+
//
|
|
7472
|
+
// The old minimums survive as floors on the ideal (`idealSize`): an HD camera
|
|
7473
|
+
// is still steered to at least the same resolution as before, while a weaker
|
|
7474
|
+
// camera is accepted at its best mode instead of being rejected.
|
|
7475
|
+
const idealSize = (requested, floor, cap) => Math.max(floor, Math.min(requested, cap));
|
|
7476
|
+
const usePermissions = ({ rectWidth = 1920, rectHeight = 1080 } = {}) => {
|
|
7405
7477
|
const [permission, setPermission] = useState(false);
|
|
7406
7478
|
const [stream, setStream] = useState(null);
|
|
7407
7479
|
const [isLoading, setIsLoading] = useState(true);
|
|
@@ -7434,9 +7506,9 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7434
7506
|
channelCount: 1 // Mono: the gateway downmixes with -ac 1 and the engine expects mono (BM-446)
|
|
7435
7507
|
},
|
|
7436
7508
|
video: {
|
|
7437
|
-
width: { ideal:
|
|
7438
|
-
height: { ideal:
|
|
7439
|
-
frameRate: { ideal: 30
|
|
7509
|
+
width: { ideal: idealSize(rectWidth, 1280, 1920) },
|
|
7510
|
+
height: { ideal: idealSize(rectHeight, 720, 1080) },
|
|
7511
|
+
frameRate: { ideal: 30 },
|
|
7440
7512
|
facingMode: 'user',
|
|
7441
7513
|
aspectRatio: { ideal: rectWidth / rectHeight }
|
|
7442
7514
|
}
|
|
@@ -7453,9 +7525,9 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7453
7525
|
channelCount: 1
|
|
7454
7526
|
},
|
|
7455
7527
|
video: {
|
|
7456
|
-
width: { ideal:
|
|
7457
|
-
height: { ideal:
|
|
7458
|
-
frameRate: { ideal: 24
|
|
7528
|
+
width: { ideal: idealSize(rectWidth, 640, 1280) },
|
|
7529
|
+
height: { ideal: idealSize(rectHeight, 480, 720) },
|
|
7530
|
+
frameRate: { ideal: 24 },
|
|
7459
7531
|
facingMode: 'user',
|
|
7460
7532
|
aspectRatio: { ideal: rectWidth / rectHeight }
|
|
7461
7533
|
}
|
|
@@ -7471,15 +7543,16 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7471
7543
|
channelCount: 1
|
|
7472
7544
|
},
|
|
7473
7545
|
video: {
|
|
7474
|
-
width: { ideal:
|
|
7475
|
-
height: { ideal:
|
|
7476
|
-
frameRate: { ideal: 20
|
|
7546
|
+
width: { ideal: idealSize(rectWidth, 640, 1280) },
|
|
7547
|
+
height: { ideal: idealSize(rectHeight, 480, 720) },
|
|
7548
|
+
frameRate: { ideal: 20 },
|
|
7477
7549
|
facingMode: 'user',
|
|
7478
7550
|
aspectRatio: { ideal: rectWidth / rectHeight }
|
|
7479
7551
|
}
|
|
7480
7552
|
};
|
|
7481
7553
|
};
|
|
7482
7554
|
const requestPermissions = async () => {
|
|
7555
|
+
var _a, _b;
|
|
7483
7556
|
try {
|
|
7484
7557
|
setIsLoading(true);
|
|
7485
7558
|
const constraints = getAdaptiveConstraints();
|
|
@@ -7496,7 +7569,10 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7496
7569
|
setStream(streamData);
|
|
7497
7570
|
}
|
|
7498
7571
|
catch (err) {
|
|
7499
|
-
|
|
7572
|
+
// Not every failure is a denied permission: OverconstrainedError names
|
|
7573
|
+
// the failing rule, NotReadableError means the camera is busy.
|
|
7574
|
+
const e = err;
|
|
7575
|
+
console.error('biometry-react-components: getUserMedia failed:', e === null || e === void 0 ? void 0 : e.name, (_a = e === null || e === void 0 ? void 0 : e.constraint) !== null && _a !== void 0 ? _a : '', (_b = e === null || e === void 0 ? void 0 : e.message) !== null && _b !== void 0 ? _b : '');
|
|
7500
7576
|
if (isMountedRef.current)
|
|
7501
7577
|
setPermission(false);
|
|
7502
7578
|
}
|
|
@@ -7538,11 +7614,41 @@ if (typeof window !== 'undefined' && typeof console !== 'undefined') {
|
|
|
7538
7614
|
const DEFAULT_RECORD_SECONDS = VIDEO_LENGTH_MS / 1000;
|
|
7539
7615
|
const DEFAULT_PREP_SECONDS = 3;
|
|
7540
7616
|
const DIGITS_NUMBER$1 = 7;
|
|
7617
|
+
/** Printed once per FaceRecorder instance when no challenge is given. */
|
|
7618
|
+
const DEVICE_CHALLENGE_WARNING = 'biometry-react-components FaceRecorder: no challengeDigits given, so the challenge was generated in the browser. ' +
|
|
7619
|
+
'This is deprecated: device-generated challenges give no replay protection. ' +
|
|
7620
|
+
'Pass a challenge issued by your backend as challengeDigits. ' +
|
|
7621
|
+
'See https://developer.biometrysolutions.com/concepts/liveness-challenge/';
|
|
7622
|
+
/**
|
|
7623
|
+
* A stable memo key for a challenge: the string itself, or the JSON of an
|
|
7624
|
+
* array (prefixed so a string and an array never share a key).
|
|
7625
|
+
*/
|
|
7626
|
+
const challengeKeyOf = (challenge) => {
|
|
7627
|
+
if (challenge === null || challenge === undefined)
|
|
7628
|
+
return null;
|
|
7629
|
+
if (typeof challenge === 'string')
|
|
7630
|
+
return `s:${challenge}`;
|
|
7631
|
+
try {
|
|
7632
|
+
return `a:${JSON.stringify(challenge)}`;
|
|
7633
|
+
}
|
|
7634
|
+
catch (_a) {
|
|
7635
|
+
// Not serialisable, so certainly invalid: let parseChallenge reject it.
|
|
7636
|
+
return `x:${String(challenge)}`;
|
|
7637
|
+
}
|
|
7638
|
+
};
|
|
7541
7639
|
const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, recordingSeconds = DEFAULT_RECORD_SECONDS, countdownSeconds = DEFAULT_PREP_SECONDS, emitAudio = false, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
|
|
7542
7640
|
const [state, setState] = useState('preparation');
|
|
7543
7641
|
const [prepTimer, setPrepTimer] = useState(countdownSeconds);
|
|
7544
7642
|
const [recordTimer, setRecordTimer] = useState(recordingSeconds);
|
|
7545
|
-
|
|
7643
|
+
// Keyed on the challenge's value, not its identity, so an inline array
|
|
7644
|
+
// literal (a new array every parent render) doesn't re-run the effects below.
|
|
7645
|
+
const challengeKey = challengeKeyOf(challengeDigits);
|
|
7646
|
+
// Validated whenever the value changes, so an invalid challenge throws here,
|
|
7647
|
+
// visibly, instead of quietly falling back to generated digits.
|
|
7648
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7649
|
+
const issuedDigits = useMemo(() => parseChallenge(challengeDigits), [challengeKey]);
|
|
7650
|
+
const warnedDeviceChallengeRef = useRef(false);
|
|
7651
|
+
const [digits, setDigits] = useState(() => issuedDigits !== null && issuedDigits !== void 0 ? issuedDigits : generateRandomDigits(DIGITS_NUMBER$1));
|
|
7546
7652
|
const { permission, stream, stopStreamTracks, requestPermissions, isLoading } = usePermissions({ rectWidth, rectHeight });
|
|
7547
7653
|
const [videoBlob, setVideoBlob] = useState(null);
|
|
7548
7654
|
const [audioBlob, setAudioBlob] = useState(null);
|
|
@@ -7655,19 +7761,19 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
|
|
|
7655
7761
|
}
|
|
7656
7762
|
}, [state, recordTimer]);
|
|
7657
7763
|
/**
|
|
7658
|
-
*
|
|
7659
|
-
* one place so every reset path applies
|
|
7764
|
+
* Use the challenge your backend issued; without one, fall back to the
|
|
7765
|
+
* deprecated generated digits. Kept in one place so every reset path applies
|
|
7766
|
+
* the same rule.
|
|
7660
7767
|
*/
|
|
7661
7768
|
const applyChallenge = () => {
|
|
7662
|
-
|
|
7663
|
-
const issued = (_a = challengeDigits === null || challengeDigits === void 0 ? void 0 : challengeDigits.filter(d => Number.isInteger(d) && d >= 0 && d <= 9)) !== null && _a !== void 0 ? _a : [];
|
|
7664
|
-
setDigits(issued.length > 0 ? issued : generateRandomDigits(DIGITS_NUMBER$1));
|
|
7769
|
+
setDigits(issuedDigits ? [...issuedDigits] : generateRandomDigits(DIGITS_NUMBER$1));
|
|
7665
7770
|
};
|
|
7666
7771
|
/** Build the emitted payload; `audio` is omitted unless a blob exists. */
|
|
7667
7772
|
const buildRecording = (video, audio, challenge) => ({
|
|
7668
7773
|
file: new File([video], 'face-video.webm', { type: video.type }),
|
|
7669
7774
|
...(audio ? { audio: new File([audio], 'face-audio.webm', { type: audio.type }) } : {}),
|
|
7670
7775
|
phrase: numbersToPhrase(challenge),
|
|
7776
|
+
challenge: challenge.join(''),
|
|
7671
7777
|
});
|
|
7672
7778
|
/** Return to the start after a capture that produced no usable video. */
|
|
7673
7779
|
const resetAfterFailedCapture = () => {
|
|
@@ -7682,10 +7788,14 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
|
|
|
7682
7788
|
};
|
|
7683
7789
|
// Adopt a challenge that arrives (or changes) while idle; never mid-capture.
|
|
7684
7790
|
useEffect(() => {
|
|
7791
|
+
if (issuedDigits === null && !warnedDeviceChallengeRef.current) {
|
|
7792
|
+
warnedDeviceChallengeRef.current = true;
|
|
7793
|
+
console.warn(DEVICE_CHALLENGE_WARNING);
|
|
7794
|
+
}
|
|
7685
7795
|
if (state === 'preparation')
|
|
7686
7796
|
applyChallenge();
|
|
7687
7797
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7688
|
-
}, [
|
|
7798
|
+
}, [issuedDigits]);
|
|
7689
7799
|
const startRecording = async () => {
|
|
7690
7800
|
applyChallenge();
|
|
7691
7801
|
setRecordTimer(recordingSeconds);
|
|
@@ -7974,7 +8084,7 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
|
|
|
7974
8084
|
background: 'rgba(0,0,0,0.4)',
|
|
7975
8085
|
zIndex: 1,
|
|
7976
8086
|
borderRadius: noShadow ? 0 : 8,
|
|
7977
|
-
} }), jsxs("div", { className: "facerecorder-oval-area", style: ovalArea, children: [jsx("span", { className: "facerecorder-guidance-text", style: guidanceText, children: "Place your face in the oval" }), jsx("div", { className: "facerecorder-oval-dashed", style: ovalDashed })] }), state === 'countdown' && (jsx("div", { style: prepTimerStyle, children: prepTimer })), state === 'recording' && (jsx("div", { style: timerStyle, children: recordTimer })), (state === 'recording' || state === 'preparation') && (jsxs("div", { style: soundwaveStyle, children: [jsx("div", { style: { ...barBase, height: `${8 + micLevel * 30}px` } }), jsx("div", { style: { ...barBase, height: `${4 + micLevel * 40}px` } }), jsx("div", { style: { ...barBase, height: `${10 + micLevel * 20}px` } })] }))] }), jsx("div", { style: numberRow, children: (state === 'recording' ? digits : Array(
|
|
8087
|
+
} }), jsxs("div", { className: "facerecorder-oval-area", style: ovalArea, children: [jsx("span", { className: "facerecorder-guidance-text", style: guidanceText, children: "Place your face in the oval" }), jsx("div", { className: "facerecorder-oval-dashed", style: ovalDashed })] }), state === 'countdown' && (jsx("div", { style: prepTimerStyle, children: prepTimer })), state === 'recording' && (jsx("div", { style: timerStyle, children: recordTimer })), (state === 'recording' || state === 'preparation') && (jsxs("div", { style: soundwaveStyle, children: [jsx("div", { style: { ...barBase, height: `${8 + micLevel * 30}px` } }), jsx("div", { style: { ...barBase, height: `${4 + micLevel * 40}px` } }), jsx("div", { style: { ...barBase, height: `${10 + micLevel * 20}px` } })] }))] }), jsx("div", { style: numberRow, children: (state === 'recording' ? digits : Array(digits.length).fill('*')).map((num, i) => (jsx("div", { style: numberBox, children: num }, i))) }), state === 'preparation' && (jsx("button", { type: "button", style: actionButton, onClick: startRecording, children: "Start recording" })), state === 'recording' && (jsx("button", { type: "button", style: { ...actionButton, background: '#d32f2f' }, onClick: handleCancel, children: "Cancel" }))] })), state === 'processing' && (jsx(Fragment, { children: jsx("div", { className: "facerecorder-processing", style: preview, children: jsxs("div", { style: {
|
|
7978
8088
|
display: 'flex',
|
|
7979
8089
|
flexDirection: 'column',
|
|
7980
8090
|
alignItems: 'center',
|
|
@@ -8495,4 +8605,4 @@ const VoiceRecorder = ({ rectWidth = 360, rectHeight, takes = DEFAULT_TAKES, onC
|
|
|
8495
8605
|
maxWidth: '300px',
|
|
8496
8606
|
marginBottom: '16px',
|
|
8497
8607
|
} })), jsx("div", { className: "voicerecorder-confirm-text", style: confirmText, children: "Do you want to use this recording?" }), jsxs("div", { className: "voicerecorder-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 12, width: '100%' }, children: [jsx("button", { type: "button", "aria-label": "Retake", className: "voicerecorder-icon-btn", style: iconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsx("button", { type: "button", "aria-label": "Confirm", className: "voicerecorder-icon-btn", style: iconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] }) }))] }));
|
|
8498
|
-
};export{CHALLENGE_DIGITS,DocScan,ENROLMENT_DIGIT_SEQUENCES,FaceCapture,FaceRecorder,VoiceRecorder,generateRandomDigits,numbersToPhrase,useAudioPermissions,usePermissions,useRecorder};
|
|
8608
|
+
};export{CHALLENGE_DIGITS,DocScan,ENROLMENT_DIGIT_SEQUENCES,FaceCapture,FaceRecorder,MAX_CHALLENGE_DIGITS,MIN_CHALLENGE_DIGITS,VoiceRecorder,generateRandomDigits,numbersToPhrase,parseChallenge,useAudioPermissions,usePermissions,useRecorder};
|
package/dist/index.js
CHANGED
|
@@ -7278,6 +7278,14 @@ function numbersToPhrase(digits) {
|
|
|
7278
7278
|
*
|
|
7279
7279
|
* Used for the face recorder's fallback challenge when the server issued none.
|
|
7280
7280
|
* Enrolment does not call this — see ENROLMENT_DIGIT_SEQUENCES.
|
|
7281
|
+
*
|
|
7282
|
+
* @deprecated as a source of liveness challenges: digits generated in the
|
|
7283
|
+
* browser don't stop replays, because whoever controls the browser can pair a
|
|
7284
|
+
* pre-recorded video with the digits it happens to say. Have your backend
|
|
7285
|
+
* generate the challenge, pass it to the face recorder as `challengeDigits`,
|
|
7286
|
+
* and send your backend's own stored copy as `phrase`. See the dev portal's
|
|
7287
|
+
* "Liveness challenge" guide. The face recorder still calls this for its
|
|
7288
|
+
* deprecated fallback when no challenge is given.
|
|
7281
7289
|
*/
|
|
7282
7290
|
function generateRandomDigits(count) {
|
|
7283
7291
|
const digits = [...CHALLENGE_DIGITS];
|
|
@@ -7286,6 +7294,56 @@ function generateRandomDigits(count) {
|
|
|
7286
7294
|
[digits[i], digits[j]] = [digits[j], digits[i]];
|
|
7287
7295
|
}
|
|
7288
7296
|
return digits.slice(0, count);
|
|
7297
|
+
}
|
|
7298
|
+
/**
|
|
7299
|
+
* Shortest liveness challenge accepted from your backend.
|
|
7300
|
+
*
|
|
7301
|
+
* The voice engine's quality check needs at least 7 recognised words, so a
|
|
7302
|
+
* shorter phrase can't be used for voice enrollment (voice auto-enrollment
|
|
7303
|
+
* skips it). We recommend issuing 8 distinct digits from 1-9.
|
|
7304
|
+
*/
|
|
7305
|
+
const MIN_CHALLENGE_DIGITS = 7;
|
|
7306
|
+
/**
|
|
7307
|
+
* Longest liveness challenge accepted from your backend. Longer phrases don't
|
|
7308
|
+
* fit a sensible recording window.
|
|
7309
|
+
*/
|
|
7310
|
+
const MAX_CHALLENGE_DIGITS = 12;
|
|
7311
|
+
/**
|
|
7312
|
+
* Validate a challenge issued by your backend and return its digits, in order
|
|
7313
|
+
* and unchanged.
|
|
7314
|
+
*
|
|
7315
|
+
* Returns `null` only when no challenge was given (`null` / `undefined`); the
|
|
7316
|
+
* face recorder then falls back to generating digits on the device, which is
|
|
7317
|
+
* deprecated. Anything else that isn't MIN_CHALLENGE_DIGITS to
|
|
7318
|
+
* MAX_CHALLENGE_DIGITS digits 0–9 throws, rather than being replaced by generated digits: a silently
|
|
7319
|
+
* swapped challenge would have the user speak digits your backend never issued.
|
|
7320
|
+
*
|
|
7321
|
+
* A string must contain only the ASCII digits `0`–`9`, with no spaces or
|
|
7322
|
+
* separators. An array must contain only integers 0–9.
|
|
7323
|
+
*/
|
|
7324
|
+
function parseChallenge(challenge) {
|
|
7325
|
+
if (challenge === null || challenge === undefined)
|
|
7326
|
+
return null;
|
|
7327
|
+
let digits;
|
|
7328
|
+
if (typeof challenge === 'string') {
|
|
7329
|
+
if (!/^[0-9]+$/.test(challenge)) {
|
|
7330
|
+
throw new Error(`Invalid liveness challenge ${JSON.stringify(challenge)}: use only the digits 0-9, with no spaces or separators.`);
|
|
7331
|
+
}
|
|
7332
|
+
digits = Array.from(challenge, c => c.charCodeAt(0) - 48);
|
|
7333
|
+
}
|
|
7334
|
+
else if (Array.isArray(challenge)) {
|
|
7335
|
+
if (!challenge.every(d => Number.isInteger(d) && d >= 0 && d <= 9)) {
|
|
7336
|
+
throw new Error(`Invalid liveness challenge ${JSON.stringify(challenge)}: every element must be an integer from 0 to 9.`);
|
|
7337
|
+
}
|
|
7338
|
+
digits = [...challenge];
|
|
7339
|
+
}
|
|
7340
|
+
else {
|
|
7341
|
+
throw new Error('Invalid liveness challenge: pass a digit string such as "48719352" or an array of digits.');
|
|
7342
|
+
}
|
|
7343
|
+
if (digits.length < MIN_CHALLENGE_DIGITS || digits.length > MAX_CHALLENGE_DIGITS) {
|
|
7344
|
+
throw new Error(`Invalid liveness challenge ${JSON.stringify(challenge)}: it has ${digits.length} digits, ${MIN_CHALLENGE_DIGITS} to ${MAX_CHALLENGE_DIGITS} are required.`);
|
|
7345
|
+
}
|
|
7346
|
+
return digits;
|
|
7289
7347
|
}function getSupportedMimeType() {
|
|
7290
7348
|
if (MediaRecorder.isTypeSupported('video/webm;codecs=vp9')) {
|
|
7291
7349
|
return 'video/webm;codecs=vp9';
|
|
@@ -7401,7 +7459,21 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7401
7459
|
}
|
|
7402
7460
|
};
|
|
7403
7461
|
return { isRecording, startRecording, stopRecording, cancelRecording };
|
|
7404
|
-
}
|
|
7462
|
+
};// Video constraints are IDEAL-ONLY - never `min` (BM-530).
|
|
7463
|
+
//
|
|
7464
|
+
// Without a deviceId, getUserMedia considers every camera and returns one that
|
|
7465
|
+
// satisfies all required (min/max/exact) constraints. A hard `min` therefore
|
|
7466
|
+
// filters cameras: with `width.min: 1280` a 640x480 laptop webcam is silently
|
|
7467
|
+
// excluded and the browser falls back to whatever passes - typically a virtual
|
|
7468
|
+
// camera (OBS) that accepts any resolution - or, with no such camera, throws
|
|
7469
|
+
// OverconstrainedError, which used to surface as "Camera Permission Required".
|
|
7470
|
+
// Chrome's camera picker for the site is narrowed the same way.
|
|
7471
|
+
//
|
|
7472
|
+
// The old minimums survive as floors on the ideal (`idealSize`): an HD camera
|
|
7473
|
+
// is still steered to at least the same resolution as before, while a weaker
|
|
7474
|
+
// camera is accepted at its best mode instead of being rejected.
|
|
7475
|
+
const idealSize = (requested, floor, cap) => Math.max(floor, Math.min(requested, cap));
|
|
7476
|
+
const usePermissions = ({ rectWidth = 1920, rectHeight = 1080 } = {}) => {
|
|
7405
7477
|
const [permission, setPermission] = React.useState(false);
|
|
7406
7478
|
const [stream, setStream] = React.useState(null);
|
|
7407
7479
|
const [isLoading, setIsLoading] = React.useState(true);
|
|
@@ -7434,9 +7506,9 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7434
7506
|
channelCount: 1 // Mono: the gateway downmixes with -ac 1 and the engine expects mono (BM-446)
|
|
7435
7507
|
},
|
|
7436
7508
|
video: {
|
|
7437
|
-
width: { ideal:
|
|
7438
|
-
height: { ideal:
|
|
7439
|
-
frameRate: { ideal: 30
|
|
7509
|
+
width: { ideal: idealSize(rectWidth, 1280, 1920) },
|
|
7510
|
+
height: { ideal: idealSize(rectHeight, 720, 1080) },
|
|
7511
|
+
frameRate: { ideal: 30 },
|
|
7440
7512
|
facingMode: 'user',
|
|
7441
7513
|
aspectRatio: { ideal: rectWidth / rectHeight }
|
|
7442
7514
|
}
|
|
@@ -7453,9 +7525,9 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7453
7525
|
channelCount: 1
|
|
7454
7526
|
},
|
|
7455
7527
|
video: {
|
|
7456
|
-
width: { ideal:
|
|
7457
|
-
height: { ideal:
|
|
7458
|
-
frameRate: { ideal: 24
|
|
7528
|
+
width: { ideal: idealSize(rectWidth, 640, 1280) },
|
|
7529
|
+
height: { ideal: idealSize(rectHeight, 480, 720) },
|
|
7530
|
+
frameRate: { ideal: 24 },
|
|
7459
7531
|
facingMode: 'user',
|
|
7460
7532
|
aspectRatio: { ideal: rectWidth / rectHeight }
|
|
7461
7533
|
}
|
|
@@ -7471,15 +7543,16 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7471
7543
|
channelCount: 1
|
|
7472
7544
|
},
|
|
7473
7545
|
video: {
|
|
7474
|
-
width: { ideal:
|
|
7475
|
-
height: { ideal:
|
|
7476
|
-
frameRate: { ideal: 20
|
|
7546
|
+
width: { ideal: idealSize(rectWidth, 640, 1280) },
|
|
7547
|
+
height: { ideal: idealSize(rectHeight, 480, 720) },
|
|
7548
|
+
frameRate: { ideal: 20 },
|
|
7477
7549
|
facingMode: 'user',
|
|
7478
7550
|
aspectRatio: { ideal: rectWidth / rectHeight }
|
|
7479
7551
|
}
|
|
7480
7552
|
};
|
|
7481
7553
|
};
|
|
7482
7554
|
const requestPermissions = async () => {
|
|
7555
|
+
var _a, _b;
|
|
7483
7556
|
try {
|
|
7484
7557
|
setIsLoading(true);
|
|
7485
7558
|
const constraints = getAdaptiveConstraints();
|
|
@@ -7496,7 +7569,10 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7496
7569
|
setStream(streamData);
|
|
7497
7570
|
}
|
|
7498
7571
|
catch (err) {
|
|
7499
|
-
|
|
7572
|
+
// Not every failure is a denied permission: OverconstrainedError names
|
|
7573
|
+
// the failing rule, NotReadableError means the camera is busy.
|
|
7574
|
+
const e = err;
|
|
7575
|
+
console.error('biometry-react-components: getUserMedia failed:', e === null || e === void 0 ? void 0 : e.name, (_a = e === null || e === void 0 ? void 0 : e.constraint) !== null && _a !== void 0 ? _a : '', (_b = e === null || e === void 0 ? void 0 : e.message) !== null && _b !== void 0 ? _b : '');
|
|
7500
7576
|
if (isMountedRef.current)
|
|
7501
7577
|
setPermission(false);
|
|
7502
7578
|
}
|
|
@@ -7538,11 +7614,41 @@ if (typeof window !== 'undefined' && typeof console !== 'undefined') {
|
|
|
7538
7614
|
const DEFAULT_RECORD_SECONDS = VIDEO_LENGTH_MS / 1000;
|
|
7539
7615
|
const DEFAULT_PREP_SECONDS = 3;
|
|
7540
7616
|
const DIGITS_NUMBER$1 = 7;
|
|
7617
|
+
/** Printed once per FaceRecorder instance when no challenge is given. */
|
|
7618
|
+
const DEVICE_CHALLENGE_WARNING = 'biometry-react-components FaceRecorder: no challengeDigits given, so the challenge was generated in the browser. ' +
|
|
7619
|
+
'This is deprecated: device-generated challenges give no replay protection. ' +
|
|
7620
|
+
'Pass a challenge issued by your backend as challengeDigits. ' +
|
|
7621
|
+
'See https://developer.biometrysolutions.com/concepts/liveness-challenge/';
|
|
7622
|
+
/**
|
|
7623
|
+
* A stable memo key for a challenge: the string itself, or the JSON of an
|
|
7624
|
+
* array (prefixed so a string and an array never share a key).
|
|
7625
|
+
*/
|
|
7626
|
+
const challengeKeyOf = (challenge) => {
|
|
7627
|
+
if (challenge === null || challenge === undefined)
|
|
7628
|
+
return null;
|
|
7629
|
+
if (typeof challenge === 'string')
|
|
7630
|
+
return `s:${challenge}`;
|
|
7631
|
+
try {
|
|
7632
|
+
return `a:${JSON.stringify(challenge)}`;
|
|
7633
|
+
}
|
|
7634
|
+
catch (_a) {
|
|
7635
|
+
// Not serialisable, so certainly invalid: let parseChallenge reject it.
|
|
7636
|
+
return `x:${String(challenge)}`;
|
|
7637
|
+
}
|
|
7638
|
+
};
|
|
7541
7639
|
const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, recordingSeconds = DEFAULT_RECORD_SECONDS, countdownSeconds = DEFAULT_PREP_SECONDS, emitAudio = false, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
|
|
7542
7640
|
const [state, setState] = React.useState('preparation');
|
|
7543
7641
|
const [prepTimer, setPrepTimer] = React.useState(countdownSeconds);
|
|
7544
7642
|
const [recordTimer, setRecordTimer] = React.useState(recordingSeconds);
|
|
7545
|
-
|
|
7643
|
+
// Keyed on the challenge's value, not its identity, so an inline array
|
|
7644
|
+
// literal (a new array every parent render) doesn't re-run the effects below.
|
|
7645
|
+
const challengeKey = challengeKeyOf(challengeDigits);
|
|
7646
|
+
// Validated whenever the value changes, so an invalid challenge throws here,
|
|
7647
|
+
// visibly, instead of quietly falling back to generated digits.
|
|
7648
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7649
|
+
const issuedDigits = React.useMemo(() => parseChallenge(challengeDigits), [challengeKey]);
|
|
7650
|
+
const warnedDeviceChallengeRef = React.useRef(false);
|
|
7651
|
+
const [digits, setDigits] = React.useState(() => issuedDigits !== null && issuedDigits !== void 0 ? issuedDigits : generateRandomDigits(DIGITS_NUMBER$1));
|
|
7546
7652
|
const { permission, stream, stopStreamTracks, requestPermissions, isLoading } = usePermissions({ rectWidth, rectHeight });
|
|
7547
7653
|
const [videoBlob, setVideoBlob] = React.useState(null);
|
|
7548
7654
|
const [audioBlob, setAudioBlob] = React.useState(null);
|
|
@@ -7655,19 +7761,19 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
|
|
|
7655
7761
|
}
|
|
7656
7762
|
}, [state, recordTimer]);
|
|
7657
7763
|
/**
|
|
7658
|
-
*
|
|
7659
|
-
* one place so every reset path applies
|
|
7764
|
+
* Use the challenge your backend issued; without one, fall back to the
|
|
7765
|
+
* deprecated generated digits. Kept in one place so every reset path applies
|
|
7766
|
+
* the same rule.
|
|
7660
7767
|
*/
|
|
7661
7768
|
const applyChallenge = () => {
|
|
7662
|
-
|
|
7663
|
-
const issued = (_a = challengeDigits === null || challengeDigits === void 0 ? void 0 : challengeDigits.filter(d => Number.isInteger(d) && d >= 0 && d <= 9)) !== null && _a !== void 0 ? _a : [];
|
|
7664
|
-
setDigits(issued.length > 0 ? issued : generateRandomDigits(DIGITS_NUMBER$1));
|
|
7769
|
+
setDigits(issuedDigits ? [...issuedDigits] : generateRandomDigits(DIGITS_NUMBER$1));
|
|
7665
7770
|
};
|
|
7666
7771
|
/** Build the emitted payload; `audio` is omitted unless a blob exists. */
|
|
7667
7772
|
const buildRecording = (video, audio, challenge) => ({
|
|
7668
7773
|
file: new File([video], 'face-video.webm', { type: video.type }),
|
|
7669
7774
|
...(audio ? { audio: new File([audio], 'face-audio.webm', { type: audio.type }) } : {}),
|
|
7670
7775
|
phrase: numbersToPhrase(challenge),
|
|
7776
|
+
challenge: challenge.join(''),
|
|
7671
7777
|
});
|
|
7672
7778
|
/** Return to the start after a capture that produced no usable video. */
|
|
7673
7779
|
const resetAfterFailedCapture = () => {
|
|
@@ -7682,10 +7788,14 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
|
|
|
7682
7788
|
};
|
|
7683
7789
|
// Adopt a challenge that arrives (or changes) while idle; never mid-capture.
|
|
7684
7790
|
React.useEffect(() => {
|
|
7791
|
+
if (issuedDigits === null && !warnedDeviceChallengeRef.current) {
|
|
7792
|
+
warnedDeviceChallengeRef.current = true;
|
|
7793
|
+
console.warn(DEVICE_CHALLENGE_WARNING);
|
|
7794
|
+
}
|
|
7685
7795
|
if (state === 'preparation')
|
|
7686
7796
|
applyChallenge();
|
|
7687
7797
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7688
|
-
}, [
|
|
7798
|
+
}, [issuedDigits]);
|
|
7689
7799
|
const startRecording = async () => {
|
|
7690
7800
|
applyChallenge();
|
|
7691
7801
|
setRecordTimer(recordingSeconds);
|
|
@@ -7974,7 +8084,7 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
|
|
|
7974
8084
|
background: 'rgba(0,0,0,0.4)',
|
|
7975
8085
|
zIndex: 1,
|
|
7976
8086
|
borderRadius: noShadow ? 0 : 8,
|
|
7977
|
-
} }), jsxRuntime.jsxs("div", { className: "facerecorder-oval-area", style: ovalArea, children: [jsxRuntime.jsx("span", { className: "facerecorder-guidance-text", style: guidanceText, children: "Place your face in the oval" }), jsxRuntime.jsx("div", { className: "facerecorder-oval-dashed", style: ovalDashed })] }), state === 'countdown' && (jsxRuntime.jsx("div", { style: prepTimerStyle, children: prepTimer })), state === 'recording' && (jsxRuntime.jsx("div", { style: timerStyle, children: recordTimer })), (state === 'recording' || state === 'preparation') && (jsxRuntime.jsxs("div", { style: soundwaveStyle, children: [jsxRuntime.jsx("div", { style: { ...barBase, height: `${8 + micLevel * 30}px` } }), jsxRuntime.jsx("div", { style: { ...barBase, height: `${4 + micLevel * 40}px` } }), jsxRuntime.jsx("div", { style: { ...barBase, height: `${10 + micLevel * 20}px` } })] }))] }), jsxRuntime.jsx("div", { style: numberRow, children: (state === 'recording' ? digits : Array(
|
|
8087
|
+
} }), jsxRuntime.jsxs("div", { className: "facerecorder-oval-area", style: ovalArea, children: [jsxRuntime.jsx("span", { className: "facerecorder-guidance-text", style: guidanceText, children: "Place your face in the oval" }), jsxRuntime.jsx("div", { className: "facerecorder-oval-dashed", style: ovalDashed })] }), state === 'countdown' && (jsxRuntime.jsx("div", { style: prepTimerStyle, children: prepTimer })), state === 'recording' && (jsxRuntime.jsx("div", { style: timerStyle, children: recordTimer })), (state === 'recording' || state === 'preparation') && (jsxRuntime.jsxs("div", { style: soundwaveStyle, children: [jsxRuntime.jsx("div", { style: { ...barBase, height: `${8 + micLevel * 30}px` } }), jsxRuntime.jsx("div", { style: { ...barBase, height: `${4 + micLevel * 40}px` } }), jsxRuntime.jsx("div", { style: { ...barBase, height: `${10 + micLevel * 20}px` } })] }))] }), jsxRuntime.jsx("div", { style: numberRow, children: (state === 'recording' ? digits : Array(digits.length).fill('*')).map((num, i) => (jsxRuntime.jsx("div", { style: numberBox, children: num }, i))) }), state === 'preparation' && (jsxRuntime.jsx("button", { type: "button", style: actionButton, onClick: startRecording, children: "Start recording" })), state === 'recording' && (jsxRuntime.jsx("button", { type: "button", style: { ...actionButton, background: '#d32f2f' }, onClick: handleCancel, children: "Cancel" }))] })), state === 'processing' && (jsxRuntime.jsx(jsxRuntime.Fragment, { children: jsxRuntime.jsx("div", { className: "facerecorder-processing", style: preview, children: jsxRuntime.jsxs("div", { style: {
|
|
7978
8088
|
display: 'flex',
|
|
7979
8089
|
flexDirection: 'column',
|
|
7980
8090
|
alignItems: 'center',
|
|
@@ -8495,4 +8605,4 @@ const VoiceRecorder = ({ rectWidth = 360, rectHeight, takes = DEFAULT_TAKES, onC
|
|
|
8495
8605
|
maxWidth: '300px',
|
|
8496
8606
|
marginBottom: '16px',
|
|
8497
8607
|
} })), jsxRuntime.jsx("div", { className: "voicerecorder-confirm-text", style: confirmText, children: "Do you want to use this recording?" }), jsxRuntime.jsxs("div", { className: "voicerecorder-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 12, width: '100%' }, children: [jsxRuntime.jsx("button", { type: "button", "aria-label": "Retake", className: "voicerecorder-icon-btn", style: iconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxRuntime.jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsxRuntime.jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsxRuntime.jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsxRuntime.jsx("button", { type: "button", "aria-label": "Confirm", className: "voicerecorder-icon-btn", style: iconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxRuntime.jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsxRuntime.jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] }) }))] }));
|
|
8498
|
-
};exports.CHALLENGE_DIGITS=CHALLENGE_DIGITS;exports.DocScan=DocScan;exports.ENROLMENT_DIGIT_SEQUENCES=ENROLMENT_DIGIT_SEQUENCES;exports.FaceCapture=FaceCapture;exports.FaceRecorder=FaceRecorder;exports.VoiceRecorder=VoiceRecorder;exports.generateRandomDigits=generateRandomDigits;exports.numbersToPhrase=numbersToPhrase;exports.useAudioPermissions=useAudioPermissions;exports.usePermissions=usePermissions;exports.useRecorder=useRecorder;
|
|
8608
|
+
};exports.CHALLENGE_DIGITS=CHALLENGE_DIGITS;exports.DocScan=DocScan;exports.ENROLMENT_DIGIT_SEQUENCES=ENROLMENT_DIGIT_SEQUENCES;exports.FaceCapture=FaceCapture;exports.FaceRecorder=FaceRecorder;exports.MAX_CHALLENGE_DIGITS=MAX_CHALLENGE_DIGITS;exports.MIN_CHALLENGE_DIGITS=MIN_CHALLENGE_DIGITS;exports.VoiceRecorder=VoiceRecorder;exports.generateRandomDigits=generateRandomDigits;exports.numbersToPhrase=numbersToPhrase;exports.parseChallenge=parseChallenge;exports.useAudioPermissions=useAudioPermissions;exports.usePermissions=usePermissions;exports.useRecorder=useRecorder;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "biometry-react-components",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "React UI component library for capturing high-quality biometric data (voice, face, documents, video) to facilitate integration with 'Biometry' services",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"build": "rollup -c",
|
|
14
14
|
"build:types": "rollup -c rollup.config.dts.js",
|
|
15
15
|
"build:clean": "rm -rf dist && npm run build && npm run build:types",
|
|
16
|
-
"test": "
|
|
16
|
+
"test": "vitest run",
|
|
17
17
|
"prepublishOnly": "npm run build:clean"
|
|
18
18
|
},
|
|
19
19
|
"keywords": [
|
|
@@ -42,15 +42,19 @@
|
|
|
42
42
|
"@rollup/plugin-commonjs": "^28.0.6",
|
|
43
43
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
44
44
|
"@rollup/plugin-typescript": "^12.1.4",
|
|
45
|
+
"@testing-library/dom": "^10.4.2",
|
|
46
|
+
"@testing-library/react": "^16.3.3",
|
|
45
47
|
"@types/react": "^19.1.8",
|
|
46
48
|
"@types/react-dom": "^19.1.6",
|
|
47
49
|
"@types/recordrtc": "^5.6.14",
|
|
50
|
+
"jsdom": "^26.1.0",
|
|
48
51
|
"react": "^19.1.0",
|
|
49
52
|
"react-dom": "^19.1.0",
|
|
50
53
|
"rollup": "^4.45.1",
|
|
51
54
|
"rollup-plugin-dts": "^6.2.1",
|
|
52
55
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
53
|
-
"typescript": "^5.8.3"
|
|
56
|
+
"typescript": "^5.8.3",
|
|
57
|
+
"vitest": "^3.2.7"
|
|
54
58
|
},
|
|
55
59
|
"peerDependencies": {
|
|
56
60
|
"react": ">=16.8.0",
|