Skip to content

Commit

Permalink
Bug/headers modified (eu-digital-green-certificates#299)
Browse files Browse the repository at this point in the history
* - add new header if-modified-since to handle refresh after 304 error;

* - update version;

* - update test;
  • Loading branch information
MykhailoNester authored Apr 1, 2022
1 parent 4fcb64c commit ad25372
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fun DccRevocationPartitionLocal.fromLocal(): DccRevocationPartition {
x = x,
y = y,
expires = expires,
lastUpdated = lastUpdated,
chunks = chunks
)
}
Expand All @@ -69,6 +70,7 @@ fun DccRevocationPartition.toLocal(): DccRevocationPartitionLocal {
x = x,
y = y,
expires = expires,
lastUpdated = lastUpdated,
chunks = chunks
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ data class DccRevocationPartitionLocal(
val x: Char?,
val y: Char?,
val expires: ZonedDateTime,
val lastUpdated: ZonedDateTime,
val chunks: String
)

This file was deleted.

4 changes: 2 additions & 2 deletions buildSrc/src/main/java/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ object Config {
const val targetSdk = 30
val javaVersion = JavaVersion.VERSION_1_8

const val versionCode = 45
const val versionName = "1.3.0"
const val versionCode = 46
const val versionName = "1.3.1"

const val androidTestInstrumentation = "androidx.test.runner.AndroidJUnitRunner"
const val proguardConsumerRules = "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface RevocationService {
@GET("/lists/{kid}/partitions")
suspend fun getRevocationListPartitions(
@Header("if-Match") eTag: String,
@Header("if-modified-since") modifiedSince: String?,
@Header("X-SLICE-FILTER-TYPE") type: SliceType,
@Path("kid") kid: String
): Response<List<RevocationPartitionResponse>>
Expand All @@ -55,6 +56,7 @@ interface RevocationService {
@GET("/lists/{kid}/partitions/{id}/chunks/{cid}/slices")
suspend fun getRevocationChunk(
@Header("If-Match") eTag: String,
@Header("if-modified-since") modifiedSince: String,
@Header("X-SLICE-FILTER-TYPE") type: SliceType,
@Path("kid") kid: String,
@Path("id") id: String,
Expand All @@ -64,6 +66,7 @@ interface RevocationService {
@POST("/lists/{kid}/partitions/{id}/chunks/{cid}/slices")
suspend fun getRevocationChunkSlices(
@Header("if-Match") eTag: String,
@Header("if-modified-since") modifiedSince: String,
@Header("X-SLICE-FILTER-TYPE") type: SliceType,
@Path("kid") kid: String,
@Path("id") partitionId: String,
Expand All @@ -74,6 +77,7 @@ interface RevocationService {
@GET("/lists/{kid}/partitions/{id}/chunks/{cid}/slices/{sid}")
suspend fun getRevocationChunkSlice(
@Header("If-Match") eTag: String,
@Header("if-modified-since") modifiedSince: String,
@Header("X-SLICE-FILTER-TYPE") type: SliceType,
@Path("kid") kid: String,
@Path("id") id: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ data class RevocationPartitionResponse(
@JsonProperty("y")
val y: Char?,

@JsonProperty("lastUpdated")
val lastUpdated: ZonedDateTime,

@JsonProperty("expired")
val expires: ZonedDateTime,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,32 @@ interface RevocationRepository {
suspend fun getRevocationLists(): List<RevocationKidData>?

@Throws(Exception::class)
suspend fun getRevocationPartitions(sliceType: SliceType, kid: String): List<RevocationPartitionResponse>?
suspend fun getRevocationPartitions(
lastUpdated: String?,
sliceType: SliceType,
kid: String,
): List<RevocationPartitionResponse>?

@Throws(Exception::class)
suspend fun getPartitionChunks(sliceType: SliceType, kid: String, partitionId: String?, cidList: List<String>): ResponseBody?
suspend fun getPartitionChunks(
sliceType: SliceType,
kid: String,
partitionId: String?,
cidList: List<String>
): ResponseBody?

@Throws(Exception::class)
suspend fun getRevocationChunk(sliceType: SliceType, kid: String, id: String?, chunkId: String): ResponseBody?
suspend fun getRevocationChunk(
lastUpdated: String,
sliceType: SliceType,
kid: String,
id: String?,
chunkId: String
): ResponseBody?

@Throws(Exception::class)
suspend fun getRevocationChunkSlices(
lastUpdated: String,
sliceType: SliceType,
kid: String,
partitionId: String?,
Expand All @@ -54,7 +70,14 @@ interface RevocationRepository {
): ResponseBody?

@Throws(Exception::class)
suspend fun getSlice(sliceType: SliceType, kid: String, partitionId: String?, cid: String, sid: String): ResponseBody?
suspend fun getSlice(
lastUpdated: String,
sliceType: SliceType,
kid: String,
partitionId: String?,
cid: String,
sid: String
): ResponseBody?

suspend fun getMetadataByKid(kid: String): DccRevocationKidMetadata?

Expand Down
6 changes: 5 additions & 1 deletion revocation/src/main/java/dcc/app/revocation/domain/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import android.util.Base64
import com.upokecenter.cbor.CBORObject
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.*

const val ECDSA_256 = -7
Expand Down Expand Up @@ -83,4 +85,6 @@ fun String.hexToByteArray(): ByteArray = chunked(2)
.toByteArray()

fun String.toBase64Url(): String =
Base64.encodeToString(Base64.decode(this, Base64.DEFAULT), Base64.URL_SAFE or Base64.NO_PADDING or Base64.NO_WRAP)
Base64.encodeToString(Base64.decode(this, Base64.DEFAULT), Base64.URL_SAFE or Base64.NO_PADDING or Base64.NO_WRAP)

fun ZonedDateTime.toUtcString(): String = this.withZoneSameInstant(ZoneOffset.UTC).toString()
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ data class DccRevocationPartition(
val x: Char?,
val y: Char?,
val expires: ZonedDateTime,
val lastUpdated: ZonedDateTime,
val chunks: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import dcc.app.revocation.domain.model.DccRevocationPartition
import dcc.app.revocation.domain.model.DccRevocationSlice
import dcc.app.revocation.domain.model.RevocationKidData
import dcc.app.revocation.domain.toBase64Url
import dcc.app.revocation.domain.toUtcString
import dcc.app.revocation.isEqualTo
import kotlinx.coroutines.CoroutineDispatcher
import okhttp3.ResponseBody
Expand Down Expand Up @@ -95,8 +96,9 @@ class GetRevocationDataUseCase @Inject constructor(

// Check the last modified date for each kid. If the last date per kid != received date,
// call for the kid /{kid}/partitions to receive the metadata objects. If last date == received date, do nothing.
if (metadataLocal.lastUpdated != revocationKidData.lastUpdated) {
getPartitions(kid)
val lastUpdated = metadataLocal.lastUpdated.toUtcString()
if (lastUpdated != revocationKidData.lastUpdated.toUtcString()) {
getPartitions(kid, lastUpdated)
}
}

Expand All @@ -112,9 +114,13 @@ class GetRevocationDataUseCase @Inject constructor(
)
}

private suspend fun getPartitions(kid: String) {
private suspend fun getPartitions(kid: String, lastUpdated: String? = null) {
val kidUrlSafe = kid.toBase64Url()
repository.getRevocationPartitions(sliceType, kidUrlSafe)?.forEach { partition ->
repository.getRevocationPartitions(
sliceType = sliceType,
kid = kidUrlSafe,
lastUpdated = lastUpdated
)?.forEach { partition ->
handlePartition(kid, partition)
}
}
Expand All @@ -123,15 +129,17 @@ class GetRevocationDataUseCase @Inject constructor(
val localPartition = repository.getLocalRevocationPartition(remotePartition.id, kid)
if (localPartition != null) {
// Optimization - section/chunk validation what changed
compareChunksWithLocal(kid, localPartition, remotePartition)
val lastUpdated = localPartition.lastUpdated.toUtcString()
compareChunksWithLocal(kid, localPartition, remotePartition, lastUpdated)
} else {
// Initial sync. load all chunks for partition
val result =
repository.getPartitionChunks(
sliceType = sliceType,
kid = kid.toBase64Url(),
partitionId = remotePartition.id,
cidList = remotePartition.chunks.map { it.key })
cidList = remotePartition.chunks.map { it.key }
)
handlePartitionSlices(kid, remotePartition, result)
}

Expand All @@ -147,7 +155,8 @@ class GetRevocationDataUseCase @Inject constructor(
private suspend fun compareChunksWithLocal(
kid: String,
localPartition: DccRevocationPartition,
remotePartition: RevocationPartitionResponse
remotePartition: RevocationPartitionResponse,
lastUpdated: String
) {
val type: Type = object : TypeToken<Map<String, Map<String, Slice>>>() {}.type
val localChunks =
Expand All @@ -158,6 +167,7 @@ class GetRevocationDataUseCase @Inject constructor(
if (localSlices == null) {
// When chunk not found load from api
val response = repository.getRevocationChunk(
lastUpdated = lastUpdated,
sliceType = sliceType,
kid = kid.toBase64Url(),
id = remotePartition.id,
Expand All @@ -180,6 +190,7 @@ class GetRevocationDataUseCase @Inject constructor(
if (slices.size < remoteChunkValue.size / 2) {
// Load updated or missing slices by sid list.
val response = repository.getRevocationChunkSlices(
lastUpdated = lastUpdated,
sliceType = sliceType,
kid = kid.toBase64Url(),
partitionId = remotePartition.id,
Expand All @@ -189,6 +200,7 @@ class GetRevocationDataUseCase @Inject constructor(
handlePartitionSlices(kid, remotePartition, response)
} else {
val response = repository.getRevocationChunk(
lastUpdated = lastUpdated,
sliceType = sliceType,
kid = kid.toBase64Url(),
id = remotePartition.id,
Expand All @@ -208,6 +220,7 @@ class GetRevocationDataUseCase @Inject constructor(
x = partition.x,
y = partition.y,
expires = partition.expires,
lastUpdated = partition.lastUpdated,
chunks = Gson().toJson(partition.chunks)
)
)
Expand Down
Loading

0 comments on commit ad25372

Please sign in to comment.