Commit 91f7bcbc authored by MozLando's avatar MozLando
Browse files

Merge #8252



8252: Fennec MP import API r=rocketsroger a=grigoryk

Exposes the logins importer functions for the purpose of building an MP import UI in Fenix. No test coverage for this class, but there's also no new logic being added, just delegation to already well-tested methods.

Obtain the profile necessary for this class via `FennecProfile.findDefault(context, crashReporter)`.
Co-authored-by: default avatarGrisha Kruglov <gkruglov@mozilla.com>
parents 49664757 e1f84e98
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.support.migration
import mozilla.components.service.sync.logins.ServerPassword
import mozilla.components.support.base.crash.CrashReporting
import mozilla.components.support.base.log.logger.Logger
/**
* Helper class that allows:
* - checking for presence of MP on the Fennec's logins database
* - reading MP-protected logins from Fennec's logins database
*/
class FennecLoginsMPImporter(
profile: FennecProfile
) {
private val logger = Logger("FennecLoginsMPImporter")
private val key4DbPath: String = "${profile.path}/key4.db"
private val signonsDbPath: String = "${profile.path}/signons.sqlite"
/**
* @return 'true' if MP is detected, 'false' otherwise (and in case of any errors).
*/
@Suppress("TooGenericExceptionCaught")
fun hasMasterPassword(): Boolean {
return try {
// MP is set if default password doesn't work.
!FennecLoginsMigration.isMasterPasswordValid(FennecLoginsMigration.DEFAULT_MASTER_PASSWORD, key4DbPath)
} catch (e: Exception) {
logger.error("Failed to check MP validity", e)
false
}
}
/**
* Checks if provided [password] is able to unlock MP-protected logins storage.
* @param password Password to check.
* @return 'true' if [password] is correct, 'false otherwise (and in case of any errors).
*/
@Suppress("TooGenericExceptionCaught")
fun checkPassword(password: String): Boolean {
return try {
FennecLoginsMigration.isMasterPasswordValid(password, key4DbPath)
} catch (e: Exception) {
logger.error("Failed to check passwor", e)
false
}
}
/**
* @param password An MP to use for decrypting the Fennec logins database.
* @param crashReporter [CrashReporting] instance for recording encountered exceptions.
* @return A list of [ServerPassword] records representing logins stored in Fennec.
*/
@Suppress("TooGenericExceptionCaught")
fun getLoginRecords(password: String, crashReporter: CrashReporting): List<ServerPassword> {
return try {
FennecLoginsMigration.getLogins(crashReporter, password, signonsDbPath, key4DbPath).records
} catch (e: Exception) {
logger.error("Failed to read protected logins", e)
listOf()
}
}
}
......@@ -207,7 +207,6 @@ internal object FennecLoginsMigration {
}
@Suppress("LongMethod")
@VisibleForTesting
internal fun getLogins(
crashReporter: CrashReporting,
masterPassword: String,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment