Skip to content

Commit

Permalink
Add developer settings for importing passwords from Google Password M…
Browse files Browse the repository at this point in the history
…anager
  • Loading branch information
CDRussell committed Oct 22, 2024
1 parent 0f14463 commit 4232859
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ 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.CsvPasswordImporter.ParseResult
import com.duckduckgo.autofill.impl.importing.CsvPasswordImporter.ParseResult.Success
import com.duckduckgo.autofill.impl.importing.CsvPasswordImporter.CsvPasswordImportResult
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
Expand All @@ -29,15 +28,15 @@ import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize

interface CsvPasswordImporter {
suspend fun readCsv(blob: String): ParseResult
suspend fun readCsv(fileUri: Uri): ParseResult
suspend fun readCsv(blob: String): CsvPasswordImportResult
suspend fun readCsv(fileUri: Uri): CsvPasswordImportResult

sealed interface ParseResult : Parcelable {
sealed interface CsvPasswordImportResult : Parcelable {
@Parcelize
data class Success(val numberPasswordsInSource: Int, val loginCredentialsToImport: List<LoginCredentials>) : ParseResult
data class Success(val numberPasswordsInSource: Int, val loginCredentialsToImport: List<LoginCredentials>) : CsvPasswordImportResult

@Parcelize
data object Error : ParseResult
data object Error : CsvPasswordImportResult
}
}

Expand All @@ -51,30 +50,39 @@ class GooglePasswordManagerCsvPasswordImporter @Inject constructor(
private val blobDecoder: GooglePasswordBlobDecoder,
) : CsvPasswordImporter {

override suspend fun readCsv(blob: String): ParseResult {
override suspend fun readCsv(blob: String): CsvPasswordImportResult {
return kotlin.runCatching {
withContext(dispatchers.io()) {
val csv = blobDecoder.decode(blob)
convertToLoginCredentials(csv)
}
}.getOrElse { ParseResult.Error }
}.getOrElse { CsvPasswordImportResult.Error }
}

override suspend fun readCsv(fileUri: Uri): ParseResult {
override suspend fun readCsv(fileUri: Uri): CsvPasswordImportResult {
return kotlin.runCatching {
withContext(dispatchers.io()) {
val csv = fileReader.readCsvFile(fileUri)
convertToLoginCredentials(csv)
}
}.getOrElse { ParseResult.Error }
}.getOrElse { CsvPasswordImportResult.Error }
}

private suspend fun convertToLoginCredentials(csv: String): Success {
val allPasswords = parser.parseCsv(csv)
private suspend fun convertToLoginCredentials(csv: String): CsvPasswordImportResult {
return when (val parseResult = parser.parseCsv(csv)) {
is CsvPasswordParser.ParseResult.Success -> {
val toImport = deduplicateAndCleanup(parseResult.passwords)
CsvPasswordImportResult.Success(parseResult.passwords.size, toImport)
}
is CsvPasswordParser.ParseResult.Error -> CsvPasswordImportResult.Error
}
}

private suspend fun deduplicateAndCleanup(allPasswords: List<LoginCredentials>): List<LoginCredentials> {
val dedupedPasswords = allPasswords.distinct()
val validPasswords = filterValidPasswords(dedupedPasswords)
val normalizedDomains = domainNameNormalizer.normalizeDomains(validPasswords)
return Success(allPasswords.size, normalizedDomains)
return normalizedDomains
}

private fun filterValidPasswords(passwords: List<LoginCredentials>): List<LoginCredentials> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.duckduckgo.autofill.impl.importing

import com.duckduckgo.autofill.api.domain.app.LoginCredentials
import com.duckduckgo.autofill.impl.importing.CsvPasswordParser.ParseResult
import com.duckduckgo.autofill.impl.importing.CsvPasswordParser.ParseResult.Error
import com.duckduckgo.autofill.impl.importing.CsvPasswordParser.ParseResult.Success
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
Expand All @@ -27,27 +30,30 @@ import kotlinx.coroutines.withContext
import timber.log.Timber

interface CsvPasswordParser {
suspend fun parseCsv(csv: String): List<LoginCredentials>
suspend fun parseCsv(csv: String): ParseResult

sealed interface ParseResult {
data class Success(val passwords: List<LoginCredentials>) : ParseResult
data object Error : ParseResult
}
}

@ContributesBinding(AppScope::class)
class GooglePasswordManagerCsvPasswordParser @Inject constructor(
private val dispatchers: DispatcherProvider,
) : CsvPasswordParser {

// private val csvFormat by lazy {
// CSVFormat.Builder.create(CSVFormat.DEFAULT).build()
// }

override suspend fun parseCsv(csv: String): List<LoginCredentials> {
override suspend fun parseCsv(csv: String): ParseResult {
return kotlin.runCatching {
convertToPasswordList(csv).also {
val passwords = convertToPasswordList(csv).also {
Timber.i("Parsed CSV. Found %d passwords", it.size)
}
Success(passwords)
}.onFailure {
Timber.e("Failed to parse CSV: %s", it.message)
Error
}.getOrElse {
emptyList()
Error
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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.PasswordImporter.ImportResult
import com.duckduckgo.autofill.impl.importing.PasswordImporter.ImportResult.Finished
import com.duckduckgo.autofill.impl.importing.PasswordImporter.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 PasswordImporter {
suspend fun importPasswords(importList: List<LoginCredentials>): String
fun getImportStatus(jobId: String): Flow<ImportResult>

sealed interface ImportResult : Parcelable {

@Parcelize
data class InProgress(
val savedCredentialIds: List<Long>,
val duplicatedPasswords: List<LoginCredentials>,
val importListSize: Int,
val jobId: String,
) : ImportResult

@Parcelize
data class Finished(
val savedCredentialIds: List<Long>,
val duplicatedPasswords: List<LoginCredentials>,
val importListSize: Int,
val jobId: String,
) : ImportResult
}
}

@SingleInstanceIn(AppScope::class)
@ContributesBinding(AppScope::class)
class PasswordImporterImpl @Inject constructor(
private val existingPasswordMatchDetector: ExistingPasswordMatchDetector,
private val autofillStore: InternalAutofillStore,
private val dispatchers: DispatcherProvider,
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
) : PasswordImporter {

private val _importStatus = MutableSharedFlow<ImportResult>(replay = 1)
private val mutex = Mutex()

override suspend fun importPasswords(importList: List<LoginCredentials>): String {
val jobId = UUID.randomUUID().toString()

mutex.withLock {
appCoroutineScope.launch(dispatchers.io()) {
doImportPasswords(importList, jobId)
}
}

return jobId
}

private suspend fun doImportPasswords(
importList: List<LoginCredentials>,
jobId: String,
) {
val savedCredentialIds = mutableListOf<Long>()
val duplicatedPasswords = mutableListOf<LoginCredentials>()

_importStatus.emit(InProgress(savedCredentialIds, duplicatedPasswords, importList.size, jobId))

importList.forEach {
if (!existingPasswordMatchDetector.alreadyExists(it)) {
val insertedId = autofillStore.saveCredentials(it.domain!!, it)?.id

if (insertedId != null) {
savedCredentialIds.add(insertedId)
}
} else {
duplicatedPasswords.add(it)
}

_importStatus.emit(InProgress(savedCredentialIds, duplicatedPasswords, importList.size, jobId))
}

_importStatus.emit(Finished(savedCredentialIds, duplicatedPasswords, importList.size, jobId))
}

override fun getImportStatus(jobId: String): Flow<ImportResult> {
return _importStatus.filter { result ->
when (result) {
is InProgress -> result.jobId == jobId
is Finished -> result.jobId == jobId
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@

package com.duckduckgo.autofill.internal

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State.STARTED
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.duckduckgo.anvil.annotations.InjectWith
import com.duckduckgo.app.tabs.BrowserNav
import com.duckduckgo.autofill.api.AutofillFeature
import com.duckduckgo.autofill.api.AutofillScreens.AutofillSettingsScreen
import com.duckduckgo.autofill.api.AutofillSettingsLaunchSource.InternalDevSettings
Expand All @@ -33,6 +37,11 @@ import com.duckduckgo.autofill.api.email.EmailManager
import com.duckduckgo.autofill.impl.configuration.AutofillJavascriptEnvironmentConfiguration
import com.duckduckgo.autofill.impl.email.incontext.store.EmailProtectionInContextDataStore
import com.duckduckgo.autofill.impl.engagement.store.AutofillEngagementRepository
import com.duckduckgo.autofill.impl.importing.CsvPasswordImporter
import com.duckduckgo.autofill.impl.importing.CsvPasswordImporter.CsvPasswordImportResult
import com.duckduckgo.autofill.impl.importing.PasswordImporter
import com.duckduckgo.autofill.impl.importing.PasswordImporter.ImportResult.Finished
import com.duckduckgo.autofill.impl.importing.PasswordImporter.ImportResult.InProgress
import com.duckduckgo.autofill.impl.reporting.AutofillSiteBreakageReportingDataStore
import com.duckduckgo.autofill.impl.store.InternalAutofillStore
import com.duckduckgo.autofill.impl.store.NeverSavedSiteRepository
Expand All @@ -48,6 +57,7 @@ import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.ActivityScope
import com.duckduckgo.feature.toggles.api.Toggle
import com.duckduckgo.navigation.api.GlobalActivityStarter
import com.google.android.material.snackbar.Snackbar
import java.text.SimpleDateFormat
import javax.inject.Inject
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -75,6 +85,12 @@ class AutofillInternalSettingsActivity : DuckDuckGoActivity() {
@Inject
lateinit var autofillStore: InternalAutofillStore

@Inject
lateinit var passwordImporter: PasswordImporter

@Inject
lateinit var browserNav: BrowserNav

@Inject
lateinit var autofillPrefsStore: AutofillPrefsStore

Expand All @@ -101,6 +117,50 @@ class AutofillInternalSettingsActivity : DuckDuckGoActivity() {
@Inject
lateinit var reportBreakageDataStore: AutofillSiteBreakageReportingDataStore

@Inject
lateinit var csvPasswordImporter: CsvPasswordImporter

private val importCsvLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
val fileUrl = data?.data

logcat { "cdr onActivityResult for CSV file request. resultCode=${result.resultCode}. uri=$fileUrl" }
if (fileUrl != null) {
lifecycleScope.launch {
when (val parseResult = csvPasswordImporter.readCsv(fileUrl)) {
is CsvPasswordImportResult.Success -> {
val jobId = passwordImporter.importPasswords(parseResult.loginCredentialsToImport)
observePasswordInputUpdates(jobId)
}
is CsvPasswordImportResult.Error -> {
"Failed to import passwords due to an error".showSnackbar()
}
}
}
}
}
}

private fun observePasswordInputUpdates(jobId: String) {
lifecycleScope.launch {
repeatOnLifecycle(STARTED) {
passwordImporter.getImportStatus(jobId).collect {
when (it) {
is InProgress -> {
logcat { "cdr import status: $it" }
}

is Finished -> {
logcat { "cdr Imported ${it.savedCredentialIds.size} passwords" }
"Imported ${it.savedCredentialIds.size} passwords".showSnackbar()
}
}
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
Expand Down Expand Up @@ -168,6 +228,7 @@ class AutofillInternalSettingsActivity : DuckDuckGoActivity() {
configureEngagementEventHandlers()
configureReportBreakagesHandlers()
configureDeclineCounterHandlers()
configureImportPasswordsEventHandlers()
}

private fun configureReportBreakagesHandlers() {
Expand All @@ -179,6 +240,22 @@ class AutofillInternalSettingsActivity : DuckDuckGoActivity() {
}
}

@SuppressLint("QueryPermissionsNeeded")
private fun configureImportPasswordsEventHandlers() {
binding.importPasswordsLaunchGooglePasswordWebpage.setClickListener {
val googlePasswordsUrl = "https://passwords.google.com/options?ep=1"
startActivity(browserNav.openInNewTab(this, googlePasswordsUrl))
}

binding.importPasswordsImportCsv.setClickListener {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
}
importCsvLauncher.launch(intent)
}
}

private fun configureEngagementEventHandlers() {
binding.engagementClearEngagementHistoryButton.setOnClickListener {
lifecycleScope.launch(dispatchers.io()) {
Expand Down Expand Up @@ -443,6 +520,10 @@ class AutofillInternalSettingsActivity : DuckDuckGoActivity() {
}
}

private fun String.showSnackbar(duration: Int = Snackbar.LENGTH_LONG) {
Snackbar.make(binding.root, this, duration).show()
}

private fun Context.daysInstalledOverrideOptions(): List<Pair<String, Int>> {
return listOf(
Pair(getString(R.string.autofillDevSettingsOverrideMaxInstalledOptionNever), -1),
Expand Down
Loading

0 comments on commit 4232859

Please sign in to comment.