Skip to content

Integrity

Michael edited this page Apr 24, 2026 · 1 revision

Google Play Integrity API

The Google Play Integrity extension lets your app request integrity tokens from Google Play so your backend can verify requests are coming from a genuine app install and device.

This extension supports two request types:

  • Standard integrity token requests (recommended)
  • Classic integrity token requests (legacy / specific use cases)

Requirements

  • Android app distributed through Google Play
  • Google Cloud Project Number configured for your Play app
  • A backend service to decode and verify tokens returned by Play Integrity
  • Complete Play Integrity setup for your app and Play Console project: https://developer.android.com/google/play/integrity/setup

Basic Setup

Import the extension:

import com.distriqt.extension.googleplay.integrity.Integrity;

Check support before using the API:

if (Integrity.isSupported)
{
	trace("Integrity version: " + Integrity.service.version);
}
else
{
	trace("Integrity extension not supported on this device");
}

Standard Flow (Recommended)

The standard flow is:

  1. Prepare the API once using your Google Cloud Project Number
  2. Generate or receive a request hash for a specific backend request
  3. Request a token
  4. Send the returned token to your backend for verification

1. Prepare Integrity

Call prepareIntegrityToken() before making standard token requests:

Integrity.service.prepareIntegrityToken(
	PROJECT_NUMBER,
	function():void
	{
		trace("Integrity API prepared successfully");
	},
	function(error:Error):void
	{
		trace("Error preparing integrity token: " + error.message);
	}
);

2. Request a Standard Token

Call requestIntegrityToken() with a request hash:

Integrity.service.requestIntegrityToken(
	requestHash,
	function(token:String):void
	{
		// Send this token to your backend for verification
		trace("Integrity token received: " + token);
	},
	function(error:Error):void
	{
		trace("Error requesting integrity token: " + error.message);
	}
);

About requestHash

The requestHash should uniquely identify the backend request you are protecting.

  • It should be generated server-side wherever possible
  • It should be unique per request
  • It should be long enough to avoid collisions

In the example app, a JSON payload is hashed with SHA-256 and URL-safe Base64 encoded:

import air.security.Digest;
import flash.utils.ByteArray;

public function generateRequestHash(data:String):String
{
	var inputBytes:ByteArray = new ByteArray();
	inputBytes.writeUTFBytes(data);
	var hash:ByteArray = Digest.hash(Digest.SHA256, inputBytes);
	return Base64.encodeUrlSafe(hash);
}

The demo helper class Base64.encodeUrlSafe() performs URL-safe conversion (+ -> -, / -> _).

Classic Flow

Classic requests are still available for compatibility scenarios.

Call requestIntegrityClassicToken() with your project number and nonce:

Integrity.service.requestIntegrityClassicToken(
	PROJECT_NUMBER,
	nonce,
	function(token:String):void
	{
		trace("Classic integrity token received: " + token);
	},
	function(error:Error):void
	{
		trace("Error requesting classic integrity token: " + error.message);
	}
);

Nonce guidance:

  • Generate a strong, unpredictable value
  • Use one nonce per protected request
  • Prefer server-side generation

Events

You can use callbacks (shown above), events, or both.

The extension dispatches:

  • IntegrityEvent.PREPARE_SUCCESS
  • IntegrityErrorEvent.PREPARE_FAILED
  • IntegrityTokenEvent.TOKEN_RECEIVED
  • IntegrityErrorEvent.TOKEN_REQUEST_FAILED
  • IntegrityTokenEvent.CLASSIC_TOKEN_RECEIVED
  • IntegrityErrorEvent.CLASSIC_TOKEN_REQUEST_FAILED
  • ErrorEvent.ERROR

Example event listeners:

import com.distriqt.extension.googleplay.integrity.events.IntegrityErrorEvent;
import com.distriqt.extension.googleplay.integrity.events.IntegrityEvent;
import com.distriqt.extension.googleplay.integrity.events.IntegrityTokenEvent;
import flash.events.ErrorEvent;

Integrity.service.addEventListener(IntegrityEvent.PREPARE_SUCCESS, onPrepareSuccess);
Integrity.service.addEventListener(IntegrityTokenEvent.TOKEN_RECEIVED, onTokenReceived);
Integrity.service.addEventListener(IntegrityErrorEvent.TOKEN_REQUEST_FAILED, onTokenRequestFailed);
Integrity.service.addEventListener(ErrorEvent.ERROR, onExtensionError);

Verification Model

The token returned by the extension must be sent to your backend and verified there.

  • Do not trust integrity tokens purely on-device
  • Always evaluate the token server-side before allowing sensitive actions
  • Use the same request data (hash or nonce) on the backend when validating

Minimal End-to-End Example

if (!Integrity.isSupported)
{
	trace("Integrity not supported");
	return;
}

Integrity.service.prepareIntegrityToken(
	PROJECT_NUMBER,
	function():void
	{
		var requestData:Object = {
			user_id: "user123",
			action: "purchase_item",
			timestamp: new Date().time
		};
		var requestHash:String = generateRequestHash(JSON.stringify(requestData));

		Integrity.service.requestIntegrityToken(
			requestHash,
			function(token:String):void
			{
				// POST token to backend for verification
				trace("Integrity token received");
			},
			function(error:Error):void
			{
				trace("Token request failed: " + error.message);
			}
		);
	},
	function(error:Error):void
	{
		trace("Prepare failed: " + error.message);
	}
);

API Reference Summary

  • Integrity.isSupported : Boolean
  • Integrity.service.version : String
  • Integrity.service.nativeVersion : String
  • Integrity.service.implementation : String
  • Integrity.service.prepareIntegrityToken(projectId:String, success:Function = null, failure:Function = null) : void
  • Integrity.service.requestIntegrityToken(requestHash:String, success:Function = null, failure:Function = null) : void
  • Integrity.service.requestIntegrityClassicToken(projectId:String, nonce:String, success:Function = null, failure:Function = null) : void

Notes

  • Prefer standard token requests for new integrations.
  • Perform all trust decisions on your backend after token verification.
  • Keep project number, request generation, and nonce management aligned between app and server.