Zum Inhalt springen

Android SDK

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

The Prosopo Protect Android SDK signs your app’s requests with a key held in the phone’s secure hardware, so the Protect edge can tell your real app on a real phone from a script, an emulator or a rooted device. Read Protect for mobile apps first for how the pieces fit together.

Add the dependency to your app module, using the newest version listed on Maven Central (1.0.4 at the time of writing):

dependencies {
implementation("io.prosopo:android-sdk:1.0.4")
}

Keep the SDK up to date: newer releases fix device-attestation problems that would otherwise block real users.

The SDK declares the ACCESS_NETWORK_STATE permission itself.

Create one ProsopoProtect instance for your app, for example in your Application class:

import io.prosopo.protect.ProsopoProtect
import io.prosopo.protect.ProsopoProtectConfig
val config = ProsopoProtectConfig.Builder(siteKey = "your-site-key")
.protectUrl("https://protect.example.com")
.build()
val sdk = ProsopoProtect(application, config)

The SDK starts its work on the first protected request, not at startup.

OptionRequiredDefaultDescription
siteKeyYesYour Prosopo site key.
protectUrlYesYour Protect endpoint, e.g. https://protect.example.com.
baseDomainNoprotectUrl host without its first labelThe site you are protecting. Prosopo headers, the session cookie and signatures are only sent to this domain and its subdomains, over HTTPS. Requests to any other host are left alone.
gcloudProjectNumberNoYour app’s Google Cloud project number. Turns on Google Play Integrity as well as key attestation. See Play Integrity.
integrityIntervalMsNo86400000 (24 hours)How old a cached Play Integrity token may get before it is renewed.
attestationPolicyNoSTRONGBOX_PREFERREDWhich secure hardware holds the key: STRONGBOX_PREFERRED, TEE_MINIMUM or REQUIRE_STRONGBOX.

build() throws IllegalArgumentException if siteKey is blank or protectUrl is not set.

Add the SDK as a network interceptor:

import io.prosopo.protect.ProsopoProtectOkHttp
import okhttp3.OkHttpClient
val client = OkHttpClient.Builder()
.addNetworkInterceptor(ProsopoProtectOkHttp(sdk))
.build()

As a network interceptor it runs after OkHttp adds your CookieJar’s cookies, so the prosopo_session cookie is merged into that header. It also runs once per redirect, so a redirect to another host gets no Prosopo credentials. addInterceptor works too, but only if your client has no CookieJar returning cookies for the protected site, because OkHttp would replace the Cookie header after the SDK set it.

import io.prosopo.protect.ProsopoProtectHttpUrlConnection
import java.net.URL
val connection = ProsopoProtectHttpUrlConnection.open(URL("https://api.example.com/listings"), sdk)
val status = connection.responseCode
if (connection.isBlocked()) {
// Protect blocked this request
}
connection.disconnect()

Wrap your WebView so pages on the protected site, and the fetch and XMLHttpRequest calls they make, carry the same headers and session as your native requests:

import io.prosopo.protect.ProsopoProtectWebview
val protectedWebView = ProsopoProtectWebview.wrap(myWebView, sdk)
protectedWebView.loadUrl("https://example.com/account")

Pages from other origins get nothing.

A blocked request comes back as HTTP 403 with the header x-prosopo-decision: block. Show your own message rather than retrying.

Without gcloudProjectNumber, the SDK uses Android hardware key attestation only: the phone’s secure hardware signs a certificate chain, rooted at Google, that proves the key lives in that hardware and records whether the bootloader is locked.

With gcloudProjectNumber, the SDK also fetches a Google Play Integrity token, which adds Google’s verdict on the device, and on whether the app binary is the one Google Play distributes. Protect decodes the token through your Google Cloud project, so ask your Prosopo account manager to connect the project before you ship a build with it turned on.

Under Android Apps in your Protect settings, add one entry per app that uses this site key, for example a production and a staging build:

  • Package: the app’s applicationId, e.g. com.example.app.
  • Google Cloud project (optional): the project the app’s Play Integrity tokens belong to.
  • Allow VM / emulator: accept Google-signed emulators. Leave this off for production; turn it on for a staging build that runs in CI.
  • Allow rooted devices: accept devices below Google’s device integrity bar and phones with an unlocked bootloader.
  • Require strong integrity: only accept phones with a recent security update and hardware-backed keys. This refuses real phones that are behind on updates, so use it when abuse outweighs that cost.
  • Allow sideloaded builds: accept app builds Google Play doesn’t recognise, such as debug builds installed over adb. On by default.
  • Allow revoked keyboxes: accept devices whose attestation keys Google has revoked.
  • Signing cert SHA-256 digests: your app’s signing certificate digests, comma-separated. Repackaged copies of your app signed with another certificate are refused.
  • Integrity grace (seconds): how long a newly registered device may make requests before it needs a Play Integrity verdict.

From 1.0.4, a phone whose key failed to register replaces it on the next app start. On older versions, clearing the app’s data or reinstalling the app registers a new key.