@stacksjs/components 0.2.913 → 0.2.915
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/dist/BiometricSignIn-6enqcn9e.stx +78 -0
- package/dist/biometric/biometric.js +94 -0
- package/dist/index.js +22 -22
- package/dist/ui/auth/BiometricSignIn.d.ts +37 -0
- package/dist/ui/auth/biometric.d.ts +74 -0
- package/dist/ui/auth/index.d.ts +12 -0
- package/package.json +8 -3
- package/src/ui/auth/BiometricSignIn.stx +78 -0
- package/src/ui/auth/README.md +50 -0
- package/src/ui/auth/biometric.ts +186 -0
- package/src/ui/auth/index.ts +16 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { DefineComponent } from '@stacksjs/stx';
|
|
2
|
+
declare const __dtsx_component__: __DtsxStxComponentType;
|
|
3
|
+
/*
|
|
4
|
+
* <BiometricSignIn>: the "Sign in with Face ID" button on a sign-in screen.
|
|
5
|
+
*
|
|
6
|
+
* <BiometricSignIn reason="Sign in to HQ"
|
|
7
|
+
* @signin="session.exchange($event.token)"
|
|
8
|
+
* @fallback="showPassword()" />
|
|
9
|
+
*
|
|
10
|
+
* It is offered only when this device can release a kept credential: inside
|
|
11
|
+
* Craft, with Face ID or Touch ID enrolled, after the app kept one with
|
|
12
|
+
* `createBiometricSignIn().enable(token)` at its last password sign-in. In a
|
|
13
|
+
* browser or on Android it renders nothing, so the same screen serves every
|
|
14
|
+
* platform.
|
|
15
|
+
*
|
|
16
|
+
* Pressing it shows the system prompt (`reason` is the line under it). It
|
|
17
|
+
* emits:
|
|
18
|
+
* - `signin` with `{ token }` on a match;
|
|
19
|
+
* - `fallback` with `{ reason: 'cancelled' | 'invalidated' }` when the person
|
|
20
|
+
* dismissed the prompt, or nothing came back because another face or
|
|
21
|
+
* finger was enrolled (the credential is forgotten, the button goes away);
|
|
22
|
+
* - `failed` with `{ code, message }` when the face or finger did not match
|
|
23
|
+
* or the host refused. (Not `error`: an emitted event bubbles, and an
|
|
24
|
+
* `error` reaching window would be logged by an error tracker as a crash.)
|
|
25
|
+
*
|
|
26
|
+
* Keep a device token the server can revoke, never the password. The flow
|
|
27
|
+
* lives in ./biometric (`createBiometricSignIn`), which an app's settings
|
|
28
|
+
* screen uses for "Turn on Face ID" and sign-out.
|
|
29
|
+
*/
|
|
30
|
+
export declare const credentialKey: unknown;
|
|
31
|
+
export declare const reason: unknown;
|
|
32
|
+
export declare const variant: unknown;
|
|
33
|
+
export declare const fullWidth: unknown;
|
|
34
|
+
export declare const className: unknown;
|
|
35
|
+
declare const offered: ReturnType<typeof state>;
|
|
36
|
+
declare type __DtsxStxComponentType = DefineComponent<{ credentialKey?: string, reason?: string, variant?: string, fullWidth?: unknown, className?: string }> & { readonly __stxEmits?: ["signin", "fallback", "failed"], readonly __stxExposed?: { refresh: unknown, offered: typeof offered } }
|
|
37
|
+
export default __dtsx_component__;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** What to call the host's kind of biometry: `faceId` is `Face ID`. */
|
|
2
|
+
export declare function biometricLabel(kind: unknown): string;
|
|
3
|
+
/** The button's words: `Sign in with Face ID`. */
|
|
4
|
+
export declare function biometricSignInLabel(kind: unknown): string;
|
|
5
|
+
/** The host's `craft.biometrics`, when this page runs inside Craft and it keeps credentials. */
|
|
6
|
+
export declare function biometricHost(): BiometricCredentialHost | null;
|
|
7
|
+
/**
|
|
8
|
+
* The flow for one credential.
|
|
9
|
+
*
|
|
10
|
+
* `host` defaults to the page's `craft.biometrics`, read on each call, so a
|
|
11
|
+
* bridge installed after this was created is still found.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createBiometricSignIn(key?: string, host?: BiometricCredentialHost | null): BiometricSignIn;
|
|
14
|
+
/** The key a credential is kept under when the app does not name one. */
|
|
15
|
+
export declare const BIOMETRIC_DEFAULT_KEY: 'session';
|
|
16
|
+
/** The part of `craft.biometrics` this flow uses. */
|
|
17
|
+
export declare interface BiometricCredentialHost {
|
|
18
|
+
isAvailable: () => Promise<boolean>
|
|
19
|
+
getBiometricType: () => Promise<string | null>
|
|
20
|
+
storeCredential: (key: string, value: string) => Promise<unknown>
|
|
21
|
+
getCredential: (key: string, reason?: string) => Promise<string | null>
|
|
22
|
+
hasCredential: (key: string) => Promise<boolean>
|
|
23
|
+
deleteCredential: (key: string) => Promise<unknown>
|
|
24
|
+
}
|
|
25
|
+
/** Whether to offer biometric sign-in here, and what to call it. */
|
|
26
|
+
export declare interface BiometricProbe {
|
|
27
|
+
available: boolean
|
|
28
|
+
kind: BiometricKind
|
|
29
|
+
label: string
|
|
30
|
+
saved: boolean
|
|
31
|
+
}
|
|
32
|
+
export declare interface BiometricSignIn {
|
|
33
|
+
probe: () => Promise<BiometricProbe>
|
|
34
|
+
signIn: (reason?: string) => Promise<BiometricSignInResult>
|
|
35
|
+
enable: (token: string) => Promise<boolean>
|
|
36
|
+
forget: () => Promise<void>
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* "Sign in with Face ID", without a template.
|
|
40
|
+
*
|
|
41
|
+
* Craft keeps a credential where only the face or finger enrolled on this
|
|
42
|
+
* device releases it (`craft.biometrics.storeCredential`, Craft 0.0.511): the
|
|
43
|
+
* Keychain itself withholds it until the person passes, so a token read
|
|
44
|
+
* afterwards is not merely guarded by the app's own code. This module is the
|
|
45
|
+
* flow around those calls, so `<BiometricSignIn>` and an app's own settings
|
|
46
|
+
* screen say and do the same thing:
|
|
47
|
+
*
|
|
48
|
+
* - after a password sign-in, `enable(token)` keeps the app's device token;
|
|
49
|
+
* - on the sign-in screen, `probe()` says whether to offer the button and
|
|
50
|
+
* what to call it, and `signIn(reason)` shows the system prompt;
|
|
51
|
+
* - on sign-out, `forget()`.
|
|
52
|
+
*
|
|
53
|
+
* Keep a device token the server can revoke, never the password: the server
|
|
54
|
+
* swaps it for a session and forgets it on sign-out.
|
|
55
|
+
*
|
|
56
|
+
* Away from Craft (a browser, Android today) there is no host and every call
|
|
57
|
+
* answers that nothing is available, so the button is simply not offered.
|
|
58
|
+
*/
|
|
59
|
+
/** What the host reports: `craft.biometrics.getBiometricType()`. */
|
|
60
|
+
export type BiometricKind = 'faceId' | 'touchId' | 'fingerprint' | 'none' | null;
|
|
61
|
+
/**
|
|
62
|
+
* How a sign-in ended.
|
|
63
|
+
*
|
|
64
|
+
* - `signed-in`: the token, to swap for a session.
|
|
65
|
+
* - `cancelled`: the person dismissed the prompt; leave the button.
|
|
66
|
+
* - `invalidated`: nothing came back (another face or finger was enrolled, or
|
|
67
|
+
* it was never kept). It is forgotten, so ask for the password.
|
|
68
|
+
* - `failed`: the face or finger did not match, or the host refused; `code`
|
|
69
|
+
* is the host's (`AUTHENTICATION_FAILED`, `BIOMETRICS_UNAVAILABLE`, ...).
|
|
70
|
+
*/
|
|
71
|
+
export type BiometricSignInResult = | { status: 'signed-in', token: string }
|
|
72
|
+
| { status: 'cancelled' }
|
|
73
|
+
| { status: 'invalidated' }
|
|
74
|
+
| { status: 'failed', code: string, message: string }
|
package/dist/ui/auth/index.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
export declare interface BiometricSignInProps {
|
|
2
|
+
credentialKey?: string
|
|
3
|
+
reason?: string
|
|
4
|
+
variant?: string
|
|
5
|
+
fullWidth?: boolean
|
|
6
|
+
onSignin?: (detail: { token: string }) => void
|
|
7
|
+
onFallback?: (detail: { reason: 'cancelled' | 'invalidated' }) => void
|
|
8
|
+
onFailed?: (detail: { code: string, message: string }) => void
|
|
9
|
+
className?: string
|
|
10
|
+
}
|
|
1
11
|
export declare interface LoginProps {
|
|
2
12
|
showLogo?: boolean
|
|
3
13
|
headingText?: string
|
|
@@ -24,6 +34,8 @@ export declare interface TwoFactorChallengeProps {
|
|
|
24
34
|
onUseRecoveryCode?: () => void
|
|
25
35
|
className?: string
|
|
26
36
|
}
|
|
37
|
+
export { default as BiometricSignIn } from './BiometricSignIn';
|
|
27
38
|
export { default as Login } from './Login';
|
|
28
39
|
export { default as Signup } from './Signup';
|
|
29
40
|
export { default as TwoFactorChallenge } from './TwoFactorChallenge';
|
|
41
|
+
export * from './biometric';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/components",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.915",
|
|
5
5
|
"description": "A modern component library built with STX, featuring headwind utility classes and TypeScript syntax highlighting.",
|
|
6
6
|
"author": "Chris Breuer <chris@stacksjs.org>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -47,6 +47,11 @@
|
|
|
47
47
|
"bun": "./stx-plugin.ts",
|
|
48
48
|
"import": "./stx-plugin.ts"
|
|
49
49
|
},
|
|
50
|
+
"./biometric": {
|
|
51
|
+
"types": "./dist/ui/auth/biometric.d.ts",
|
|
52
|
+
"bun": "./dist/biometric/biometric.js",
|
|
53
|
+
"import": "./dist/biometric/biometric.js"
|
|
54
|
+
},
|
|
50
55
|
"./chat": {
|
|
51
56
|
"types": "./dist/ui/chat/controller.d.ts",
|
|
52
57
|
"bun": "./dist/chat/controller.js",
|
|
@@ -129,8 +134,8 @@
|
|
|
129
134
|
"dependencies": {
|
|
130
135
|
"@iconify-json/f7": "^1.2.2",
|
|
131
136
|
"@stacksjs/ts-css": "^0.3.11",
|
|
132
|
-
"@stacksjs/stx": "0.2.
|
|
133
|
-
"bun-plugin-stx": "0.2.
|
|
137
|
+
"@stacksjs/stx": "0.2.915",
|
|
138
|
+
"bun-plugin-stx": "0.2.915",
|
|
134
139
|
"ts-syntax-highlighter": "^0.2.20",
|
|
135
140
|
"ts-video-player": "^0.1.11"
|
|
136
141
|
},
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
/*
|
|
3
|
+
* <BiometricSignIn>: the "Sign in with Face ID" button on a sign-in screen.
|
|
4
|
+
*
|
|
5
|
+
* <BiometricSignIn reason="Sign in to HQ"
|
|
6
|
+
* @signin="session.exchange($event.token)"
|
|
7
|
+
* @fallback="showPassword()" />
|
|
8
|
+
*
|
|
9
|
+
* It is offered only when this device can release a kept credential: inside
|
|
10
|
+
* Craft, with Face ID or Touch ID enrolled, after the app kept one with
|
|
11
|
+
* `createBiometricSignIn().enable(token)` at its last password sign-in. In a
|
|
12
|
+
* browser or on Android it renders nothing, so the same screen serves every
|
|
13
|
+
* platform.
|
|
14
|
+
*
|
|
15
|
+
* Pressing it shows the system prompt (`reason` is the line under it). It
|
|
16
|
+
* emits:
|
|
17
|
+
* - `signin` with `{ token }` on a match;
|
|
18
|
+
* - `fallback` with `{ reason: 'cancelled' | 'invalidated' }` when the person
|
|
19
|
+
* dismissed the prompt, or nothing came back because another face or
|
|
20
|
+
* finger was enrolled (the credential is forgotten, the button goes away);
|
|
21
|
+
* - `failed` with `{ code, message }` when the face or finger did not match
|
|
22
|
+
* or the host refused. (Not `error`: an emitted event bubbles, and an
|
|
23
|
+
* `error` reaching window would be logged by an error tracker as a crash.)
|
|
24
|
+
*
|
|
25
|
+
* Keep a device token the server can revoke, never the password. The flow
|
|
26
|
+
* lives in ./biometric (`createBiometricSignIn`), which an app's settings
|
|
27
|
+
* screen uses for "Turn on Face ID" and sign-out.
|
|
28
|
+
*/
|
|
29
|
+
export const credentialKey = $props.credentialKey || 'session'
|
|
30
|
+
export const reason = $props.reason || ''
|
|
31
|
+
export const variant = $props.variant || 'secondary'
|
|
32
|
+
export const fullWidth = $bool($props.fullWidth, true)
|
|
33
|
+
export const className = $props.className || ''
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<script client>
|
|
37
|
+
import { biometricSignInLabel, createBiometricSignIn } from './biometric'
|
|
38
|
+
|
|
39
|
+
const emit = defineEmits(['signin', 'fallback', 'failed'])
|
|
40
|
+
const flow = createBiometricSignIn({{ credentialKey }})
|
|
41
|
+
const offered = state(false)
|
|
42
|
+
const label = state('')
|
|
43
|
+
const busy = state(false)
|
|
44
|
+
|
|
45
|
+
async function look() {
|
|
46
|
+
const found = await flow.probe()
|
|
47
|
+
label.set(found.available ? biometricSignInLabel(found.kind) : '')
|
|
48
|
+
offered.set(found.available && found.saved)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function press() {
|
|
52
|
+
if (busy())
|
|
53
|
+
return
|
|
54
|
+
busy.set(true)
|
|
55
|
+
try {
|
|
56
|
+
const result = await flow.signIn({{ reason }})
|
|
57
|
+
if (result.status === 'signed-in')
|
|
58
|
+
emit('signin', { token: result.token })
|
|
59
|
+
else if (result.status === 'failed')
|
|
60
|
+
emit('failed', { code: result.code, message: result.message })
|
|
61
|
+
else
|
|
62
|
+
emit('fallback', { reason: result.status })
|
|
63
|
+
if (result.status === 'invalidated')
|
|
64
|
+
offered.set(false)
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
busy.set(false)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
onMount(look)
|
|
72
|
+
|
|
73
|
+
defineExpose({ refresh: look, offered })
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<div :show="offered" class="{{ className }}" data-biometric-sign-in>
|
|
77
|
+
<Button variant="{{ variant }}" fullWidth="{{ fullWidth }}" :label="label()" :loading="busy()" @click="press()" />
|
|
78
|
+
</div>
|
package/src/ui/auth/README.md
CHANGED
|
@@ -13,6 +13,9 @@ A registration form with name, email, email confirmation, and password fields.
|
|
|
13
13
|
### TwoFactorChallenge
|
|
14
14
|
A 2FA code entry component with auto-focus and recovery code option.
|
|
15
15
|
|
|
16
|
+
### BiometricSignIn
|
|
17
|
+
"Sign in with Face ID" (or Touch ID): a button offered only inside Craft, on a device with Face ID or Touch ID set up, once the app has kept a token. It renders nothing in a browser or on Android.
|
|
18
|
+
|
|
16
19
|
## Installation
|
|
17
20
|
|
|
18
21
|
```bash
|
|
@@ -118,6 +121,53 @@ const handleRecoveryCode = () => {
|
|
|
118
121
|
| `onUseRecoveryCode` | `function` | - | Callback for recovery code |
|
|
119
122
|
| `className` | `string` | `''` | Additional CSS classes |
|
|
120
123
|
|
|
124
|
+
### BiometricSignIn Props
|
|
125
|
+
|
|
126
|
+
| Prop | Type | Default | Description |
|
|
127
|
+
|------|------|---------|-------------|
|
|
128
|
+
| `credentialKey` | `string` | `'session'` | Key the token is kept under |
|
|
129
|
+
| `reason` | `string` | `''` | Line under the system prompt (`Sign in to HQ`) |
|
|
130
|
+
| `variant` | `string` | `'secondary'` | Button variant |
|
|
131
|
+
| `fullWidth` | `boolean` | `true` | Full-width button |
|
|
132
|
+
| `className` | `string` | `''` | Additional CSS classes |
|
|
133
|
+
|
|
134
|
+
Events: `@signin` (`$event.token`), `@fallback` (`$event.reason` is `'cancelled'` or `'invalidated'`), `@failed` (`$event.code`, `$event.message`).
|
|
135
|
+
|
|
136
|
+
## Sign in with Face ID
|
|
137
|
+
|
|
138
|
+
Craft (0.0.511 and later) keeps a credential in the Keychain behind the face or finger enrolled on the device, so the token itself is withheld until the person passes, not just guarded by the app's code. The flow has three moments, all through `createBiometricSignIn` from `@stacksjs/components/biometric` (its own entry: the package root reaches code a page cannot load):
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { createBiometricSignIn } from '@stacksjs/components/biometric'
|
|
142
|
+
|
|
143
|
+
const faceId = createBiometricSignIn('session')
|
|
144
|
+
|
|
145
|
+
// 1. After a password sign-in: offer "Turn on Face ID", then keep the token.
|
|
146
|
+
if ((await faceId.probe()).available)
|
|
147
|
+
await faceId.enable(deviceToken)
|
|
148
|
+
|
|
149
|
+
// 2. On the sign-in screen: <BiometricSignIn> does this for you.
|
|
150
|
+
const result = await faceId.signIn('Sign in to HQ')
|
|
151
|
+
// { status: 'signed-in', token } | { status: 'cancelled' } | { status: 'invalidated' } | { status: 'failed', code, message }
|
|
152
|
+
|
|
153
|
+
// 3. On sign-out.
|
|
154
|
+
await faceId.forget()
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
```html
|
|
158
|
+
<BiometricSignIn reason="Sign in to HQ"
|
|
159
|
+
@signin="session.exchange($event.token)"
|
|
160
|
+
@fallback="showPassword()" />
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
What the app must provide:
|
|
164
|
+
|
|
165
|
+
- **A device token, never the password.** The server issues a long-lived token for this device at password sign-in, exchanges it for a session, and revokes it on sign-out.
|
|
166
|
+
- **`permissions.faceId` in the Craft config** (`NSFaceIDUsageDescription`). iOS ends an app that asks for Face ID without it.
|
|
167
|
+
- **A password path.** `invalidated` means another face or finger was enrolled (or the token is gone); the token is forgotten and the button goes away until the next password sign-in.
|
|
168
|
+
|
|
169
|
+
The simulator has no Secure Enclave: Features → Face ID → Enrolled, then Matching Face or Non-matching Face, exercises the flow, but only a device proves a read without a match is refused.
|
|
170
|
+
|
|
121
171
|
## Features
|
|
122
172
|
|
|
123
173
|
- **Modern ES module exports** - Clean, modern JavaScript
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Sign in with Face ID", without a template.
|
|
3
|
+
*
|
|
4
|
+
* Craft keeps a credential where only the face or finger enrolled on this
|
|
5
|
+
* device releases it (`craft.biometrics.storeCredential`, Craft 0.0.511): the
|
|
6
|
+
* Keychain itself withholds it until the person passes, so a token read
|
|
7
|
+
* afterwards is not merely guarded by the app's own code. This module is the
|
|
8
|
+
* flow around those calls, so `<BiometricSignIn>` and an app's own settings
|
|
9
|
+
* screen say and do the same thing:
|
|
10
|
+
*
|
|
11
|
+
* - after a password sign-in, `enable(token)` keeps the app's device token;
|
|
12
|
+
* - on the sign-in screen, `probe()` says whether to offer the button and
|
|
13
|
+
* what to call it, and `signIn(reason)` shows the system prompt;
|
|
14
|
+
* - on sign-out, `forget()`.
|
|
15
|
+
*
|
|
16
|
+
* Keep a device token the server can revoke, never the password: the server
|
|
17
|
+
* swaps it for a session and forgets it on sign-out.
|
|
18
|
+
*
|
|
19
|
+
* Away from Craft (a browser, Android today) there is no host and every call
|
|
20
|
+
* answers that nothing is available, so the button is simply not offered.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** What the host reports: `craft.biometrics.getBiometricType()`. */
|
|
24
|
+
export type BiometricKind = 'faceId' | 'touchId' | 'fingerprint' | 'none' | null
|
|
25
|
+
|
|
26
|
+
/** The part of `craft.biometrics` this flow uses. */
|
|
27
|
+
export interface BiometricCredentialHost {
|
|
28
|
+
isAvailable: () => Promise<boolean>
|
|
29
|
+
getBiometricType: () => Promise<string | null>
|
|
30
|
+
storeCredential: (key: string, value: string) => Promise<unknown>
|
|
31
|
+
getCredential: (key: string, reason?: string) => Promise<string | null>
|
|
32
|
+
hasCredential: (key: string) => Promise<boolean>
|
|
33
|
+
deleteCredential: (key: string) => Promise<unknown>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Whether to offer biometric sign-in here, and what to call it. */
|
|
37
|
+
export interface BiometricProbe {
|
|
38
|
+
/** A face or finger is enrolled and the host keeps credentials. */
|
|
39
|
+
available: boolean
|
|
40
|
+
kind: BiometricKind
|
|
41
|
+
/** `Face ID`, `Touch ID` or `Fingerprint`; empty when unavailable. */
|
|
42
|
+
label: string
|
|
43
|
+
/** A credential is kept under the key: offer the button. */
|
|
44
|
+
saved: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* How a sign-in ended.
|
|
49
|
+
*
|
|
50
|
+
* - `signed-in`: the token, to swap for a session.
|
|
51
|
+
* - `cancelled`: the person dismissed the prompt; leave the button.
|
|
52
|
+
* - `invalidated`: nothing came back (another face or finger was enrolled, or
|
|
53
|
+
* it was never kept). It is forgotten, so ask for the password.
|
|
54
|
+
* - `failed`: the face or finger did not match, or the host refused; `code`
|
|
55
|
+
* is the host's (`AUTHENTICATION_FAILED`, `BIOMETRICS_UNAVAILABLE`, ...).
|
|
56
|
+
*/
|
|
57
|
+
export type BiometricSignInResult =
|
|
58
|
+
| { status: 'signed-in', token: string }
|
|
59
|
+
| { status: 'cancelled' }
|
|
60
|
+
| { status: 'invalidated' }
|
|
61
|
+
| { status: 'failed', code: string, message: string }
|
|
62
|
+
|
|
63
|
+
export interface BiometricSignIn {
|
|
64
|
+
probe: () => Promise<BiometricProbe>
|
|
65
|
+
signIn: (reason?: string) => Promise<BiometricSignInResult>
|
|
66
|
+
/** Keep `token` behind the face or finger. Resolves false when it cannot be kept here. */
|
|
67
|
+
enable: (token: string) => Promise<boolean>
|
|
68
|
+
forget: () => Promise<void>
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The key a credential is kept under when the app does not name one. */
|
|
72
|
+
export const BIOMETRIC_DEFAULT_KEY = 'session'
|
|
73
|
+
|
|
74
|
+
const LABELS: Readonly<Record<string, string>> = {
|
|
75
|
+
faceId: 'Face ID',
|
|
76
|
+
touchId: 'Touch ID',
|
|
77
|
+
fingerprint: 'Fingerprint',
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** What to call the host's kind of biometry: `faceId` is `Face ID`. */
|
|
81
|
+
export function biometricLabel(kind: unknown): string {
|
|
82
|
+
return typeof kind === 'string' ? (LABELS[kind] || '') : ''
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The button's words: `Sign in with Face ID`. */
|
|
86
|
+
export function biometricSignInLabel(kind: unknown): string {
|
|
87
|
+
const label = biometricLabel(kind)
|
|
88
|
+
return label ? `Sign in with ${label}` : ''
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The host's `craft.biometrics`, when this page runs inside Craft and it keeps credentials. */
|
|
92
|
+
export function biometricHost(): BiometricCredentialHost | null {
|
|
93
|
+
const craft = (globalThis as { craft?: { biometrics?: Partial<BiometricCredentialHost> } }).craft
|
|
94
|
+
const host = craft?.biometrics
|
|
95
|
+
if (!host)
|
|
96
|
+
return null
|
|
97
|
+
const needed: Array<keyof BiometricCredentialHost> = ['isAvailable', 'getBiometricType', 'storeCredential', 'getCredential', 'hasCredential', 'deleteCredential']
|
|
98
|
+
return needed.every(name => typeof host[name] === 'function') ? host as BiometricCredentialHost : null
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const NOTHING: BiometricProbe = Object.freeze({ available: false, kind: null, label: '', saved: false })
|
|
102
|
+
|
|
103
|
+
function codeOf(error: unknown): string {
|
|
104
|
+
const code = (error as { code?: unknown } | null)?.code
|
|
105
|
+
return typeof code === 'string' && code ? code : 'UNKNOWN'
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function messageOf(error: unknown): string {
|
|
109
|
+
return error instanceof Error ? error.message : String(error ?? '')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The flow for one credential.
|
|
114
|
+
*
|
|
115
|
+
* `host` defaults to the page's `craft.biometrics`, read on each call, so a
|
|
116
|
+
* bridge installed after this was created is still found.
|
|
117
|
+
*/
|
|
118
|
+
export function createBiometricSignIn(key: string = BIOMETRIC_DEFAULT_KEY, host?: BiometricCredentialHost | null): BiometricSignIn {
|
|
119
|
+
const name = String(key || '').trim() || BIOMETRIC_DEFAULT_KEY
|
|
120
|
+
const current = (): BiometricCredentialHost | null => host === undefined ? biometricHost() : host
|
|
121
|
+
|
|
122
|
+
async function probe(): Promise<BiometricProbe> {
|
|
123
|
+
const bio = current()
|
|
124
|
+
if (!bio)
|
|
125
|
+
return NOTHING
|
|
126
|
+
try {
|
|
127
|
+
if (!await bio.isAvailable())
|
|
128
|
+
return NOTHING
|
|
129
|
+
const kind = (await bio.getBiometricType()) as BiometricKind
|
|
130
|
+
const label = biometricLabel(kind)
|
|
131
|
+
if (!label)
|
|
132
|
+
return NOTHING
|
|
133
|
+
return { available: true, kind, label, saved: await bio.hasCredential(name) === true }
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
// A host that cannot answer is one with nothing to offer.
|
|
137
|
+
return NOTHING
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function signIn(reason?: string): Promise<BiometricSignInResult> {
|
|
142
|
+
const bio = current()
|
|
143
|
+
if (!bio)
|
|
144
|
+
return { status: 'failed', code: 'UNSUPPORTED', message: 'Biometric sign-in is not available here.' }
|
|
145
|
+
try {
|
|
146
|
+
const token = await bio.getCredential(name, reason || undefined)
|
|
147
|
+
if (typeof token === 'string' && token)
|
|
148
|
+
return { status: 'signed-in', token }
|
|
149
|
+
// Gone or invalidated: forget it so the button is not offered again.
|
|
150
|
+
await bio.deleteCredential(name).catch(() => {})
|
|
151
|
+
return { status: 'invalidated' }
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
const code = codeOf(error)
|
|
155
|
+
if (code === 'AUTHENTICATION_CANCELLED')
|
|
156
|
+
return { status: 'cancelled' }
|
|
157
|
+
return { status: 'failed', code, message: messageOf(error) }
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function enable(token: string): Promise<boolean> {
|
|
162
|
+
const bio = current()
|
|
163
|
+
if (!bio || typeof token !== 'string' || !token)
|
|
164
|
+
return false
|
|
165
|
+
try {
|
|
166
|
+
if (!await bio.isAvailable())
|
|
167
|
+
return false
|
|
168
|
+
await bio.storeCredential(name, token)
|
|
169
|
+
return true
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
// BIOMETRICS_UNAVAILABLE (nothing enrolled, no passcode) or a Keychain
|
|
173
|
+
// error: the app keeps signing in with the password.
|
|
174
|
+
return false
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function forget(): Promise<void> {
|
|
179
|
+
const bio = current()
|
|
180
|
+
if (!bio)
|
|
181
|
+
return
|
|
182
|
+
await bio.deleteCredential(name).catch(() => {})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return { probe, signIn, enable, forget }
|
|
186
|
+
}
|
package/src/ui/auth/index.ts
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
|
+
export { default as BiometricSignIn } from './BiometricSignIn.stx'
|
|
1
2
|
export { default as Login } from './Login.stx'
|
|
2
3
|
export { default as Signup } from './Signup.stx'
|
|
3
4
|
export { default as TwoFactorChallenge } from './TwoFactorChallenge.stx'
|
|
4
5
|
|
|
6
|
+
export * from './biometric'
|
|
7
|
+
|
|
8
|
+
export interface BiometricSignInProps {
|
|
9
|
+
/** The key the credential is kept under (default `session`). */
|
|
10
|
+
credentialKey?: string
|
|
11
|
+
/** The line under the system prompt: `Sign in to HQ`. */
|
|
12
|
+
reason?: string
|
|
13
|
+
variant?: string
|
|
14
|
+
fullWidth?: boolean
|
|
15
|
+
onSignin?: (detail: { token: string }) => void
|
|
16
|
+
onFallback?: (detail: { reason: 'cancelled' | 'invalidated' }) => void
|
|
17
|
+
onFailed?: (detail: { code: string, message: string }) => void
|
|
18
|
+
className?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
5
21
|
export interface LoginProps {
|
|
6
22
|
showLogo?: boolean
|
|
7
23
|
headingText?: string
|