Repository navigation
Integrity
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)
- 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
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");
}The standard flow is:
- Prepare the API once using your Google Cloud Project Number
- Generate or receive a request hash for a specific backend request
- Request a token
- Send the returned token to your backend for verification
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);
}
);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);
}
);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 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
You can use callbacks (shown above), events, or both.
The extension dispatches:
IntegrityEvent.PREPARE_SUCCESSIntegrityErrorEvent.PREPARE_FAILEDIntegrityTokenEvent.TOKEN_RECEIVEDIntegrityErrorEvent.TOKEN_REQUEST_FAILEDIntegrityTokenEvent.CLASSIC_TOKEN_RECEIVEDIntegrityErrorEvent.CLASSIC_TOKEN_REQUEST_FAILEDErrorEvent.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);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
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);
}
);Integrity.isSupported : BooleanIntegrity.service.version : StringIntegrity.service.nativeVersion : StringIntegrity.service.implementation : StringIntegrity.service.prepareIntegrityToken(projectId:String, success:Function = null, failure:Function = null) : voidIntegrity.service.requestIntegrityToken(requestHash:String, success:Function = null, failure:Function = null) : voidIntegrity.service.requestIntegrityClassicToken(projectId:String, nonce:String, success:Function = null, failure:Function = null) : void
- 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.