Repository: corona-warn-app/cwa-app-android
File: Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ccl/configuration/server/CCLConfigurationServer.kt (Kotlin)

Code before:
@Reusable
class CCLConfigurationServer @Inject constructor(
    private val cclConfigurationApiLazy: Lazy<CCLConfigurationApiV1>,
    private val dispatcherProvider: DispatcherProvider,
    private val signatureValidation: SignatureValidation,
) {

    private val cclConfigurationApi
        get() = cclConfigurationApiLazy.get()

    suspend fun getCCLConfiguration(): ByteArray? = withContext(dispatcherProvider.IO) {
        Timber.tag(TAG).d("getCCLConfiguration()")
        try {
            val response = cclConfigurationApi.getCCLConfiguration()
            when (response.wasModified) {
                true -> response.parseAndValidate()
                false -> {
                    Timber.tag(TAG).d("CCL Configuration was not modified")
                    null
                }
            }
        } catch (e: Exception) {
            Timber.tag(TAG).e(e, "Failed to get ccl configuration")
            throw e
        }
    }.also { Timber.tag(TAG).d("Returning %s", it) }

    private fun Response<ResponseBody>.parseAndValidate(): ByteArray {
        if (!isSuccessful) throw HttpException(this)

        val body = requireNotNull(body()) { "Body of response was null" }
        val fileMap = body.byteStream().unzip().readIntoMap()

        val exportBinary = fileMap[EXPORT_BINARY_FILE_NAME]
        val exportSignature = fileMap[EXPORT_SIGNATURE_FILE_NAME]

        if (exportBinary == null || exportSignature == null)
            throw CCLConfigurationInvalidSignatureException(msg = "Unknown files ${fileMap.entries}")

        val hasValidSignature = signatureValidation.hasValidSignature(
            toVerify = exportBinary,
            signatureList = SignatureValidation.parseTEKStyleSignature(exportSignature)
        )

        if (!hasValidSignature)
            throw CCLConfigurationInvalidSignatureException(msg = "Signature of ccl configuration did not match")

        return exportBinary
    }

    companion object {

Review comment (suggestion):
Using `ByteArray` parameter in the log seems to be useless as it's not translated into a string. It could be removed, not necessary in this PR, as this is about something else.

Code after:
@Reusable
class CCLConfigurationServer @Inject constructor(
    private val cclConfigurationApiLazy: Lazy<CCLConfigurationApiV1>,
    private val dispatcherProvider: DispatcherProvider,
    private val signatureValidation: SignatureValidation,
) {

    private val cclConfigurationApi
        get() = cclConfigurationApiLazy.get()

    suspend fun getCCLConfiguration(): ByteArray? = withContext(dispatcherProvider.IO) {
        Timber.tag(TAG).d("getCCLConfiguration()")
        try {
            val response = cclConfigurationApi.getCCLConfiguration()
            when (response.wasModified) {
                true -> response.parseAndValidate()
                false -> {
                    Timber.tag(TAG).d("CCL Configuration was not modified")
                    null
                }
            }
        } catch (e: Exception) {
            Timber.tag(TAG).e(e, "Failed to get ccl configuration")
            throw e
        }
    }

    private fun Response<ResponseBody>.parseAndValidate(): ByteArray {
        if (!isSuccessful) throw HttpException(this)

        val body = requireNotNull(body()) { "Body of response was null" }
        val fileMap = body.byteStream().unzip().readIntoMap()

        val exportBinary = fileMap[EXPORT_BINARY_FILE_NAME]
        val exportSignature = fileMap[EXPORT_SIGNATURE_FILE_NAME]

        if (exportBinary == null || exportSignature == null)
            throw CCLConfigurationInvalidSignatureException(msg = "Unknown files ${fileMap.entries}")

        val hasValidSignature = signatureValidation.hasValidSignature(
            toVerify = exportBinary,
            signatureList = SignatureValidation.parseTEKStyleSignature(exportSignature)
        )

        if (!hasValidSignature)
            throw CCLConfigurationInvalidSignatureException(msg = "Signature of ccl configuration did not match")

        return exportBinary
    }

    companion object {