Simplyfiy backup service
This commit is contained in:
Родитель
87093dc671
Коммит
55cf4dcb14
|
@ -7,13 +7,12 @@ import com.microsoft.did.sdk.backup.content.microsoft2020.Microsoft2020BackupPro
|
|||
import com.microsoft.did.sdk.backup.content.ProtectedBackupData
|
||||
import com.microsoft.did.sdk.backup.content.microsoft2020.Microsoft2020Backup
|
||||
import com.microsoft.did.sdk.backup.content.microsoft2020.Microsoft2020UnprotectedBackupData
|
||||
import com.microsoft.did.sdk.backup.container.jwe.JwePasswordProtectionMethod
|
||||
import com.microsoft.did.sdk.backup.container.ProtectionMethod
|
||||
import com.microsoft.did.sdk.backup.content.UnprotectedBackupData
|
||||
import com.microsoft.did.sdk.backup.UnprotectedBackup
|
||||
import com.microsoft.did.sdk.backup.content.BackupProcessorFactory
|
||||
import com.microsoft.did.sdk.util.controlflow.Result
|
||||
import com.microsoft.did.sdk.util.controlflow.UnknownBackupFormatException
|
||||
import com.microsoft.did.sdk.util.controlflow.UnknownProtectionMethodException
|
||||
import com.microsoft.did.sdk.util.controlflow.runResultTry
|
||||
import kotlinx.serialization.json.Json
|
||||
import javax.inject.Inject
|
||||
|
@ -22,7 +21,7 @@ import javax.inject.Singleton
|
|||
@Singleton
|
||||
class BackupAndRestoreService @Inject constructor(
|
||||
private val backupParser: BackupParser,
|
||||
private val microsoft2020BackupProcessor: Microsoft2020BackupProcessor,
|
||||
private val backupProcessorFactory: BackupProcessorFactory,
|
||||
private val serializer: Json
|
||||
) {
|
||||
|
||||
|
@ -39,19 +38,12 @@ class BackupAndRestoreService @Inject constructor(
|
|||
*/
|
||||
suspend fun createBackup(unprotectedBackup: UnprotectedBackup, protectionMethod: ProtectionMethod): Result<ProtectedBackupData> {
|
||||
return runResultTry {
|
||||
val unprotectedBackupData = transformToBackupData(unprotectedBackup)
|
||||
val unprotectedBackupData = backupProcessorFactory.export(unprotectedBackup)
|
||||
val protectedBackup = protectionMethod.wrap(unprotectedBackupData, serializer)
|
||||
Result.Success(protectedBackup)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun transformToBackupData(unprotectedBackup: UnprotectedBackup): UnprotectedBackupData {
|
||||
return when (unprotectedBackup) {
|
||||
is Microsoft2020Backup -> microsoft2020BackupProcessor.transformToBackupData(unprotectedBackup)
|
||||
else -> throw UnknownBackupFormatException("Unknown backup options: ${unprotectedBackup::class.qualifiedName}")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a serialized backup tries to determine the backup type and deserialize it.
|
||||
*
|
||||
|
@ -77,15 +69,8 @@ class BackupAndRestoreService @Inject constructor(
|
|||
suspend fun restoreBackup(protectedBackupData: ProtectedBackupData, protectionMethod: ProtectionMethod): Result<UnprotectedBackup> {
|
||||
return runResultTry {
|
||||
val unprotectedBackup = protectionMethod.unwrap(protectedBackupData, serializer)
|
||||
val unprotectedBackupData = importBackup(unprotectedBackup)
|
||||
val unprotectedBackupData = backupProcessorFactory.import(unprotectedBackup)
|
||||
Result.Success(unprotectedBackupData)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun importBackup(backupData: UnprotectedBackupData): UnprotectedBackup {
|
||||
return when (backupData) {
|
||||
is Microsoft2020UnprotectedBackupData -> microsoft2020BackupProcessor.import(backupData)
|
||||
else -> throw UnknownBackupFormatException("Unknown restore file: ${backupData::class.qualifiedName}")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved
|
||||
|
||||
package com.microsoft.did.sdk.backup.content
|
||||
|
||||
import com.microsoft.did.sdk.backup.UnprotectedBackup
|
||||
|
||||
interface BackupProcessor {
|
||||
suspend fun export(backup: UnprotectedBackup): UnprotectedBackupData
|
||||
|
||||
suspend fun import(backupData: UnprotectedBackupData): UnprotectedBackup
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved
|
||||
|
||||
package com.microsoft.did.sdk.backup.content
|
||||
|
||||
import com.microsoft.did.sdk.backup.UnprotectedBackup
|
||||
import com.microsoft.did.sdk.backup.content.microsoft2020.Microsoft2020Backup
|
||||
import com.microsoft.did.sdk.backup.content.microsoft2020.Microsoft2020BackupProcessor
|
||||
import com.microsoft.did.sdk.backup.content.microsoft2020.Microsoft2020UnprotectedBackupData
|
||||
import com.microsoft.did.sdk.util.controlflow.UnknownBackupFormatException
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class BackupProcessorFactory @Inject constructor(
|
||||
private val microsoft2020BackupProcessor: Microsoft2020BackupProcessor
|
||||
): BackupProcessor {
|
||||
|
||||
private fun getProcessor(unprotectedBackup: UnprotectedBackup): BackupProcessor {
|
||||
return when (unprotectedBackup) {
|
||||
is Microsoft2020Backup -> microsoft2020BackupProcessor
|
||||
else -> throw UnknownBackupFormatException("Unknown backup options: ${unprotectedBackup::class.qualifiedName}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun getProcessor(backupData: UnprotectedBackupData): BackupProcessor {
|
||||
return when (backupData) {
|
||||
is Microsoft2020UnprotectedBackupData -> microsoft2020BackupProcessor
|
||||
else -> throw UnknownBackupFormatException("Unknown restore file: ${backupData::class.qualifiedName}")
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun export(backup: UnprotectedBackup): UnprotectedBackupData {
|
||||
return getProcessor(backup).export(backup)
|
||||
}
|
||||
|
||||
override suspend fun import(backupData: UnprotectedBackupData): UnprotectedBackup {
|
||||
return getProcessor(backupData).import(backupData)
|
||||
}
|
||||
}
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
package com.microsoft.did.sdk.backup.content.microsoft2020
|
||||
|
||||
import com.microsoft.did.sdk.backup.UnprotectedBackup
|
||||
import com.microsoft.did.sdk.backup.content.BackupProcessor
|
||||
import com.microsoft.did.sdk.backup.content.UnprotectedBackupData
|
||||
import com.microsoft.did.sdk.credential.models.VerifiableCredential
|
||||
import com.microsoft.did.sdk.credential.models.VerifiableCredentialContent
|
||||
import com.microsoft.did.sdk.crypto.keyStore.EncryptedKeyStore
|
||||
|
@ -20,9 +23,10 @@ class Microsoft2020BackupProcessor @Inject constructor(
|
|||
private val keyStore: EncryptedKeyStore,
|
||||
private val rawIdentifierConverter: RawIdentifierConverter,
|
||||
private val jsonSerializer: Json
|
||||
) {
|
||||
) : BackupProcessor {
|
||||
|
||||
suspend fun transformToBackupData(backup: Microsoft2020Backup): Microsoft2020UnprotectedBackupData {
|
||||
override suspend fun export(backup: UnprotectedBackup): UnprotectedBackupData {
|
||||
if (backup !is Microsoft2020Backup) throw BackupRestoreException("Backup has wrong type ${backup::class.simpleName}")
|
||||
val vcMap = mutableMapOf<String, String>()
|
||||
val vcMetaMap = mutableMapOf<String, VcMetadata>()
|
||||
backup.verifiableCredentials.forEach { verifiableCredentialMetadataPair ->
|
||||
|
@ -37,11 +41,12 @@ class Microsoft2020BackupProcessor @Inject constructor(
|
|||
)
|
||||
}
|
||||
|
||||
suspend fun import(backup: Microsoft2020UnprotectedBackupData): Microsoft2020Backup {
|
||||
override suspend fun import(backupData: UnprotectedBackupData): UnprotectedBackup {
|
||||
if (backupData !is Microsoft2020UnprotectedBackupData) throw BackupRestoreException("BackupData has wrong type ${backupData::class.simpleName}")
|
||||
val identifiers = mutableListOf<Identifier>()
|
||||
var keySet = setOf<JWK>()
|
||||
|
||||
backup.identifiers.forEach { raw ->
|
||||
backupData.identifiers.forEach { raw ->
|
||||
val pair = rawIdentifierConverter.parseRawIdentifier(raw)
|
||||
identifiers.add(pair.first)
|
||||
keySet = keySet.union(pair.second)
|
||||
|
@ -51,8 +56,8 @@ class Microsoft2020BackupProcessor @Inject constructor(
|
|||
identifiers.forEach { id -> identityRepository.insert(id) }
|
||||
|
||||
return Microsoft2020Backup(
|
||||
walletMetadata = backup.metaInf,
|
||||
verifiableCredentials = transformVcs(backup)
|
||||
walletMetadata = backupData.metaInf,
|
||||
verifiableCredentials = transformVcs(backupData)
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ class Microsoft2020BackupProcessorTest {
|
|||
@Test
|
||||
fun `transformToBackupData transforms backup correctly`() {
|
||||
runBlocking {
|
||||
val actual = backupProcessor.transformToBackupData(backupData)
|
||||
val actual = backupProcessor.export(backupData)
|
||||
coVerify {
|
||||
identifierRepository.queryAllLocal()
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче