import android.content.Context import com.google.android.play.core.integrity.IntegrityManagerFactory import com.google.android.play.core.integrity.StandardIntegrityManager.PrepareIntegrityTokenRequest import com.google.android.play.core.integrity.StandardIntegrityManager.StandardIntegrityTokenRequest import kotlinx.coroutines.tasks.await import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.json.JSONObject import java.io.IOException import java.net.URL import javax.net.ssl.HttpsURLConnection data class SignupChallenge(val id: String, val requestHash: String) data class PilotSignup(val email: String, val challengeId: String?, val integrityToken: String?) // Supply your app's HTTPS client for both calls. Keep its pre-signup session // and CSRF handling. Request a fresh challenge for each submission. suspend fun submitPlaySignup( context: Context, cloudProjectNumber: Long, email: String, challenge: suspend (String) -> SignupChallenge, submit: suspend (PilotSignup) -> SignupOutcome ): SignupOutcome { val observation = try { val manager = IntegrityManagerFactory.createStandard(context.applicationContext) val provider = manager.prepareIntegrityToken( PrepareIntegrityTokenRequest.builder().setCloudProjectNumber(cloudProjectNumber).build() ).await() val issued = challenge(email) val token = provider.request( StandardIntegrityTokenRequest.builder().setRequestHash(issued.requestHash).build() ).await().token() PilotSignup(email, issued.id, token) } catch (cancelled: java.util.concurrent.CancellationException) { throw cancelled } catch (unavailable: Exception) { PilotSignup(email, null, null) } return submit(observation) } // The staging app supplies its existing pre-signup session cookie and CSRF // token. Keep this origin in app configuration and never follow redirects. class StagingSignupApi(private val origin: URL, private val cookie: String, private val csrfToken: String) { init { require(origin.protocol == "https" && origin.host.isNotEmpty() && origin.userInfo == null) } private suspend fun post(path: String, body: JSONObject): Pair = withContext(Dispatchers.IO) { val connection = URL(origin, path).openConnection() as HttpsURLConnection try { connection.requestMethod = "POST" connection.instanceFollowRedirects = false connection.connectTimeout = 4000 connection.readTimeout = 8000 connection.doOutput = true connection.setRequestProperty("Content-Type", "application/json") connection.setRequestProperty("Cookie", cookie) connection.setRequestProperty("X-CSRF-Token", csrfToken) connection.outputStream.use { it.write(body.toString().toByteArray(Charsets.UTF_8)) } val status = connection.responseCode val response = if (path == "/signup/challenge" && status == 200) connection.inputStream.bufferedReader().use { it.readText() } else "" status to response } finally { connection.disconnect() } } suspend fun challenge(email: String): SignupChallenge { val (status, text) = post("/signup/challenge", JSONObject().put("email", email)) if (status != 200) throw IOException("Challenge unavailable") val response = JSONObject(text) return SignupChallenge(response.getString("id"), response.getString("requestHash")) } suspend fun submit(input: PilotSignup): SignupOutcome { val body = JSONObject().put("email", input.email) input.challengeId?.let { body.put("challengeId", it) } input.integrityToken?.let { body.put("integrityToken", it) } return try { when (post("/signup", body).first) { 202 -> SignupOutcome.VERIFY_EMAIL 409 -> SignupOutcome.REVIEW 403 -> SignupOutcome.BLOCKED else -> SignupOutcome.TRY_LATER } } catch (unavailable: IOException) { SignupOutcome.TRY_LATER } } }