diff --git a/sdk/src/main/java/com/microsoft/did/sdk/BackupAndRestoreService.kt b/sdk/src/main/java/com/microsoft/did/sdk/BackupAndRestoreService.kt index 8c60e34f..979ec338 100644 --- a/sdk/src/main/java/com/microsoft/did/sdk/BackupAndRestoreService.kt +++ b/sdk/src/main/java/com/microsoft/did/sdk/BackupAndRestoreService.kt @@ -6,23 +6,19 @@ import com.microsoft.did.sdk.datasource.file.JweProtectedBackupFactory import com.microsoft.did.sdk.datasource.file.MicrosoftBackupSerializer import com.microsoft.did.sdk.datasource.file.models.EncryptedBackupData import com.microsoft.did.sdk.datasource.file.models.JweProtectedBackup -import com.microsoft.did.sdk.datasource.file.models.ProtectedBackupData -import com.microsoft.did.sdk.datasource.file.models.MicrosoftUnprotectedBackup2020 import com.microsoft.did.sdk.datasource.file.models.MicrosoftBackup2020Data +import com.microsoft.did.sdk.datasource.file.models.MicrosoftUnprotectedBackup2020 import com.microsoft.did.sdk.datasource.file.models.PasswordEncryptedBackupData import com.microsoft.did.sdk.datasource.file.models.PasswordProtectedBackupData +import com.microsoft.did.sdk.datasource.file.models.ProtectedBackupData import com.microsoft.did.sdk.datasource.file.models.UnprotectedBackup import com.microsoft.did.sdk.datasource.file.models.UnprotectedBackupData -import com.microsoft.did.sdk.util.controlflow.IoFailureException 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.andThen import com.microsoft.did.sdk.util.controlflow.runResultTry import kotlinx.serialization.json.Json -import java.io.IOException import java.io.InputStream -import java.io.OutputStream import javax.inject.Inject import javax.inject.Singleton @@ -32,29 +28,32 @@ class BackupAndRestoreService @Inject constructor( private val microsoftBackupSerializer: MicrosoftBackupSerializer, private val serializer: Json ) { - suspend fun createBackup(options: ProtectedBackupData): Result { - return when (options) { - is PasswordProtectedBackupData -> { - return createUnprotectedBackup(options.unprotectedBackup).andThen { - backup -> Result.Success(jweBackupFactory.createPasswordBackup(backup, options.password)) - } - } - else -> { - Result.Failure(UnknownProtectionMethodException("Unknown protection options: ${options::class.qualifiedName}")); - } + suspend fun createBackup(protectedBackupData: ProtectedBackupData): Result { + return runResultTry { + val unprotectedBackupData = unwrapProtection(protectedBackupData) + val unprotectedBackup = createUnprotectedBackup(unprotectedBackupData) + Result.Success(protectBackup(unprotectedBackup, protectedBackupData)) } } - private suspend fun createUnprotectedBackup(options: UnprotectedBackupData): Result { - return runResultTry { - when (options) { - is MicrosoftBackup2020Data -> { - Result.Success(microsoftBackupSerializer.create(options)) - } - else -> { - throw UnknownBackupFormatException("Unknown backup options: ${options::class.qualifiedName}"); - } - } + private fun unwrapProtection(protectedBackupData: ProtectedBackupData): UnprotectedBackupData { + return when (protectedBackupData) { + is PasswordProtectedBackupData -> protectedBackupData.unprotectedBackup + else -> throw UnknownProtectionMethodException("Unknown protection options: ${protectedBackupData::class.qualifiedName}") + } + } + + private suspend fun createUnprotectedBackup(unprotectedBackupData: UnprotectedBackupData): UnprotectedBackup { + return when (unprotectedBackupData) { + is MicrosoftBackup2020Data -> microsoftBackupSerializer.create(unprotectedBackupData) + else -> throw UnknownBackupFormatException("Unknown backup options: ${unprotectedBackupData::class.qualifiedName}"); + } + } + + private fun protectBackup(unprotectedBackup: UnprotectedBackup, protectedBackupData: ProtectedBackupData): JweProtectedBackup { + return when (protectedBackupData) { + is PasswordProtectedBackupData -> jweBackupFactory.createPasswordBackup(unprotectedBackup, protectedBackupData.password) + else -> throw UnknownProtectionMethodException("Unknown protection options: ${protectedBackupData::class.qualifiedName}") } } diff --git a/sdk/src/main/java/com/microsoft/did/sdk/datasource/file/MicrosoftBackupSerializer.kt b/sdk/src/main/java/com/microsoft/did/sdk/datasource/file/MicrosoftBackupSerializer.kt index c5625798..8e4f11e8 100644 --- a/sdk/src/main/java/com/microsoft/did/sdk/datasource/file/MicrosoftBackupSerializer.kt +++ b/sdk/src/main/java/com/microsoft/did/sdk/datasource/file/MicrosoftBackupSerializer.kt @@ -24,11 +24,11 @@ class MicrosoftBackupSerializer @Inject constructor( ) { suspend fun create(backupData: MicrosoftBackup2020Data): MicrosoftUnprotectedBackup2020 { - val vcMap = mutableMapOf(); - val vcMetaMap = mutableMapOf(); - backupData.verifiableCredentials.forEach { verifiableCredentailMetadataPair -> - vcMap.put(verifiableCredentailMetadataPair.first.jti, verifiableCredentailMetadataPair.first.raw); - vcMetaMap.put(verifiableCredentailMetadataPair.first.jti, verifiableCredentailMetadataPair.second); + val vcMap = mutableMapOf() + val vcMetaMap = mutableMapOf() + backupData.verifiableCredentials.forEach { verifiableCredentialMetadataPair -> + vcMap[verifiableCredentialMetadataPair.first.jti] = verifiableCredentialMetadataPair.first.raw; + vcMetaMap[verifiableCredentialMetadataPair.first.jti] = verifiableCredentialMetadataPair.second; } return MicrosoftUnprotectedBackup2020( vcs = vcMap, diff --git a/sdk/src/main/java/com/microsoft/did/sdk/datasource/file/models/VCMetadata.kt b/sdk/src/main/java/com/microsoft/did/sdk/datasource/file/models/VCMetadata.kt index 74e25d2f..551b815e 100644 --- a/sdk/src/main/java/com/microsoft/did/sdk/datasource/file/models/VCMetadata.kt +++ b/sdk/src/main/java/com/microsoft/did/sdk/datasource/file/models/VCMetadata.kt @@ -6,4 +6,4 @@ import kotlinx.serialization.Serializable @Serializable @SerialName("BaseVC") -open class VCMetadata(val displayContract: DisplayContract) +open class VCMetadata(open val displayContract: DisplayContract)