Implement native iPhone entry editor

This commit is contained in:
hugo committed 2026-08-11 20:27:14 +02:00
1 parent 900e62e523
commit ae64ce47a3
8 files changed
+2516 -76

No files matched your search

+277 -10
View File
@@ -1,6 +1,6 @@
//! Shared mobile authentication state; Swift only supplies input and presents results.
use std::{error::Error, fmt, sync::Mutex};
use std::{collections::BTreeMap, error::Error, fmt, sync::Mutex};
use crate::{
authentication::{
@@ -10,9 +10,17 @@ use crate::{
crypto::{CryptoError, KeyInfo, KeyStore, SecretProvider, SecretProviderError},
document::{DocumentError, EntryDocument, EntryDocumentService, EntryFieldId},
git::{AutomaticEntryCommitter, GitIdentity},
mobile_entry::{MobileEntryPage, MobileEntryValueError, field_value},
repository::{EntryPath, Repository, RepositoryError, SecretBytes},
mobile_entry::{
MobileEntryDraft, MobileEntryEditorError, MobileEntryEditorFieldKind,
MobileEntryEditorInput, MobileEntryEditorPage, MobileEntryEditorSession, MobileEntryPage,
MobileEntryValueError, field_value,
},
recipient::RecipientPolicyManager,
repository::{
DirectoryPath, EncryptedEntry, EntryPath, Repository, RepositoryError, SecretBytes,
},
secret_store::{SecretProtectionPolicy, SecretStoreError},
write::{VaultWriter, WriteError},
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -26,6 +34,7 @@ pub enum MobileAuthenticationErrorKind {
Entry,
SecureStorage,
Expired,
Conflict,
}
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -150,6 +159,8 @@ struct ActiveMobileLease {
struct MobileAuthenticationStatus {
biometric_unlock_enabled: bool,
active: Option<ActiveMobileLease>,
next_editor_id: u64,
editors: BTreeMap<u64, MobileEntryDraft>,
}
/// One process-wide mobile authentication lease shared by every tab and viewer.
@@ -181,6 +192,8 @@ impl MobileAuthentication {
status: Mutex::new(MobileAuthenticationStatus {
biometric_unlock_enabled: config.biometric_unlock_enabled(),
active: None,
next_editor_id: 0,
editors: BTreeMap::new(),
}),
config,
repository,
@@ -197,17 +210,26 @@ impl MobileAuthentication {
) -> Result<MobileAuthenticationState, MobileAuthenticationError> {
let entry = EntryPath::parse(path).map_err(entry_error)?;
let ciphertext = self.repository.read_entry(&entry).map_err(entry_error)?;
let candidates = self.keys.decrypting_keys(&ciphertext).map_err(key_error)?;
self.unlock_ciphertext(&ciphertext, passphrase)
}
fn unlock_ciphertext(
&self,
ciphertext: &EncryptedEntry,
passphrase: Option<SecretBytes>,
) -> Result<MobileAuthenticationState, MobileAuthenticationError> {
let candidates = self.keys.decrypting_keys(ciphertext).map_err(key_error)?;
if let Some(active) = self.take_active()? {
let mut provider = KeyOnlyProvider::new(active.handle.clone(), &active.key);
if self.keys.decrypt(&ciphertext, &mut provider).is_ok() {
if self.keys.decrypt(ciphertext, &mut provider).is_ok() {
self.restore_active(active)?;
return self.state();
}
self.session
.manual_lock()
.map_err(MobileAuthenticationError::authentication)?;
self.status()?.editors.clear();
}
let biometric_enabled = self.status()?.biometric_unlock_enabled;
@@ -220,7 +242,7 @@ impl MobileAuthentication {
.authenticate_with_passphrase(key, candidate)
.map_err(MobileAuthenticationError::authentication)?;
let mut provider = KeyOnlyProvider::new(handle.clone(), key);
match self.keys.decrypt(&ciphertext, &mut provider) {
match self.keys.decrypt(ciphertext, &mut provider) {
Ok(plaintext) => {
drop(plaintext);
if biometric_enabled {
@@ -257,7 +279,7 @@ impl MobileAuthentication {
Err(_) => continue,
};
let mut provider = KeyOnlyProvider::new(handle.clone(), key);
if let Ok(plaintext) = self.keys.decrypt(&ciphertext, &mut provider) {
if let Ok(plaintext) = self.keys.decrypt(ciphertext, &mut provider) {
drop(plaintext);
self.set_active(handle, key.clone())?;
return self.state();
@@ -312,6 +334,7 @@ impl MobileAuthentication {
let mut status = self.status()?;
status.biometric_unlock_enabled = false;
status.active = None;
status.editors.clear();
}
self.state()
}
@@ -324,6 +347,7 @@ impl MobileAuthentication {
let mut status = self.status()?;
if remaining.is_none() {
status.active = None;
status.editors.clear();
}
Ok(MobileAuthenticationState {
unlocked: status.active.is_some(),
@@ -391,11 +415,191 @@ impl MobileAuthentication {
Ok(MobileEntryPage::from_document(&document))
}
pub fn begin_entry_editor(
&self,
path: &str,
) -> Result<MobileEntryEditorSession, MobileAuthenticationError> {
let document = self.open_active_document(path)?;
self.store_editor(MobileEntryDraft::new(document, false))
}
pub fn begin_create_entry(
&self,
directory: &str,
name: &str,
passphrase: Option<SecretBytes>,
) -> Result<MobileEntryEditorSession, MobileAuthenticationError> {
let directory = DirectoryPath::parse(directory).map_err(entry_error)?;
let leaf = EntryPath::parse(name).map_err(entry_error)?;
if leaf
.as_path()
.parent()
.is_some_and(|parent| !parent.as_os_str().is_empty())
{
return Err(entry_detail(
"Entry Name Is Invalid",
"enter a name without a folder separator",
));
}
let path =
EntryPath::parse(directory.as_path().join(leaf.as_path())).map_err(entry_error)?;
let path_text = path.to_string();
if VaultWriter::new(&self.repository, &self.keys)
.entry_exists(&path_text)
.map_err(|error| entry_detail("Password Entry Is Unavailable", error))?
{
return Err(entry_detail(
"Password Already Exists",
"Choose a different entry name.",
));
}
let recipients = RecipientPolicyManager::new(&self.repository, &self.keys)
.resolve_for_entry(&path, None)
.map_err(|error| entry_detail("Recipients Are Unavailable", error))?;
let probe = self
.keys
.encrypt(
SecretBytes::new(b"IronStorage mobile entry creation".to_vec()),
recipients.recipients(),
)
.map_err(key_error)?;
self.unlock_ciphertext(&probe, passphrase)?;
let mut document = self.open_active_document(&path_text)?;
document
.add(
0,
crate::document::EntryFieldDraft::line(Vec::new()).map_err(document_error)?,
)
.map_err(document_error)?;
self.store_editor(MobileEntryDraft::new(document, true))
}
pub fn entry_editor(
&self,
editor: u64,
) -> Result<MobileEntryEditorPage, MobileAuthenticationError> {
self.ensure_active()?;
self.status()?
.editors
.get(&editor)
.map(MobileEntryDraft::page)
.ok_or_else(editor_missing)
}
pub fn update_entry_editor(
&self,
editor: u64,
fields: Vec<MobileEntryEditorInput>,
) -> Result<MobileEntryEditorPage, MobileAuthenticationError> {
self.ensure_active()?;
let mut status = self.status()?;
let draft = status.editors.get_mut(&editor).ok_or_else(editor_missing)?;
draft.apply(fields).map_err(editor_error)?;
Ok(draft.page())
}
pub fn add_entry_editor_field(
&self,
editor: u64,
kind: MobileEntryEditorFieldKind,
name: Option<String>,
value: String,
) -> Result<MobileEntryEditorPage, MobileAuthenticationError> {
self.ensure_active()?;
let mut status = self.status()?;
let draft = status.editors.get_mut(&editor).ok_or_else(editor_missing)?;
draft.add(kind, name, value).map_err(editor_error)?;
Ok(draft.page())
}
pub fn remove_entry_editor_field(
&self,
editor: u64,
field: u64,
) -> Result<MobileEntryEditorPage, MobileAuthenticationError> {
self.ensure_active()?;
let mut status = self.status()?;
let draft = status.editors.get_mut(&editor).ok_or_else(editor_missing)?;
draft.remove(field).map_err(editor_error)?;
Ok(draft.page())
}
pub fn reorder_entry_editor_field(
&self,
editor: u64,
field: u64,
index: usize,
) -> Result<MobileEntryEditorPage, MobileAuthenticationError> {
self.ensure_active()?;
let mut status = self.status()?;
let draft = status.editors.get_mut(&editor).ok_or_else(editor_missing)?;
draft.reorder(field, index).map_err(editor_error)?;
Ok(draft.page())
}
pub fn generate_entry_editor_password(
&self,
editor: u64,
length: Option<u32>,
no_symbols: bool,
) -> Result<MobileEntryEditorPage, MobileAuthenticationError> {
self.ensure_active()?;
let mut status = self.status()?;
let draft = status.editors.get_mut(&editor).ok_or_else(editor_missing)?;
draft
.generate_password(length, no_symbols)
.map_err(editor_error)?;
Ok(draft.page())
}
pub fn save_entry_editor(
&self,
editor: u64,
fields: Vec<MobileEntryEditorInput>,
) -> Result<MobileEntryPage, MobileAuthenticationError> {
self.ensure_active()?;
let mut draft = self
.status()?
.editors
.remove(&editor)
.ok_or_else(editor_missing)?;
if let Err(error) = draft.apply(fields) {
self.restore_editor(editor, draft)?;
return Err(editor_error(error));
}
let path = draft.document().path().to_string();
let mut committer = match AutomaticEntryCommitter::for_entry(
&self.repository,
&path,
GitIdentity::ironstorage(),
) {
Ok(committer) => committer,
Err(error) => {
self.restore_editor(editor, draft)?;
return Err(entry_detail("Password Entry Could Not Be Saved", error));
}
};
if let Err(error) = EntryDocumentService::new(&self.repository, &self.keys)
.save_recoverable(draft.document(), None, &mut committer)
{
self.restore_editor(editor, draft)?;
return Err(document_error(error));
}
Ok(MobileEntryPage::from_document(draft.document()))
}
pub fn discard_entry_editor(&self, editor: u64) -> Result<(), MobileAuthenticationError> {
self.status()?.editors.remove(&editor);
Ok(())
}
pub fn manual_lock(&self) -> Result<(), MobileAuthenticationError> {
self.session
.manual_lock()
.map_err(MobileAuthenticationError::authentication)?;
self.status()?.active = None;
let mut status = self.status()?;
status.active = None;
status.editors.clear();
Ok(())
}
@@ -403,7 +607,9 @@ impl MobileAuthentication {
self.session
.cancel()
.map_err(MobileAuthenticationError::authentication)?;
self.status()?.active = None;
let mut status = self.status()?;
status.active = None;
status.editors.clear();
Ok(())
}
@@ -447,6 +653,48 @@ impl MobileAuthentication {
Ok(())
}
fn ensure_active(&self) -> Result<(), MobileAuthenticationError> {
let handle = self
.status()?
.active
.as_ref()
.map(|active| active.handle.clone())
.ok_or_else(locked_error)?;
if let Err(error) = handle.ensure_active() {
let mut status = self.status()?;
status.active = None;
status.editors.clear();
return Err(MobileAuthenticationError::authentication(error));
}
Ok(())
}
fn store_editor(
&self,
draft: MobileEntryDraft,
) -> Result<MobileEntryEditorSession, MobileAuthenticationError> {
let mut status = self.status()?;
let id = status.next_editor_id;
status.next_editor_id = status.next_editor_id.checked_add(1).ok_or_else(|| {
entry_detail(
"Entry Editor Is Unavailable",
"entry editor identifiers are exhausted",
)
})?;
let page = draft.page();
status.editors.insert(id, draft);
Ok(MobileEntryEditorSession::new(id, page))
}
fn restore_editor(
&self,
editor: u64,
draft: MobileEntryDraft,
) -> Result<(), MobileAuthenticationError> {
self.status()?.editors.insert(editor, draft);
Ok(())
}
fn open_active_document(&self, path: &str) -> Result<EntryDocument, MobileAuthenticationError> {
let (handle, key) = {
let status = self.status()?;
@@ -519,13 +767,32 @@ fn locked_error() -> MobileAuthenticationError {
}
fn document_error(error: DocumentError) -> MobileAuthenticationError {
entry_detail("Password Entry Is Unavailable", error)
let kind = if matches!(
&error,
DocumentError::Write(WriteError::ConcurrentModification { .. })
) {
MobileAuthenticationErrorKind::Conflict
} else {
MobileAuthenticationErrorKind::Entry
};
MobileAuthenticationError::new(kind, "Password Entry Could Not Be Saved", error.to_string())
}
fn value_error(error: MobileEntryValueError) -> MobileAuthenticationError {
entry_detail("Field Value Is Unavailable", error)
}
fn editor_error(error: MobileEntryEditorError) -> MobileAuthenticationError {
entry_detail("Entry Draft Is Invalid", error)
}
fn editor_missing() -> MobileAuthenticationError {
entry_detail(
"Entry Draft Is Unavailable",
"the entry draft was discarded or locked",
)
}
fn entry_detail(title: &str, error: impl fmt::Display) -> MobileAuthenticationError {
MobileAuthenticationError::new(
MobileAuthenticationErrorKind::Entry,