-
Notifications
You must be signed in to change notification settings - Fork 7
/
BiometryView.vue
404 lines (362 loc) · 9.95 KB
/
BiometryView.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<template>
<ion-page class="w-full h-full">
<ion-header>
<ion-toolbar>
<ion-title>Biometry</ion-title>
<ion-buttons slot="end">
<ion-button @click="onAuthenticate"> Authenticate</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content :scroll-y="true">
<ion-list lines="full">
<ion-item>
<ion-label>
<h3 class="!text-sm">Supported biometry</h3>
<!-- eslint-disable-next-line vue/no-v-text-v-html-on-component,vue/no-v-html -->
<div v-html="biometryNames" />
</ion-label>
</ion-item>
<ion-item>
<ion-label>
<ion-text class="block">Biometry available</ion-text>
<ion-text class="block !text-sm text-neutral-400">
{{ biometry.reason }}
</ion-text>
</ion-label>
<ion-text slot="end">
{{ availableBiometry }}
</ion-text>
</ion-item>
<ion-item>
<ion-label>
<ion-text class="block">Strong biometry available</ion-text>
<ion-text class="block !text-sm text-neutral-400">
{{ biometry.strongReason }}
</ion-text>
</ion-label>
<ion-text slot="end">
{{ biometry.strongBiometryIsAvailable ? 'Yes' : 'No' }}
</ion-text>
</ion-item>
<ion-item>
<ion-label> Device is secure</ion-label>
<ion-text slot="end">
{{ biometry.deviceIsSecure ? 'Yes' : 'No' }}
</ion-text>
</ion-item>
</ion-list>
<ion-list
class="mt-6"
lines="full"
>
<ion-list-header>Options</ion-list-header>
<template v-if="!isNative">
<ion-item>
<ion-select
v-model="biometryType"
label="Biometry"
class="[--padding-start:0px] max-w-full"
interface="action-sheet"
:interface-options="{ header: 'Select biometry type' }"
@ion-change="onSelectBiometry"
>
<ion-select-option
v-for="entry in biometryTypes"
:key="entry.type"
:value="entry.type"
>
{{ entry.title }}
</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-checkbox
v-model="isEnrolled"
:disabled="biometry.biometryType === BiometryType.none"
@ion-change="onSetIsEnrolled"
>
Enrolled
</ion-checkbox>
</ion-item>
<ion-item>
<ion-checkbox
v-model="deviceIsSecure"
@ion-change="onSetDeviceIsSecure"
>
Device is secure
</ion-checkbox>
</ion-item>
</template>
<ion-item v-if="isAndroid">
<ion-checkbox
v-model="onlyUseStrongBiometry"
@ion-change="onSetAndroidBiometryStrength"
>
Only use strong biometry
</ion-checkbox>
</ion-item>
<ion-item>
<ion-checkbox v-model="options.allowDeviceCredential">
Allow device credential
</ion-checkbox>
</ion-item>
<ion-item v-if="isAndroid">
<ion-checkbox v-model="options.androidConfirmationRequired">
Require confirmation
</ion-checkbox>
</ion-item>
<template v-if="isAndroid">
<ion-item>
<ion-input
v-model="options.androidTitle"
label="Title:"
type="text"
autocapitalize="sentences"
/>
</ion-item>
<ion-item>
<ion-input
v-model="options.androidSubtitle"
label="Subtitle:"
type="text"
autocapitalize="sentences"
/>
</ion-item>
</template>
<ion-item>
<ion-input
v-model="options.reason"
label="Reason:"
type="text"
autocapitalize="sentences"
/>
</ion-item>
<ion-item v-if="isNative">
<ion-input
v-model="options.cancelTitle"
label="Cancel title:"
type="text"
autocapitalize="sentences"
/>
</ion-item>
<ion-item v-if="isIOS">
<ion-input
v-model="options.iosFallbackTitle"
label="Fallback title:"
type="text"
autocapitalize="sentences"
/>
</ion-item>
</ion-list>
</ion-content>
</ion-page>
</template>
<script setup lang="ts">
import {
AndroidBiometryStrength,
type AuthenticateOptions,
BiometricAuth,
type BiometryError,
BiometryErrorType,
BiometryType,
type CheckBiometryResult,
getBiometryName,
} from '@aparajita/capacitor-biometric-auth'
import { Capacitor, type PluginListenerHandle } from '@capacitor/core'
import { SplashScreen } from '@capacitor/splash-screen'
import type { SelectCustomEvent } from '@ionic/core'
import {
alertController,
IonButton,
IonButtons,
IonCheckbox,
IonContent,
IonHeader,
IonInput,
IonItem,
IonLabel,
IonList,
IonListHeader,
IonPage,
IonSelect,
IonSelectOption,
IonText,
IonTitle,
IonToolbar,
} from '@ionic/vue'
import {
computed,
onBeforeMount,
onBeforeUnmount,
onMounted,
reactive,
ref,
toRaw,
} from 'vue'
interface BiometryTypeEntry {
title: string
type: number
}
const biometryTypes: BiometryTypeEntry[] = [
{
title: 'None',
type: BiometryType.none,
},
{
title: 'Touch ID',
type: BiometryType.touchId,
},
{
title: 'Face ID',
type: BiometryType.faceId,
},
{
title: 'Fingerprint',
type: BiometryType.fingerprintAuthentication,
},
{
title: 'Fingerprint + face',
type:
BiometryType.fingerprintAuthentication * 10 +
BiometryType.faceAuthentication,
},
{
title: 'Fingerprint + iris',
type:
BiometryType.fingerprintAuthentication * 10 +
BiometryType.irisAuthentication,
},
]
/*
* ref
*/
const biometry = ref<CheckBiometryResult>({
isAvailable: false,
strongBiometryIsAvailable: false,
biometryType: BiometryType.none,
biometryTypes: [],
deviceIsSecure: false,
reason: '',
code: BiometryErrorType.none,
strongReason: '',
strongCode: BiometryErrorType.none,
})
const options = reactive<AuthenticateOptions>({
reason: '',
cancelTitle: '',
iosFallbackTitle: '',
androidTitle: '',
androidSubtitle: '',
allowDeviceCredential: false,
androidConfirmationRequired: false,
androidBiometryStrength: AndroidBiometryStrength.weak,
})
const biometryType = ref(BiometryType.none)
const onlyUseStrongBiometry = ref(false)
const isEnrolled = ref(false)
const deviceIsSecure = ref(false)
let appListener: PluginListenerHandle
const isNative = Capacitor.isNativePlatform()
const isIOS = Capacitor.getPlatform() === 'ios'
const isAndroid = Capacitor.getPlatform() === 'android'
/*
* computed
*/
const biometryName = computed(() => {
if (biometry.value.biometryTypes.length === 0) {
return 'No biometry'
}
if (biometry.value.biometryTypes.length === 1) {
return getBiometryName(biometry.value.biometryType)
}
return 'Biometry'
})
const biometryNames = computed(() => {
if (biometry.value.biometryTypes.length === 0) {
return 'None'
}
return biometry.value.biometryTypes
.map((type) => getBiometryName(type))
.join('<br>')
})
const availableBiometry = computed(() => {
if (biometry.value.isAvailable) {
return biometry.value.biometryTypes.length > 1 ? 'One or more' : 'Yes'
} else {
return 'None'
}
})
/*
* methods
*/
function updateBiometryInfo(info: CheckBiometryResult): void {
console.log('updateBiometryInfo', info)
biometry.value = info
}
onBeforeMount(async () => {
updateBiometryInfo(await BiometricAuth.checkBiometry())
try {
appListener = await BiometricAuth.addResumeListener(updateBiometryInfo)
} catch (error) {
if (error instanceof Error) {
console.error(error.message)
}
}
})
onMounted(async () => {
await SplashScreen.hide()
})
onBeforeUnmount(async () => {
await appListener?.remove()
})
async function showAlert(message: string): Promise<void> {
const alert = await alertController.create({
header: `${biometryName.value} says:`,
subHeader: '',
message,
buttons: ['OK'],
})
await alert.present()
}
async function showErrorAlert(error: BiometryError): Promise<void> {
await showAlert(`${error.message} [${error.code}].`)
}
async function onAuthenticate(): Promise<void> {
try {
// options is a reactive proxy, we can't pass it directly to a plugin.
// so pass the underlying object.
await BiometricAuth.authenticate(toRaw(options))
await showAlert('Authorization successful!')
} catch (error) {
// Someday TypeScript will let us type catch clauses...
await showErrorAlert(error as BiometryError)
}
}
async function onSelectBiometry(
event: SelectCustomEvent<string>,
): Promise<void> {
const type = Number(event.detail.value)
if (type > 10) {
const primary = Math.floor(type / 10) as BiometryType
const secondary = (type % 10) as BiometryType
await BiometricAuth.setBiometryType([primary, secondary])
} else {
await BiometricAuth.setBiometryType(type === 0 ? BiometryType.none : type)
}
updateBiometryInfo(await BiometricAuth.checkBiometry())
}
function onSetAndroidBiometryStrength(): void {
options.androidBiometryStrength = onlyUseStrongBiometry.value
? AndroidBiometryStrength.strong
: AndroidBiometryStrength.weak
}
async function onSetIsEnrolled(): Promise<void> {
await BiometricAuth.setBiometryIsEnrolled(isEnrolled.value)
updateBiometryInfo(await BiometricAuth.checkBiometry())
}
async function onSetDeviceIsSecure(): Promise<void> {
await BiometricAuth.setDeviceIsSecure(deviceIsSecure.value)
updateBiometryInfo(await BiometricAuth.checkBiometry())
}
</script>