Skip to content

Commit 392b8fb

Browse files
Update biometric.mdx
Inserting the documentation of the new biometricCipher() method. See tauri-apps#2454 and tauri-apps#2306.
1 parent 523cc4a commit 392b8fb

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

src/content/docs/plugin/biometric.mdx

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ In the `src-tauri/Info.ios.plist` file, add the following snippet:
9898

9999
## Usage
100100

101-
This plugin enables you to verify the availability of Biometric Authentication on a device, prompt the user for biometric authentication, and check the result to determine if the authentication was successful or not.
101+
This plugin enables you to verify the availability of Biometric Authentication on a device, prompt the user for biometric authentication, and check the result to determine if the authentication was successful or not. On Android, it also allows you to encrypt/decrypt data using assymmetric keys that can be accessed only if the user authenticates using their registered biometric authentication method.
102102

103103
### Check Status
104104

@@ -213,6 +213,100 @@ fn bio_auth(app_handle: tauri::AppHandle) {
213213
</TabItem>
214214
</Tabs>
215215

216+
### Biometric protected cryptography
217+
218+
To encrypt/decrypt data using an assymetric cryptography method that is protected behid the user Biometric Authentication, utilize the `biometricCipher()` method.
219+
220+
<Tabs syncKey="lang">
221+
222+
<TabItem label="JavaScript">
223+
224+
```javascript ins={18}
225+
import { biometricCipher } from '@tauri-apps/plugin-biometric';
226+
227+
// Encrypts data
228+
const encryptOptions = {
229+
// ... other options
230+
dataToEncrypt: getOriginalData()
231+
};
232+
233+
try {
234+
const encrypted = await biometricCipher('Passwordless authentication', encryptOptions);
235+
console.log(
236+
'Hooray! Successfully encrypted data! We can now store it to decrypt later, when needed'
237+
);
238+
} catch (err) {
239+
console.log('Oh no! Authentication failed because ' + err.message);
240+
}
241+
242+
243+
// Decrypts data back to the original
244+
const decryptOptions = {
245+
// ... other options
246+
dataToDecrypt: encrypted.data,
247+
};
248+
249+
try {
250+
const original = await biometricCipher('Passwordless authentication', decryptOptions);
251+
console.log(
252+
'Hooray! Successfully decrypted data after the user authenticated with their biometric method.'
253+
);
254+
const valid = originalData() == dataToDecrypt.data;
255+
} catch (err) {
256+
console.log('Oh no! Authentication failed because ' + err.message);
257+
}
258+
259+
```
260+
261+
</TabItem>
262+
263+
<TabItem label="Rust">
264+
265+
```rust ins={21}
266+
use tauri_plugin_biometric::{BiometricExt, AuthOptions};
267+
268+
fn bio_cipher(app_handle: tauri::AppHandle, original_data: Option<String>) {
269+
270+
let encrypt_options = AuthOptions {
271+
// ... other options
272+
data_to_encrypt: original_data.unwrap()
273+
};
274+
275+
// if the encryption was successful, the function returns Result::Ok(CipherResult)
276+
// otherwise returns Result::Error()
277+
match app_handle.biometric().biometric_cipher("Passwordless authentication".to_string(), encrypt_options) {
278+
Ok(encrypted) => {
279+
println!("Hooray! Successfully Authenticated! We can now perform the locked Tauri function!");
280+
}
281+
Err(e) => {
282+
println!("Oh no! Authentication failed because : {e}");
283+
}
284+
}
285+
286+
let decrypt_options = AuthOptions {
287+
// ... other options
288+
data_to_decrypt: encrypted.data
289+
};
290+
291+
// if the encryption was successful, the function returns Result::Ok(CipherResult)
292+
// otherwise returns Result::Error()
293+
match app_handle.biometric().biometric_cipher("Passwordless authentication".to_string(), decrypt_options) {
294+
Ok(decrypted) => {
295+
println!("Hooray! Successfully Authenticated! We can now perform the locked Tauri function!");
296+
}
297+
Err(e) => {
298+
println!("Oh no! Authentication failed because : {e}");
299+
}
300+
}
301+
302+
assert_equal!(decrypted.data, original_data.unwrap());
303+
304+
}
305+
```
306+
307+
</TabItem>
308+
</Tabs>
309+
216310
## Permissions
217311

218312
By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your `capabilities` configuration to enable these.

0 commit comments

Comments
 (0)