@octane-xplat/biometrics 0.14.0 → 0.15.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 CHANGED
@@ -6,9 +6,9 @@ pnpm add @octane-xplat/biometrics
6
6
 
7
7
  Local biometric verification — Face ID / Touch ID / fingerprint or device
8
8
  credential — behind one capability object. iOS and Android run
9
- `@nativescript/biometrics`; web and the macOS AppKit host report
10
- `supported: false` (WebAuthn is an authentication ceremony, not a
11
- local-presence check, and the dev host has no LocalAuthentication bridge).
9
+ `@nativescript/biometrics`; macOS AppKit uses Apple LocalAuthentication
10
+ for Touch ID. Web reports `supported: false` because WebAuthn requires
11
+ a registered credential and an authentication ceremony.
12
12
 
13
13
  ```ts
14
14
  import { biometrics } from '@octane-xplat/biometrics'
@@ -24,7 +24,24 @@ implementation exists on this target — branch on it rather than catching —
24
24
  the service once usable. `verify(reason)` resolves `false` for a declined
25
25
  or failed prompt rather than throwing.
26
26
 
27
+ On macOS, availability checks do not open a prompt. Missing hardware,
28
+ enrollment, pairing, or a connected sensor returns `unsupported`; lockout
29
+ or an interaction restriction returns `denied`. A host without the Objective-C
30
+ LocalAuthentication bridge reports `supported: false` and `impl: null`.
31
+ Verification uses biometrics only, hides password fallback, and returns
32
+ `false` on cancellation, failure, or an overlapping prompt.
33
+
34
+ ```ts
35
+ import { biometrics } from '@octane-xplat/biometrics'
36
+
37
+ const availability = await biometrics.ensure()
38
+ if (availability === 'granted') {
39
+ const verified = await biometrics.impl!.verify('Unlock your saved trips')
40
+ console.log(verified ? 'Unlocked' : 'Still locked')
41
+ }
42
+ ```
43
+
27
44
  Guide: [Using device features](../../docs/platform/platform-services.md);
28
- per-target availability: [platform notes](../../docs/notes/platform-notes.md).
45
+ per-target availability: [platform notes](../../.agents/docs/notes/platform-notes.md).
29
46
  Exercised by the harness `Services` screen
30
47
  ([`packages/app/src/Services.tsrx`](../app/src/Services.tsrx)).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octane-xplat/biometrics",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Biometric verification (FaceID/TouchID/fingerprint) for Octane xplat — matching web + NativeScript implementations",
5
5
  "keywords": [
6
6
  "android",
@@ -23,15 +23,29 @@
23
23
  "directory": "packages/biometrics"
24
24
  },
25
25
  "files": [
26
- "src"
26
+ "src",
27
+ "types",
28
+ "!src/**/*.test.*",
29
+ "!src/**/*.spec.*"
27
30
  ],
28
31
  "type": "module",
29
32
  "exports": {
30
33
  ".": {
31
- "macos": "./src/index.macos.ts",
34
+ "macos": {
35
+ "types": "./types/generated/index.macos.d.ts",
36
+ "default": "./src/index.macos.ts"
37
+ },
38
+ "types": "./types/generated/index.d.ts",
32
39
  "default": "./src/index.ts"
33
40
  },
34
- "./*": "./src/*"
41
+ "./*": {
42
+ "types": "./types/generated/*.d.ts",
43
+ "default": "./src/*"
44
+ },
45
+ "./*.ts": {
46
+ "types": "./types/generated/*.d.ts",
47
+ "default": "./src/*.ts"
48
+ }
35
49
  },
36
50
  "publishConfig": {
37
51
  "access": "public"
@@ -40,9 +54,13 @@
40
54
  "@nativescript/biometrics": "1.3.1"
41
55
  },
42
56
  "devDependencies": {
43
- "@nativescript-community/octane": "0.2.4",
57
+ "@nativescript-community/octane": "0.2.5",
44
58
  "@nativescript/core": "9.1.3",
45
- "octane": "0.11.0"
59
+ "@nativescript/types": "9.1.4",
60
+ "@tsrx/typescript-plugin": "0.4.11",
61
+ "octane": "0.11.0",
62
+ "tsrx-typegen": "0.3.0",
63
+ "typescript": "5.9.3"
46
64
  },
47
65
  "peerDependencies": {
48
66
  "@nativescript-community/octane": ">=0.2.1 <1",
@@ -64,6 +82,10 @@
64
82
  }
65
83
  },
66
84
  "scripts": {
67
- "test:packed": "node tests/packed-consumer.mjs"
85
+ "test:packed": "node tests/packed-consumer.mjs",
86
+ "test": "node --test tests/*.test.mjs",
87
+ "test:macos-runtime": "node ../../apps/macos/test/verify-biometrics.mjs",
88
+ "typegen": "tsrx-typegen --project tsconfig.types.json",
89
+ "pack:check": "tsrx-typegen --pack-check"
68
90
  }
69
91
  }
@@ -1,9 +1,140 @@
1
- // Biometrics — AppKit host leaf. No LocalAuthentication bridge exists on the
2
- // dev host.
3
- import type { Capability, BiometricsImpl } from './types'
1
+ // LocalAuthentication through the AppKit host's existing Objective-C bridge.
2
+ import type { BiometricsImpl, Capability } from './types'
3
+
4
+ declare const objc: { import(name: string): void }
5
+ declare const interop: { Reference: new () => { value?: { domain: string; code: number } } }
6
+ declare const LAContext: { new: () => AuthenticationContext }
7
+
8
+ interface AuthenticationContext {
9
+ localizedFallbackTitle: string
10
+ canEvaluatePolicyError(policy: number, error: InstanceType<typeof interop.Reference>): boolean
11
+ evaluatePolicyLocalizedReasonReply(
12
+ policy: number,
13
+ reason: string,
14
+ reply: (success: boolean, error: unknown) => void,
15
+ ): void
16
+ invalidate(): void
17
+ }
18
+
19
+ // LAPolicyDeviceOwnerAuthenticationWithBiometrics: do not silently substitute
20
+ // password/Apple Watch authentication on a machine without biometric hardware.
21
+ const biometricPolicy = 1
22
+
23
+ function frameworkAvailable(): boolean {
24
+ try {
25
+ objc.import('LocalAuthentication')
26
+ } catch {
27
+ // Some hosts have already bound the symbols; others cannot provide them.
28
+ }
29
+
30
+ return (
31
+ typeof LAContext !== 'undefined' &&
32
+ typeof interop !== 'undefined' &&
33
+ typeof interop.Reference !== 'undefined'
34
+ )
35
+ }
36
+
37
+ function availability(context: AuthenticationContext): 'granted' | 'denied' | 'unsupported' {
38
+ const error = new interop.Reference()
39
+ if (context.canEvaluatePolicyError(biometricPolicy, error)) {
40
+ return 'granted'
41
+ }
42
+
43
+ // Missing hardware (-6), enrollment (-7), pairing (-12) or a connected
44
+ // sensor (-13) is absence, not a permission denial.
45
+ // Lockout (-8), interaction restrictions and other failures remain denied.
46
+ return error.value?.domain === 'com.apple.LocalAuthentication' &&
47
+ [-6, -7, -12, -13].includes(Number(error.value.code))
48
+ ? 'unsupported'
49
+ : 'denied'
50
+ }
51
+
52
+ function invalidate(context: AuthenticationContext | null): void {
53
+ try {
54
+ context?.invalidate()
55
+ } catch {
56
+ // Cleanup must not turn a failed authentication into a rejected promise.
57
+ }
58
+ }
59
+
60
+ // Root both the context and callback until completion; native callbacks may
61
+ // outlive the JS call. Refuse concurrent prompts rather than canceling one.
62
+ let active: {
63
+ context: AuthenticationContext | null
64
+ reply: ((success: boolean, error: unknown) => void) | null
65
+ } | null = null
66
+
67
+ const implementation: BiometricsImpl = {
68
+ verify(reason) {
69
+ if (active || !frameworkAvailable() || typeof reason !== 'string' || !reason.trim()) {
70
+ return Promise.resolve(false)
71
+ }
72
+
73
+ const operation = {
74
+ context: null as AuthenticationContext | null,
75
+ reply: null as ((success: boolean, error: unknown) => void) | null,
76
+ }
77
+
78
+ active = operation
79
+ return new Promise<boolean>((resolve) => {
80
+ let settled = false
81
+ const finish = (success: boolean) => {
82
+ if (settled) {
83
+ return
84
+ }
85
+
86
+ settled = true
87
+ if (active === operation) {
88
+ active = null
89
+ }
90
+
91
+ invalidate(operation.context)
92
+ operation.context = null
93
+ operation.reply = null
94
+ resolve(success)
95
+ }
96
+
97
+ try {
98
+ operation.context = LAContext.new()
99
+ if (availability(operation.context) !== 'granted') {
100
+ finish(false)
101
+ return
102
+ }
103
+
104
+ operation.context.localizedFallbackTitle = ''
105
+ operation.reply = (success, error) => finish(Boolean(success) && !error)
106
+ operation.context.evaluatePolicyLocalizedReasonReply(
107
+ biometricPolicy,
108
+ reason,
109
+ operation.reply,
110
+ )
111
+ } catch {
112
+ finish(false)
113
+ }
114
+ })
115
+ },
116
+ }
4
117
 
5
118
  export const biometrics: Capability<BiometricsImpl> = {
6
- supported: false,
7
- ensure: async () => 'unsupported',
8
- impl: null,
119
+ get supported() {
120
+ return frameworkAvailable()
121
+ },
122
+ async ensure() {
123
+ if (!frameworkAvailable()) {
124
+ return 'unsupported'
125
+ }
126
+
127
+ let context: AuthenticationContext | null = null
128
+ try {
129
+ context = LAContext.new()
130
+ return availability(context)
131
+ } catch {
132
+ return 'denied'
133
+ } finally {
134
+ invalidate(context)
135
+ }
136
+ },
137
+ get impl() {
138
+ return frameworkAvailable() ? implementation : null
139
+ },
9
140
  }
package/src/biometrics.ts CHANGED
@@ -1,23 +1,53 @@
1
1
  // Biometrics — FaceID/TouchID/fingerprint via @nativescript/biometrics.
2
2
  import { BiometricAuth } from '@nativescript/biometrics'
3
+ import type { VerifyBiometricOptions } from '@nativescript/biometrics'
4
+ import { isIOS } from '@nativescript/core'
3
5
  import type { Capability } from './types'
4
6
  import type { BiometricsImpl } from './types'
5
7
 
6
8
  const bio = new BiometricAuth()
7
9
 
10
+ // One prompt at a time — matching the macOS leaf, an overlapping call
11
+ // resolves false rather than stacking prompts or racing the shared
12
+ // keychain entry on iOS.
13
+ let verifying = false
14
+
8
15
  export const biometrics: Capability<BiometricsImpl> = {
9
16
  supported: true,
10
17
  async ensure() {
11
- const r = await bio.available()
12
- return r.any ? 'granted' : 'unsupported'
18
+ try {
19
+ const r = await bio.available()
20
+ // Android reports `any` for a secure lockscreen without biometrics —
21
+ // verify() requires BIOMETRIC_STRONG, so that state is not usable.
22
+ return r.any && (isIOS || r.biometrics) ? 'granted' : 'unsupported'
23
+ } catch {
24
+ // Android rejects when the device lacks biometric hardware,
25
+ // enrollment, or a secure lockscreen — absence, not an error.
26
+ return 'unsupported'
27
+ }
13
28
  },
14
29
  impl: {
15
30
  async verify(reason) {
31
+ if (verifying) {
32
+ return false
33
+ }
34
+
35
+ verifying = true
16
36
  try {
17
- const r = await bio.verifyBiometric({ title: reason })
37
+ // iOS ignores `title` on its default keychain path — route
38
+ // through LAContext (pinFallback selects that path; customFallback
39
+ // keeps the biometrics-only policy) so `message` surfaces the
40
+ // caller's reason, matching Android's dialog title.
41
+ const options: VerifyBiometricOptions = isIOS
42
+ ? { title: reason, message: reason, pinFallback: true, ios: { customFallback: true } }
43
+ : { title: reason }
44
+
45
+ const r = await bio.verifyBiometric(options)
18
46
  return r.code === 0
19
47
  } catch {
20
48
  return false
49
+ } finally {
50
+ verifying = false
21
51
  }
22
52
  },
23
53
  },
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": 1,
3
+ "files": [
4
+ "biometrics.d.ts",
5
+ "biometrics.macos.d.ts",
6
+ "biometrics.web.d.ts",
7
+ "index.d.ts",
8
+ "index.macos.d.ts",
9
+ "types.d.ts"
10
+ ]
11
+ }
@@ -0,0 +1,3 @@
1
+ import type { Capability } from './types.js';
2
+ import type { BiometricsImpl } from './types.js';
3
+ export declare const biometrics: Capability<BiometricsImpl>;
@@ -0,0 +1,2 @@
1
+ import type { BiometricsImpl, Capability } from './types.js';
2
+ export declare const biometrics: Capability<BiometricsImpl>;
@@ -0,0 +1,2 @@
1
+ import type { BiometricsImpl, Capability } from './types.js';
2
+ export declare const biometrics: Capability<BiometricsImpl>;
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export { biometrics } from './biometrics.js';
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export { biometrics } from './biometrics.macos.js';
@@ -0,0 +1,10 @@
1
+ /** Optional capability — never throws for absence (docs/platform/platform-services.md). */
2
+ export interface Capability<T> {
3
+ supported: boolean;
4
+ ensure(): Promise<'granted' | 'denied' | 'unsupported'>;
5
+ /** usable iff supported && ensured */
6
+ impl: T | null;
7
+ }
8
+ export interface BiometricsImpl {
9
+ verify(reason: string): Promise<boolean>;
10
+ }