Skip to content

Commit

Permalink
refactor(AppIntegrity): Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Dec 13, 2024
1 parent ee91dc0 commit 1cfb281
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AppIntegrityManager(private val appContext: Context) {
private var challenge = ""
private var challengeId = ""

fun warmUpTokenProvider(appCloudNumber: Long, onFailure: (Throwable) -> Unit) {
fun warmUpTokenProvider(appCloudNumber: Long, onFailure: () -> Unit) {
val integrityManager = IntegrityManagerFactory.createStandard(appContext)
integrityManager.prepareIntegrityToken(
PrepareIntegrityTokenRequest.builder().setCloudProjectNumber(appCloudNumber).build()
Expand All @@ -51,7 +51,7 @@ class AppIntegrityManager(private val appContext: Context) {
fun requestIntegrityVerdictToken(
requestHash: String,
onSuccess: (String) -> Unit,
onFailure: (Throwable?) -> Unit,
onFailure: () -> Unit,
onNullTokenProvider: (String) -> Unit,
) {
if (appIntegrityTokenProvider == null) {
Expand All @@ -63,7 +63,7 @@ class AppIntegrityManager(private val appContext: Context) {
}
}

fun requestClassicIntegrityVerdictToken(onSuccess: (String) -> Unit, onFailure: (Throwable?) -> Unit) {
fun requestClassicIntegrityVerdictToken(onSuccess: (String) -> Unit, onFailure: () -> Unit) {
val nonce = Base64.encodeToString(challenge.toByteArray(), Base64.DEFAULT)

classicIntegrityTokenProvider.requestIntegrityToken(IntegrityTokenRequest.builder().setNonce(nonce).build())
Expand All @@ -76,7 +76,7 @@ class AppIntegrityManager(private val appContext: Context) {
packageName: String,
targetUrl: String,
onSuccess: (String) -> Unit,
onFailure: (Throwable) -> Unit,
onFailure: () -> Unit,
) {
runCatching {
val apiResponse = appIntegrityRepository.getJwtToken(
Expand All @@ -91,7 +91,7 @@ class AppIntegrityManager(private val appContext: Context) {
}
}

suspend fun getChallenge(onSuccess: () -> Unit, onFailure: (Throwable) -> Unit) = runCatching {
suspend fun getChallenge(onSuccess: () -> Unit, onFailure: () -> Unit) = runCatching {
generateChallengeId()
val apiResponse = appIntegrityRepository.getChallenge(challengeId)
SentryLog.d(
Expand Down Expand Up @@ -125,15 +125,15 @@ class AppIntegrityManager(private val appContext: Context) {
challengeId = UUID.randomUUID().toString()
}

private fun manageException(exception: Throwable, errorMessage: String, onFailure: (Throwable) -> Unit) {
private fun manageException(exception: Throwable, errorMessage: String, onFailure: () -> Unit) {
if (exception !is NetworkException) {
Sentry.captureMessage(errorMessage, SentryLevel.ERROR) { scope ->
scope.setTag("exception", exception.message.toString())
scope.setExtra("stacktrace", exception.printStackTrace().toString())
}
}
exception.printStackTrace()
onFailure(exception)
onFailure()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal class AppIntegrityRepository {
return post<ApiResponse<String>>(
url = Url(AppIntegrityRoutes.demo),
data = mapOf<String, String>(),
appendHeaders = { append(ATTESTATION_TOKEN_HEADER, mobileToken) }
appendHeaders = { append(ATTESTATION_TOKEN_HEADER, mobileToken) },
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.ktor.client.statement.HttpResponse
import io.ktor.http.*
import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Test

/**
Expand All @@ -34,12 +33,7 @@ class ExampleUnitTest {
}

@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}

@Test
fun toto() {
fun apiClientProviderTest() {
runBlocking {
post<ApiResponse<String>>(Url("toto"), data = mapOf("toto" to 1))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class NewTransferActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ class ImportFilesViewModel @Inject constructor(
}
}

private fun setFailedIntegrityResult(exception: Throwable?) {
SentryLog.e(APP_INTEGRITY_MANAGER_TAG, "Failed integrity check", exception)
private fun setFailedIntegrityResult() {
_integrityCheckResult.value = AppIntegrityResult.Fail
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,7 @@ private fun SendButton(
showIndeterminateProgress = { integrityCheckResult() == AppIntegrityResult.Ongoing },
enabled = { importedFiles().isNotEmpty() && !isImporting && isSenderEmailCorrect },
progress = progress,
onClick = {
checkAppIntegrityBeforeSendingTransfer()
},
onClick = { checkAppIntegrityBeforeSendingTransfer() },
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class UploadWorker @AssistedInject constructor(
private val uploadManager: UploadManager,
) : BaseCoroutineWorker(appContext, params) {

private val appIntegrityManager by lazy { AppIntegrityManager(appContext) }

private val fileChunkSizeManager by lazy {
FileChunkSizeManager(
chunkMinSize = EXPECTED_CHUNK_SIZE,
Expand Down

0 comments on commit 1cfb281

Please sign in to comment.