Skip to content

Commit

Permalink
Add a wipeLocal function for logins, fixes #493
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom Chiovoloni committed Jan 3, 2019
1 parent bb0e1f5 commit a861bbe
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 3 deletions.
Expand Up @@ -96,6 +96,13 @@ class DatabaseLoginsStorage(private val dbPath: String) : AutoCloseable, LoginsS
}
}

@Throws(LoginsStorageException::class)
override fun wipeLocal() {
rustCallWithLock { raw, error ->
PasswordSyncAdapter.INSTANCE.sync15_passwords_wipe_local(raw, error)
}
}

@Throws(LoginsStorageException::class)
override fun delete(id: String): Boolean {
return rustCallWithLock { raw, error ->
Expand Down
Expand Up @@ -62,21 +62,30 @@ interface LoginsStorage : AutoCloseable {
fun sync(syncInfo: SyncUnlockInfo)

/**
* Delete all locally stored login sync metadata.
* Delete all locally stored login sync metadata (last sync timestamps, etc).
*
* @throws [LoginsStorageException] On unexpected errors (IO failure, rust panics, etc)
*/
@Throws(LoginsStorageException::class)
@Deprecated("Most uses should be replaced with wipe or wipeLocal instead")
fun reset()

/**
* Delete all locally stored login data.
* Delete all login records. These deletions will be synced to the server on the next call to sync.
*
* @throws [LoginsStorageException] On unexpected errors (IO failure, rust panics, etc)
*/
@Throws(LoginsStorageException::class)
fun wipe()

/**
* Clear out all local state, bringing us back to the state before the first sync.
*
* @throws [LoginsStorageException] On unexpected errors (IO failure, rust panics, etc)
*/
@Throws(LoginsStorageException::class)
fun wipeLocal()

/**
* Deletes the password with the given ID.
*
Expand Down
Expand Up @@ -93,6 +93,14 @@ class MemoryLoginsStorage(private var list: List<ServerPassword>) : AutoCloseabl
list = ArrayList()
}

@Synchronized
@Throws(LoginsStorageException::class)
override fun wipeLocal() {
checkUnlocked()
// No remote state.
list = ArrayList()
}

@Synchronized
@Throws(LoginsStorageException::class)
override fun delete(id: String): Boolean {
Expand Down
Expand Up @@ -63,6 +63,7 @@ internal interface PasswordSyncAdapter : Library {
error: RustError.ByReference)

fun sync15_passwords_wipe(state: RawLoginSyncState, error: RustError.ByReference)
fun sync15_passwords_wipe_local(state: RawLoginSyncState, error: RustError.ByReference)
fun sync15_passwords_reset(state: RawLoginSyncState, error: RustError.ByReference)

fun sync15_passwords_touch(state: RawLoginSyncState, id: String, error: RustError.ByReference)
Expand Down
Expand Up @@ -64,7 +64,10 @@ abstract class LoginsStorageTest {
expectException(LoginsStorageException::class.java) { test.touch("bbbbbbbbbbbb") }
expectException(LoginsStorageException::class.java) { test.wipe() }
expectException(LoginsStorageException::class.java) { test.sync(SyncUnlockInfo("", "", "", "")) }
expectException(LoginsStorageException::class.java) { test.reset() }
expectException(LoginsStorageException::class.java) {
@Suppress("DEPRECATION")
test.reset()
}

test.unlock(encryptionKey)
assertEquals(test.isLocked(), false)
Expand Down Expand Up @@ -145,6 +148,22 @@ abstract class LoginsStorageTest {
finishAndClose(test)
}


@Test
fun testWipeLocal() {
val test = getTestStore()
test.unlock(encryptionKey)
assertEquals(2, test.list().size)

test.wipeLocal()
assertEquals(0, test.list().size)

assertNull(test.get("aaaaaaaaaaaa"))
assertNull(test.get("bbbbbbbbbbbb"))

finishAndClose(test)
}

@Test
fun testAdd() {
val test = getTestStore()
Expand Down Expand Up @@ -265,6 +284,7 @@ abstract class LoginsStorageTest {
}

@Test
@Suppress("DEPRECATION")
fun testUnlockAfterError() {
val test = getTestStore()

Expand Down
9 changes: 9 additions & 0 deletions components/logins/ffi/src/lib.rs
Expand Up @@ -88,6 +88,15 @@ pub unsafe extern "C" fn sync15_passwords_wipe(state: &PasswordEngine, error: &m
call_with_result(error, || state.wipe())
}

#[no_mangle]
pub unsafe extern "C" fn sync15_passwords_wipe_local(
state: &PasswordEngine,
error: &mut ExternError,
) {
log::trace!("sync15_passwords_wipe_local");
call_with_result(error, || state.wipe_local())
}

#[no_mangle]
pub extern "C" fn sync15_passwords_reset(state: &PasswordEngine, error: &mut ExternError) {
log::trace!("sync15_passwords_reset");
Expand Down
10 changes: 10 additions & 0 deletions components/logins/src/db.rs
Expand Up @@ -604,6 +604,16 @@ impl LoginDb {
Ok(())
}

pub fn wipe_local(&self) -> Result<()> {
log::info!("Executing wipe_local on password store!");
self.execute_all(&[
"DELETE FROM loginsL",
"DELETE FROM loginsM",
"DELETE FROM loginsSyncMeta",
])?;
Ok(())
}

fn reconcile(
&self,
records: Vec<SyncLoginData>,
Expand Down
5 changes: 5 additions & 0 deletions components/logins/src/engine.rs
Expand Up @@ -54,6 +54,11 @@ impl PasswordEngine {
Ok(())
}

pub fn wipe_local(&self) -> Result<()> {
self.db.wipe_local()?;
Ok(())
}

pub fn reset(&self) -> Result<()> {
self.db.reset()?;
Ok(())
Expand Down

0 comments on commit a861bbe

Please sign in to comment.