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.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<JweProtectedBackup> {
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<JweProtectedBackup> {
return runResultTry {
val unprotectedBackupData = unwrapProtection(protectedBackupData)
val unprotectedBackup = createUnprotectedBackup(unprotectedBackupData)
Result.Success(protectBackup(unprotectedBackup, protectedBackupData))
}
}
private suspend fun createUnprotectedBackup(options: UnprotectedBackupData): Result<UnprotectedBackup> {
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}")
}
}

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

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

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

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