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

Commit 7dc98285 authored by Fynn Godau's avatar Fynn Godau Committed by Jonathan Klee
Browse files

Move code to more proper places

parent a687421c
Loading
Loading
Loading
Loading
+0 −45
Original line number Diff line number Diff line
package com.android.vending

import android.accounts.Account
import android.accounts.AccountManager
import android.accounts.AccountManagerFuture
import android.os.Bundle
import android.util.Log
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.util.zip.GZIPOutputStream
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

private const val TAG = "FakeStoreUtil"

/**
 * From [StackOverflow](https://stackoverflow.com/a/46688434/), CC BY-SA 4.0 by Sergey Frolov, adapted.
 */
fun ByteArray.encodeGzip(): ByteArray {
    try {
        ByteArrayOutputStream().use { byteOutput ->
            GZIPOutputStream(byteOutput).use { gzipOutput ->
                gzipOutput.write(this)
                gzipOutput.finish()
                return byteOutput.toByteArray()
            }
        }
    } catch (e: IOException) {
        Log.e(TAG, "Failed to encode bytes as GZIP")
        return ByteArray(0)
    }
}

suspend fun AccountManager.getAuthToken(account: Account, authTokenType: String, notifyAuthFailure: Boolean) =
    suspendCoroutine { continuation ->
        getAuthToken(account, authTokenType, notifyAuthFailure, { future: AccountManagerFuture<Bundle> ->
            try {
                val result = future.result
                continuation.resume(result)
            } catch (e: Exception) {
                continuation.resumeWithException(e)
            }
        }, null)
    }
 No newline at end of file
+38 −21
Original line number Diff line number Diff line
@@ -2,16 +2,20 @@ package com.android.vending.licensing

import android.accounts.Account
import android.accounts.AccountManager
import android.accounts.AccountManagerFuture
import android.accounts.AuthenticatorException
import android.accounts.OperationCanceledException
import android.content.pm.PackageInfo
import android.os.Bundle
import android.os.RemoteException
import android.util.Log
import com.android.vending.LicenseResult
import com.android.vending.getAuthToken
import com.android.volley.VolleyError
import org.microg.vending.billing.core.HttpClient
import java.io.IOException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

private const val TAG = "FakeLicenseChecker"

@@ -67,6 +71,28 @@ const val ERROR_NON_MATCHING_UID: Int = 0x103

const val AUTH_TOKEN_SCOPE: String = "oauth2:https://www.googleapis.com/auth/googleplay"

sealed class LicenseRequestParameters
data class V1Parameters(
    val nonce: Long
) : LicenseRequestParameters()
object V2Parameters : LicenseRequestParameters()

sealed class LicenseResponse(
    val result: Int
)
class V1Response(
    result: Int,
    val signedData: String,
    val signature: String
) : LicenseResponse(result)
class V2Response(
    result: Int,
    val jwt: String?
): LicenseResponse(result)
class ErrorResponse(
    result: Int
): LicenseResponse(result)

/**
 * Performs license check including caller UID verification, using a given account, for which
 * an auth token is fetched.
@@ -141,24 +167,15 @@ suspend fun HttpClient.makeLicenseV2Request(
    V2Response(LICENSED, it)
}

sealed class LicenseRequestParameters
data class V1Parameters(
    val nonce: Long
) : LicenseRequestParameters()
object V2Parameters : LicenseRequestParameters()

sealed class LicenseResponse(
    val result: Int
)
class V1Response(
    result: Int,
    val signedData: String,
    val signature: String
) : LicenseResponse(result)
class V2Response(
    result: Int,
    val jwt: String?
): LicenseResponse(result)
class ErrorResponse(
    result: Int
): LicenseResponse(result)
suspend fun AccountManager.getAuthToken(account: Account, authTokenType: String, notifyAuthFailure: Boolean) =
    suspendCoroutine { continuation ->
        getAuthToken(account, authTokenType, notifyAuthFailure, { future: AccountManagerFuture<Bundle> ->
            try {
                val result = future.result
                continuation.resume(result)
            } catch (e: Exception) {
                continuation.resumeWithException(e)
            }
        }, null)
    }
 No newline at end of file
+21 −1
Original line number Diff line number Diff line
@@ -21,12 +21,14 @@ import com.android.vending.TimestampWrapper
import com.android.vending.UnknownByte12
import com.android.vending.UserAgent
import com.android.vending.Uuid
import com.android.vending.encodeGzip
import com.google.android.gms.common.BuildConfig
import okio.ByteString
import org.microg.gms.profile.Build
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.net.URLEncoder
import java.util.UUID
import java.util.zip.GZIPOutputStream

private const val TAG = "FakeLicenseRequest"

@@ -156,3 +158,21 @@ private fun makeTimestamp(millis: Long): Timestamp {
private fun encodeString(s: String?): String {
    return URLEncoder.encode(s).replace("+", "%20")
}

/**
 * From [StackOverflow](https://stackoverflow.com/a/46688434/), CC BY-SA 4.0 by Sergey Frolov, adapted.
 */
fun ByteArray.encodeGzip(): ByteArray {
    try {
        ByteArrayOutputStream().use { byteOutput ->
            GZIPOutputStream(byteOutput).use { gzipOutput ->
                gzipOutput.write(this)
                gzipOutput.finish()
                return byteOutput.toByteArray()
            }
        }
    } catch (e: IOException) {
        Log.e(TAG, "Failed to encode bytes as GZIP")
        return ByteArray(0)
    }
}
 No newline at end of file