Commit 9f98966f authored by Grisha Kruglov's avatar Grisha Kruglov
Browse files

Closes #6184: Pass 'guid' correctly into update/add logins methods

parent da2c83c8
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -28,7 +28,11 @@ class DefaultLoginValidationDelegate(
    override fun validateCanPersist(login: Login): Deferred<Result> {
        return scope.async {
            try {
                storage.ensureValid(login)
                // We're setting guid=null here in order to trigger de-duping logic properly.
                // Internally, the library ensures to not dupe records against themselves
                // (via a `guid <> :guid` check), which means we need to "pretend" this is a new record,
                // in order for `ensureValid` to actually throw `DUPLICATE_LOGIN`.
                storage.ensureValid(login.copy(guid = null))
                Result.CanBeCreated
            } catch (e: InvalidRecordException) {
                when (e.reason) {
+5 −5
Original line number Diff line number Diff line
@@ -88,15 +88,15 @@ class GeckoLoginStorageDelegate(
                Operation.CREATE -> {
                    // If an existing Login was autofilled, we want to clear its guid to
                    // avoid updating its record
                    loginStorage.add(login.copy(guid = ""))
                    loginStorage.add(login.copy(guid = null))
                }
            }
        }
    }

    /**
     * Returns whether an existing login record should be [UPDATE]d or a new one [CREATE]d, based
     * on the saved [ServerPassword] and new [Login].
     * Returns whether an existing login record should be UPDATED or a new one [CREATE]d, based
     * on the saved [Login] and new [Login].
     */
    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    fun getPersistenceOperation(newLogin: Login, savedLogin: Login?): Operation = when {
@@ -146,8 +146,8 @@ fun Login.mergeWithLogin(login: Login): Login {
 * Converts an Android Components [Login] to an Application Services [ServerPassword]
 */
fun Login.toServerPassword() = ServerPassword(
    // Underlying Rust code will generate a new GUID
    id = "",
    // Underlying Rust code will generate a new GUID if `guid` is missing.
    id = guid ?: "",
    username = username,
    password = password,
    hostname = origin,
+1 −0
Original line number Diff line number Diff line
@@ -183,6 +183,7 @@ class SyncableLoginsStorage(
     */
    @Throws(IdCollisionException::class, InvalidRecordException::class, LoginsStorageException::class)
    override suspend fun add(login: Login): String = withContext(coroutineContext) {
        check(login.guid == null) { "'guid' for a new login must be `null`" }
        conn.getStorage().add(login.toServerPassword())
    }