-
Notifications
You must be signed in to change notification settings - Fork 908
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add developer settings for importing passwords from Google Password M…
…anager
- Loading branch information
Showing
22 changed files
with
894 additions
and
236 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
.../autofill-impl/src/main/java/com/duckduckgo/autofill/impl/importing/CredentialImporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.autofill.impl.importing | ||
|
||
import android.os.Parcelable | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.autofill.api.domain.app.LoginCredentials | ||
import com.duckduckgo.autofill.impl.importing.CredentialImporter.ImportResult | ||
import com.duckduckgo.autofill.impl.importing.CredentialImporter.ImportResult.Finished | ||
import com.duckduckgo.autofill.impl.importing.CredentialImporter.ImportResult.InProgress | ||
import com.duckduckgo.autofill.impl.store.InternalAutofillStore | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import dagger.SingleInstanceIn | ||
import java.util.UUID | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlinx.parcelize.Parcelize | ||
|
||
interface CredentialImporter { | ||
suspend fun import(importList: List<LoginCredentials>, originalImportListSize: Int): String | ||
fun getImportStatus(jobId: String): Flow<ImportResult> | ||
|
||
sealed interface ImportResult : Parcelable { | ||
val jobId: String | ||
|
||
@Parcelize | ||
data class InProgress( | ||
val savedCredentialIds: List<Long>, | ||
val numberSkipped: Int, | ||
val originalImportListSize: Int, | ||
override val jobId: String, | ||
) : ImportResult | ||
|
||
@Parcelize | ||
data class Finished( | ||
val savedCredentialIds: List<Long>, | ||
val numberSkipped: Int, | ||
override val jobId: String, | ||
) : ImportResult | ||
} | ||
} | ||
|
||
@SingleInstanceIn(AppScope::class) | ||
@ContributesBinding(AppScope::class) | ||
class CredentialImporterImpl @Inject constructor( | ||
private val existingCredentialMatchDetector: ExistingCredentialMatchDetector, | ||
private val autofillStore: InternalAutofillStore, | ||
private val dispatchers: DispatcherProvider, | ||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope, | ||
) : CredentialImporter { | ||
|
||
private val _importStatus = MutableSharedFlow<ImportResult>(replay = 1) | ||
private val mutex = Mutex() | ||
|
||
override suspend fun import(importList: List<LoginCredentials>, originalImportListSize: Int): String { | ||
val jobId = UUID.randomUUID().toString() | ||
|
||
mutex.withLock { | ||
appCoroutineScope.launch(dispatchers.io()) { | ||
doImportCredentials(importList, originalImportListSize, jobId) | ||
} | ||
} | ||
|
||
return jobId | ||
} | ||
|
||
private suspend fun doImportCredentials( | ||
importList: List<LoginCredentials>, | ||
originalImportListSize: Int, | ||
jobId: String, | ||
) { | ||
val savedCredentialIds = mutableListOf<Long>() | ||
var skippedCredentials = originalImportListSize - importList.size | ||
|
||
_importStatus.emit(InProgress(savedCredentialIds, skippedCredentials, originalImportListSize, jobId)) | ||
|
||
importList.forEach { | ||
if (!existingCredentialMatchDetector.alreadyExists(it)) { | ||
val insertedId = autofillStore.saveCredentials(it.domain!!, it)?.id | ||
|
||
if (insertedId != null) { | ||
savedCredentialIds.add(insertedId) | ||
} | ||
} else { | ||
skippedCredentials++ | ||
} | ||
|
||
_importStatus.emit(InProgress(savedCredentialIds, skippedCredentials, originalImportListSize, jobId)) | ||
} | ||
|
||
_importStatus.emit(Finished(savedCredentialIds, skippedCredentials, jobId)) | ||
} | ||
|
||
override fun getImportStatus(jobId: String): Flow<ImportResult> { | ||
return _importStatus.filter { result -> | ||
result.jobId == jobId | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...ofill-impl/src/main/java/com/duckduckgo/autofill/impl/importing/CsvCredentialConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.autofill.impl.importing | ||
|
||
import android.net.Uri | ||
import android.os.Parcelable | ||
import com.duckduckgo.autofill.api.domain.app.LoginCredentials | ||
import com.duckduckgo.autofill.impl.importing.CsvCredentialConverter.CsvCredentialImportResult | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.parcelize.Parcelize | ||
|
||
interface CsvCredentialConverter { | ||
suspend fun readCsv(encodedBlob: String): CsvCredentialImportResult | ||
suspend fun readCsv(fileUri: Uri): CsvCredentialImportResult | ||
|
||
sealed interface CsvCredentialImportResult : Parcelable { | ||
|
||
@Parcelize | ||
data class Success(val numberCredentialsInSource: Int, val loginCredentialsToImport: List<LoginCredentials>) : CsvCredentialImportResult | ||
|
||
@Parcelize | ||
data object Error : CsvCredentialImportResult | ||
} | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class GooglePasswordManagerCsvCredentialConverter @Inject constructor( | ||
private val parser: CsvCredentialParser, | ||
private val fileReader: CsvFileReader, | ||
private val credentialValidator: ImportedCredentialValidator, | ||
private val domainNameNormalizer: DomainNameNormalizer, | ||
private val dispatchers: DispatcherProvider, | ||
private val blobDecoder: GooglePasswordBlobDecoder, | ||
) : CsvCredentialConverter { | ||
|
||
override suspend fun readCsv(encodedBlob: String): CsvCredentialImportResult { | ||
return kotlin.runCatching { | ||
withContext(dispatchers.io()) { | ||
val csv = blobDecoder.decode(encodedBlob) | ||
convertToLoginCredentials(csv) | ||
} | ||
}.getOrElse { CsvCredentialImportResult.Error } | ||
} | ||
|
||
override suspend fun readCsv(fileUri: Uri): CsvCredentialImportResult { | ||
return kotlin.runCatching { | ||
withContext(dispatchers.io()) { | ||
val csv = fileReader.readCsvFile(fileUri) | ||
convertToLoginCredentials(csv) | ||
} | ||
}.getOrElse { CsvCredentialImportResult.Error } | ||
} | ||
|
||
private suspend fun convertToLoginCredentials(csv: String): CsvCredentialImportResult { | ||
return when (val parseResult = parser.parseCsv(csv)) { | ||
is CsvCredentialParser.ParseResult.Success -> { | ||
val toImport = deduplicateAndCleanup(parseResult.credentials) | ||
CsvCredentialImportResult.Success(parseResult.credentials.size, toImport) | ||
} | ||
is CsvCredentialParser.ParseResult.Error -> CsvCredentialImportResult.Error | ||
} | ||
} | ||
|
||
private suspend fun deduplicateAndCleanup(allCredentials: List<LoginCredentials>): List<LoginCredentials> { | ||
val dedupedCredentials = allCredentials.distinct() | ||
val validCredentials = dedupedCredentials.filter { credentialValidator.isValid(it) } | ||
val normalizedDomains = domainNameNormalizer.normalizeDomains(validCredentials) | ||
return normalizedDomains | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.