Skip to content

Commit

Permalink
Create a utility function to throw ValidationException
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamlooker committed Jul 8, 2024
1 parent 7ba8f8e commit 3dd16c0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package com.looker.droidify.service

import android.content.Context
import androidx.annotation.StringRes
import com.looker.core.common.R.string as strings
import com.looker.core.common.extension.calculateHash
import com.looker.core.common.extension.getPackageArchiveInfoCompat
import com.looker.core.common.extension.singleSignature
import com.looker.core.common.extension.versionCodeCompat
import com.looker.core.common.signature.FileValidator
import com.looker.core.common.signature.Hash
import com.looker.core.common.signature.ValidationException
import com.looker.core.common.signature.invalid
import com.looker.core.common.signature.verifyHash
import com.looker.core.domain.Release
import java.io.File
import com.looker.core.common.R.string as strings

class ReleaseFileValidator(
private val context: Context,
Expand All @@ -23,30 +23,28 @@ class ReleaseFileValidator(
override suspend fun validate(file: File) {
val hash = Hash(release.hashType, release.hash)
if (!file.verifyHash(hash)) {
throw ValidationException(
getString(strings.integrity_check_error_DESC)
)
invalid(getString(strings.integrity_check_error_DESC))
}
val packageInfo = context.packageManager.getPackageArchiveInfoCompat(file.path)
?: throw ValidationException(getString(strings.file_format_error_DESC))
?: invalid(getString(strings.file_format_error_DESC))
if (packageInfo.packageName != packageName ||
packageInfo.versionCodeCompat != release.versionCode
) {
throw ValidationException(getString(strings.invalid_metadata_error_DESC))
invalid(getString(strings.invalid_metadata_error_DESC))
}

packageInfo.singleSignature
?.calculateHash()
?.takeIf { it.isNotBlank() || it == release.signature }
?: throw ValidationException(getString(strings.invalid_signature_error_DESC))
?: invalid(getString(strings.invalid_signature_error_DESC))

packageInfo.permissions
?.asSequence()
.orEmpty()
.map { it.name }
.toSet()
.takeIf { release.permissions.containsAll(it) }
?: throw ValidationException(getString(strings.invalid_permissions_error_DESC))
?: invalid(getString(strings.invalid_permissions_error_DESC))
}

private fun getString(@StringRes id: Int): String = context.getString(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import java.io.File
interface FileValidator {

// Throws error if not valid
@Throws(ValidationException::class)
suspend fun validate(file: File)
}

class ValidationException(override val message: String) : Exception(message)

@Suppress("NOTHING_TO_INLINE")
inline fun invalid(message: Any): Nothing = throw ValidationException(message.toString())
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.looker.sync.fdroid
import com.looker.core.common.extension.certificate
import com.looker.core.common.extension.codeSigner
import com.looker.core.common.extension.fingerprint
import com.looker.core.common.signature.ValidationException
import com.looker.core.common.signature.invalid
import com.looker.core.domain.model.Fingerprint
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
Expand All @@ -24,17 +24,17 @@ class IndexJarValidator(
.fingerprint()
)
} catch (e: IllegalStateException) {
throw ValidationException(e.message ?: "Unknown Exception")
invalid(e.message ?: "Unknown Exception")
} catch (e: IllegalArgumentException) {
throw ValidationException(e.message ?: "Error creating Fingerprint object")
invalid(e.message ?: "Error creating Fingerprint object")
}
if (expectedFingerprint == null) {
fingerprint
} else {
if (expectedFingerprint.check(fingerprint)) {
expectedFingerprint
} else {
throw ValidationException(
invalid(
"Expected Fingerprint: ${expectedFingerprint}, " +
"Acquired Fingerprint: $fingerprint"
)
Expand Down

0 comments on commit 3dd16c0

Please sign in to comment.