1
0
Форкнуть 0
This commit is contained in:
Andreas Mikolajewski 2021-04-29 03:24:49 +02:00
Родитель 06670f95e2
Коммит 082bee09b4
3 изменённых файлов: 31 добавлений и 32 удалений

Просмотреть файл

@ -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.MicrosoftBackupSerializer
import com.microsoft.did.sdk.datasource.file.models.EncryptedBackupData 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.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.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.PasswordEncryptedBackupData
import com.microsoft.did.sdk.datasource.file.models.PasswordProtectedBackupData 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.UnprotectedBackup
import com.microsoft.did.sdk.datasource.file.models.UnprotectedBackupData 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.Result
import com.microsoft.did.sdk.util.controlflow.UnknownBackupFormatException import com.microsoft.did.sdk.util.controlflow.UnknownBackupFormatException
import com.microsoft.did.sdk.util.controlflow.UnknownProtectionMethodException 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 com.microsoft.did.sdk.util.controlflow.runResultTry
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.io.OutputStream
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -32,29 +28,32 @@ class BackupAndRestoreService @Inject constructor(
private val microsoftBackupSerializer: MicrosoftBackupSerializer, private val microsoftBackupSerializer: MicrosoftBackupSerializer,
private val serializer: Json private val serializer: Json
) { ) {
suspend fun createBackup(options: ProtectedBackupData): Result<JweProtectedBackup> { suspend fun createBackup(protectedBackupData: ProtectedBackupData): Result<JweProtectedBackup> {
return when (options) { return runResultTry {
is PasswordProtectedBackupData -> { val unprotectedBackupData = unwrapProtection(protectedBackupData)
return createUnprotectedBackup(options.unprotectedBackup).andThen { val unprotectedBackup = createUnprotectedBackup(unprotectedBackupData)
backup -> Result.Success(jweBackupFactory.createPasswordBackup(backup, options.password)) Result.Success(protectBackup(unprotectedBackup, protectedBackupData))
}
}
else -> {
Result.Failure(UnknownProtectionMethodException("Unknown protection options: ${options::class.qualifiedName}"));
}
} }
} }
private suspend fun createUnprotectedBackup(options: UnprotectedBackupData): Result<UnprotectedBackup> { private fun unwrapProtection(protectedBackupData: ProtectedBackupData): UnprotectedBackupData {
return runResultTry { return when (protectedBackupData) {
when (options) { is PasswordProtectedBackupData -> protectedBackupData.unprotectedBackup
is MicrosoftBackup2020Data -> { else -> throw UnknownProtectionMethodException("Unknown protection options: ${protectedBackupData::class.qualifiedName}")
Result.Success(microsoftBackupSerializer.create(options)) }
} }
else -> {
throw UnknownBackupFormatException("Unknown backup options: ${options::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}")
} }
} }

Просмотреть файл

@ -24,11 +24,11 @@ class MicrosoftBackupSerializer @Inject constructor(
) { ) {
suspend fun create(backupData: MicrosoftBackup2020Data): MicrosoftUnprotectedBackup2020 { suspend fun create(backupData: MicrosoftBackup2020Data): MicrosoftUnprotectedBackup2020 {
val vcMap = mutableMapOf<String, String>(); val vcMap = mutableMapOf<String, String>()
val vcMetaMap = mutableMapOf<String, VCMetadata>(); val vcMetaMap = mutableMapOf<String, VCMetadata>()
backupData.verifiableCredentials.forEach { verifiableCredentailMetadataPair -> backupData.verifiableCredentials.forEach { verifiableCredentialMetadataPair ->
vcMap.put(verifiableCredentailMetadataPair.first.jti, verifiableCredentailMetadataPair.first.raw); vcMap[verifiableCredentialMetadataPair.first.jti] = verifiableCredentialMetadataPair.first.raw;
vcMetaMap.put(verifiableCredentailMetadataPair.first.jti, verifiableCredentailMetadataPair.second); vcMetaMap[verifiableCredentialMetadataPair.first.jti] = verifiableCredentialMetadataPair.second;
} }
return MicrosoftUnprotectedBackup2020( return MicrosoftUnprotectedBackup2020(
vcs = vcMap, vcs = vcMap,

Просмотреть файл

@ -6,4 +6,4 @@ import kotlinx.serialization.Serializable
@Serializable @Serializable
@SerialName("BaseVC") @SerialName("BaseVC")
open class VCMetadata(val displayContract: DisplayContract) open class VCMetadata(open val displayContract: DisplayContract)