Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8b15daad authored by Fynn Godau's avatar Fynn Godau Committed by Jonathan Klee
Browse files

Implement callback for PlayIntegrity

parent 3f193780
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
package foundation.e.apps;

import foundation.e.apps.IAppLoungeIntegrityServiceCallback;

interface IAppLoungeIntegrityService {
    void checkIntegrity(String packageName, String nonce);
    void checkIntegrity(String packageName, String nonce, IAppLoungeIntegrityServiceCallback callback);
}
 No newline at end of file
+6 −0
Original line number Diff line number Diff line
package foundation.e.apps;

interface IAppLoungeIntegrityServiceCallback {
    void onSuccess(String token);
    void onError(int errorCode);
}
 No newline at end of file
+7 −9
Original line number Diff line number Diff line
@@ -195,11 +195,11 @@ class GPlayAPIImpl @Inject constructor(private val gPlayHttpClient: GPlayHttpCli
        packageName: String,
        nonce: String,
        droidGuardToken: String
    ) {
    ): String? {

        val integrityApiKey = "AIzaSyAjf_GZN-B3oyEKTqug3eEcch_4qK3M2Hg"

        withContext(Dispatchers.IO) {
        return withContext(Dispatchers.IO) {

            Log.i("jklee", "checking integrity with token $droidGuardToken")
            val integrityHelper = IntegrityHelper(authData, integrityApiKey).using(gPlayHttpClient)
@@ -207,13 +207,11 @@ class GPlayAPIImpl @Inject constructor(private val gPlayHttpClient: GPlayHttpCli
            Log.i("jklee", "checkIntegrity reponse ${response.code}")
            Log.i("jklee", "checkIntegrity successful ${response.isSuccessful}")


            val token = IntegrityAnswer.parseFrom(response.responseBytes).tokenWrapper.token.value
            Log.i("jklee", "token $token");
            response = integrityHelper.checkToken(token)
            Log.i("jklee", "checkToken response ${response.code}")
            Log.i("jklee", "checkToken successful ${response.isSuccessful}")
            Log.i("jklee", "checkToken response ${String(response.responseBytes)}")
            if (!response.isSuccessful) {
                null
            } else {
                IntegrityAnswer.parseFrom(response.responseBytes).tokenWrapper.token.value
            }
        }
    }

+1 −3
Original line number Diff line number Diff line
@@ -116,7 +116,5 @@ class GPlayAPIRepository @Inject constructor(private val gPlayAPIImpl: GPlayAPII
        packageName: String,
        nonce: String,
        droidGuardToken: String
    ) {
        gPlayAPIImpl.checkIntegrityService(authData, packageName, nonce, droidGuardToken)
    }
    ) = gPlayAPIImpl.checkIntegrityService(authData, packageName, nonce, droidGuardToken)
}
+9 −4
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ import com.google.android.gms.droidguard.DroidGuard
import com.google.android.gms.droidguard.DroidGuardClient
import com.google.android.gms.droidguard.internal.DroidGuardResultsRequest
import foundation.e.apps.IAppLoungeIntegrityService
import foundation.e.apps.IAppLoungeIntegrityServiceCallback
import foundation.e.apps.api.gplay.GPlayAPIRepository
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@@ -37,13 +38,14 @@ class IntegrityBinder(

    companion object {
        const val TAG = "IntegrityBinder"
        const val INTEGRITY_ERROR_NETWORK_ERROR = -3
    }

    override fun checkIntegrity(packageName: String, nonce: String) {
        requestDroidGuardToken(packageName, nonce)
    override fun checkIntegrity(packageName: String, nonce: String, callback: IAppLoungeIntegrityServiceCallback) {
        requestDroidGuardToken(packageName, nonce, callback)
    }

    private fun requestDroidGuardToken(packageName: String, nonce: String) {
    private fun requestDroidGuardToken(packageName: String, nonce: String, callback: IAppLoungeIntegrityServiceCallback) {
        val integrityPackage = IntegrityPackage.newBuilder().setPackageName(packageName)
        val versionCode = PackageVersionCode.newBuilder().setVersion(10)
        val timestamp = System.currentTimeMillis().asProtoTimestamp()
@@ -72,7 +74,10 @@ class IntegrityBinder(
        request.bundle.putString("thirdPartyCallerAppPackageName", packageName)
        client.getResults("pia_attest", map, request).addOnSuccessListener {
            lifecycleCoroutineScope.launch {
                gPlayAPIRepository.checkIntegrity(authData, packageName, nonce, it)
                gPlayAPIRepository.checkIntegrity(authData, packageName, nonce, it).let {
                    if (it == null) callback.onError(INTEGRITY_ERROR_NETWORK_ERROR)
                    else callback.onSuccess(it)
                }
            }
        }
    }