biometry-react-components 2.0.1 → 3.0.1

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 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
- const handleConfirm = ({ file, phrase }: FaceRecording) => {
99
- // Upload the video. The voice is extracted server-side from the same file
100
- // that passed the liveness check, so there is no separate audio to send.
101
- // const response = await sdk.liveness(file, 'user-123', phrase);
102
- // or, to verify against an enrolled face:
103
- // const response = await sdk.faceVerify(file, 'user-123', phrase);
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` | Server-issued challenge digits (each `0–9`) the user must read, so the backend can verify the spoken digits against what it issued. Invalid or empty values fall back to a generated challenge. Applied only while idle. |
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. |
@@ -160,9 +172,19 @@ import { VoiceRecorder, type VoiceEnrollment } from 'biometry-react-components';
160
172
  function Enroll() {
161
173
  // Fires once, after the last take is confirmed.
162
174
  const handleConfirm = async ({ files, phrases }: VoiceEnrollment) => {
163
- // Post every take in one enrollment request: repeated `voice` parts, one
164
- // `phrase` part each. A partial set is worse than none.
165
- // await sdk.enrollVoice(files, phrases, 'user-123');
175
+ // Send every take to your backend; a partial set is worse than none.
176
+ // phrases[i] was spoken in files[i].
177
+ const form = new FormData();
178
+ files.forEach((file) => form.append('voice', file));
179
+ form.append('phrases', phrases.join(','));
180
+ // await fetch('/your-backend/enroll-voice', { method: 'POST', body: form });
181
+ //
182
+ // Your backend makes ONE call to POST /v2/enrollments/voice with its own
183
+ // token: the takes as repeated `voice` parts, plus a `request` part such as
184
+ // {"user_id": "<the signed-in user, from your session>",
185
+ // "phrase": "<the phrases joined by commas, in file order>"}.
186
+ // Never take user_id from the browser, or anyone can enroll their voice
187
+ // under another user's id.
166
188
  };
167
189
 
168
190
  return <VoiceRecorder onConfirmRecording={handleConfirm} />;
@@ -313,6 +335,13 @@ function AudioPermissionManager() {
313
335
  | `requestPermissions` | `() => Promise<void>` | Request microphone permissions |
314
336
  | `isLoading` | `boolean` | Loading state during permission request |
315
337
 
338
+ ## Development
339
+
340
+ ```bash
341
+ npm test # unit and component tests (vitest + jsdom), Node 18 or newer
342
+ npm run build:clean # dist/ with types
343
+ ```
344
+
316
345
  ## License
317
346
 
318
347
  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
- * Server-issued liveness challenge digits the user must read aloud (each 0–9).
42
- * When provided these are shown and emitted instead of a generated challenge,
43
- * so the backend can verify the spoken digits against what it issued. Omitted
44
- * or invalid values fall back to a generated challenge.
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?: number[] | null;
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;
@@ -172,8 +238,9 @@ declare const useRecorder: (stream: MediaStream | null, type: "audio" | "video",
172
238
  interface UsePermissionsProps {
173
239
  rectWidth?: number;
174
240
  rectHeight?: number;
241
+ audio?: boolean;
175
242
  }
176
- declare const usePermissions: ({ rectWidth, rectHeight }?: UsePermissionsProps) => {
243
+ declare const usePermissions: ({ rectWidth, rectHeight, audio }?: UsePermissionsProps) => {
177
244
  permission: boolean;
178
245
  stream: MediaStream | null;
179
246
  stopStreamTracks: () => void;
@@ -189,5 +256,5 @@ declare const useAudioPermissions: () => {
189
256
  isLoading: boolean;
190
257
  };
191
258
 
192
- export { CHALLENGE_DIGITS, DocScan, ENROLMENT_DIGIT_SEQUENCES, FaceCapture, FaceRecorder, VoiceRecorder, generateRandomDigits, numbersToPhrase, useAudioPermissions, usePermissions, useRecorder };
193
- export type { FaceRecorderProps, FaceRecording, VoiceEnrollment, VoiceRecorderProps, VoiceTake };
259
+ export { CHALLENGE_DIGITS, DocScan, ENROLMENT_DIGIT_SEQUENCES, FaceCapture, FaceRecorder, MAX_CHALLENGE_DIGITS, MIN_CHALLENGE_DIGITS, VoiceRecorder, generateRandomDigits, numbersToPhrase, parseChallenge, useAudioPermissions, usePermissions, useRecorder };
260
+ export type { ChallengeInput, FaceRecorderProps, FaceRecording, VoiceEnrollment, VoiceRecorderProps, VoiceTake };