Esegui una richiesta API standard

Questa pagina descrive come effettuare richieste API standard per gli esiti di integrità, supportati su Android 5.0 (livello API 21) o versioni successive. Puoi effettuare una richiesta API standard per un esito di integrità ogni volta che la tua app effettua una chiamata al server per verificare se l'interazione è autentica.

Panoramica

Figura 1. Diagramma di sequenza che mostra il design di alto livello dell'API Play Integrity.

Una richiesta standard è composta da due parti:

  • Prepara il fornitore di token di integrità (una tantum): devi chiamare l'API Integrity per preparare il fornitore di token di integrità molto prima di dover ottenere l'esito relativo all'integrità. Ad esempio, puoi farlo quando l'app viene avviata o in background prima che sia necessario l'esito di integrità.
  • Richiedi un token di integrità (on demand): ogni volta che la tua app effettua una richiesta al server che vuoi verificare sia autentica, richiedi un token di integrità e invialo al server di backend della tua app per la decriptazione e la verifica. Il tuo server di backend può quindi determinare come agire.

Prepara il provider di token di integrità (una tantum):

  1. La tua app chiama il provider di token di integrità con il numero del tuo progetto Google Cloud.
  2. La tua app conserva in memoria il provider di token di integrità per ulteriori chiamate di controllo dell'attestazione.

Richiedi un token di integrità (on demand):

  1. Per l'azione utente che deve essere protetta, l'app calcola l'hash (utilizzando un algoritmo di hashing adatto, ad esempio SHA256) della richiesta da effettuare.
  2. La tua app richiede un token di integrità, passando l'hash della richiesta.
  3. La tua app riceve il token di integrità firmato e criptato dall'API Play Integrity.
  4. La tua app passa il token di integrità al backend dell'app.
  5. Il backend della tua app invia il token a un server di Google Play. Il server di Google Play decripta e verifica l'esito, restituendo i risultati al backend della tua app.
  6. Il backend della tua app determina come procedere in base agli indicatori contenuti nel payload del token.
  7. Il backend della tua app invia i risultati della decisione alla tua app.

Prepara il provider di token di integrità (una tantum)

Prima di effettuare una richiesta standard di un esito di integrità da Google Play, devi preparare (o "riscaldare") il provider di token di integrità. In questo modo, Google Play può memorizzare nella cache in modo intelligente le informazioni di attestazione parziale sul dispositivo per ridurre la latenza nel percorso critico quando effettui una richiesta di esito di integrità. Puoi anche preparare nuovamente il provider di token periodicamente per mantenere aggiornate le informazioni di attestazione memorizzate nella cache.

Potresti preparare il provider di token di integrità:

  • Quando l'app viene avviata (ovvero all'avvio a freddo). La preparazione del provider di token è asincrona, quindi non influirà sul tempo di avvio. Questa opzione è ideale se prevedi di effettuare una richiesta di esito relativo all'integrità poco dopo il lancio dell'app, ad esempio quando un utente accede o un giocatore partecipa a un gioco.
  • Quando l'app viene aperta (ovvero all'avvio a caldo). Tuttavia, tieni presente che ogni istanza dell'app può preparare il token di integrità solo fino a 5 volte al minuto.
  • In qualsiasi momento in background, quando vuoi preparare il token prima di una richiesta di esito di integrità.

Per preparare il provider di token di integrità, segui questi passaggi:

  1. Crea un StandardIntegrityManager, come mostrato negli esempi seguenti.
  2. Crea un PrepareIntegrityTokenRequest, fornendo il numero di progetto Google Cloud tramite il metodo setCloudProjectNumber().
  3. Utilizza il gestore per chiamare prepareIntegrityToken(), fornendo PrepareIntegrityTokenRequest.

Java

import com.google.android.gms.tasks.Task;

// Create an instance of a manager.
StandardIntegrityManager standardIntegrityManager =
    IntegrityManagerFactory.createStandard(applicationContext);

StandardIntegrityTokenProvider integrityTokenProvider;
long cloudProjectNumber = ...;

// Prepare integrity token. Can be called once in a while to keep internal
// state fresh.
standardIntegrityManager.prepareIntegrityToken(
    PrepareIntegrityTokenRequest.builder()
        .setCloudProjectNumber(cloudProjectNumber)
        .build())
    .addOnSuccessListener(tokenProvider -> {
        integrityTokenProvider = tokenProvider;
    })
    .addOnFailureListener(exception -> handleError(exception));

Unità

IEnumerator PrepareIntegrityTokenCoroutine() {
    long cloudProjectNumber = ...;

    // Initialize the V2 manager. Requires Play Integrity Unity Plugin v2.0.0
    // or higher.
    var standardIntegrityManager = new StandardIntegrityManagerV2();

    // Request the token provider.
    var integrityTokenProviderOperation =
      standardIntegrityManager.PrepareIntegrityToken(
        new PrepareIntegrityTokenRequest(cloudProjectNumber));

    // Wait for PlayAsyncOperation to complete.
    yield return integrityTokenProviderOperation;

    // Check the resulting error code.
    if (integrityTokenProviderOperation.Error != null &&
        integrityTokenProviderOperation.Error.ErrorCode != StandardIntegrityErrorCode.NoError)
    {
        AppendStatusLog("StandardIntegrityAsyncOperation failed with error: " +
                integrityTokenProviderOperation.Error.ErrorCode);
        yield break;
    }

    // Get the response.
    var integrityTokenProvider = integrityTokenProviderOperation.GetResult();
}

Unreal Engine

// .h
void MyClass::OnPrepareIntegrityTokenCompleted(
  EStandardIntegrityErrorCode ErrorCode,
  UStandardIntegrityTokenProvider* Provider)
{
  // Check the resulting error code.
  if (ErrorCode == EStandardIntegrityErrorCode::StandardIntegrity_NO_ERROR)
  {
    // ...
  }
}

// .cpp
void MyClass::PrepareIntegrityToken()
{
  int64 CloudProjectNumber = ...

  // Create the Integrity Token Request.
  FPrepareIntegrityTokenRequest Request = { CloudProjectNumber };

  // Create a delegate to bind the callback function.
  FPrepareIntegrityOperationCompletedDelegate Delegate;

  // Bind the completion handler (OnPrepareIntegrityTokenCompleted) to the delegate.
  Delegate.BindDynamic(this, &MyClass::OnPrepareIntegrityTokenCompleted);

  // Initiate the prepare integrity token operation, passing the delegate to handle the result.
  GetGameInstance()
    ->GetSubsystem<UStandardIntegrityManager>()
    ->PrepareIntegrityToken(Request, Delegate);
}

Nativo

/// Initialize StandardIntegrityManager
StandardIntegrityManager_init(/* app's java vm */, /* an android context */);
/// Create a PrepareIntegrityTokenRequest opaque object.
int64_t cloudProjectNumber = ...;
PrepareIntegrityTokenRequest* tokenProviderRequest;
PrepareIntegrityTokenRequest_create(&tokenProviderRequest);
PrepareIntegrityTokenRequest_setCloudProjectNumber(tokenProviderRequest, cloudProjectNumber);

/// Prepare a StandardIntegrityTokenProvider opaque type pointer and call
/// StandardIntegrityManager_prepareIntegrityToken().
StandardIntegrityTokenProvider* tokenProvider;
StandardIntegrityErrorCode error_code =
        StandardIntegrityManager_prepareIntegrityToken(tokenProviderRequest, &tokenProvider);

/// ...
/// Proceed to polling iff error_code == STANDARD_INTEGRITY_NO_ERROR
if (error_code != STANDARD_INTEGRITY_NO_ERROR)
{
    /// Remember to call the *_destroy() functions.
    return;
}
/// ...
/// Use polling to wait for the async operation to complete.

IntegrityResponseStatus token_provider_status;

/// Check for error codes.
StandardIntegrityErrorCode error_code =
        StandardIntegrityTokenProvider_getStatus(tokenProvider, &token_provider_status);
if (error_code == STANDARD_INTEGRITY_NO_ERROR
    && token_provider_status == INTEGRITY_RESPONSE_COMPLETED)
{
    /// continue to request token from the token provider
}
/// ...
/// Remember to free up resources.
PrepareIntegrityTokenRequest_destroy(tokenProviderRequest);

Proteggi le richieste dalle manomissioni (opzione consigliata)

Quando controlli un'azione utente nella tua app con l'API Play Integrity, puoi sfruttare il campo requestHash per mitigare gli attacchi di manomissione. Ad esempio, un gioco potrebbe voler segnalare il punteggio del giocatore al server di backend del gioco e il tuo server vuole assicurarsi che questo punteggio non sia stato manomesso da un server proxy. L'API Play Integrity restituisce il valore impostato nel campo requestHash all'interno della risposta di integrità firmata. Senza requestHash, il token di integrità verrà associato solo al dispositivo, ma non alla richiesta specifica, il che apre la possibilità di attacchi. Le seguenti istruzioni descrivono come utilizzare in modo efficace il campo requestHash:

Quando richiedi un esito relativo all'integrità:

  • Calcola un digest di tutti i parametri di richiesta pertinenti (ad es. SHA256 di una serializzazione della richiesta stabile) dall'azione dell'utente o dalla richiesta del server in corso. Il valore impostato nel campo requestHash ha una lunghezza massima di 500 byte. Includi nel requestHash tutti i dati della richiesta dell'app che sono fondamentali o pertinenti all'azione che stai controllando o proteggendo. Il campo requestHash è incluso nel token di integrità alla lettera, pertanto i valori lunghi potrebbero aumentare le dimensioni della richiesta.
  • Fornisci il digest come campo requestHash all'API Play Integrity e ottieni il token di integrità.

Quando ricevi un esito relativo all'integrità:

  • Decodifica il token di integrità ed estrai il campo requestHash.
  • Calcola un digest della richiesta nello stesso modo dell'app (ad es. SHA256 di una serializzazione della richiesta stabile).
  • Confronta i digest lato app e lato server. Se non corrispondono, la richiesta non è attendibile.

Richiedi un esito relativo all'integrità (on demand)

Dopo aver preparato il provider di token di integrità, puoi iniziare a richiedere esiti di integrità da Google Play. Per farlo, segui questa procedura.

  1. Ottieni un StandardIntegrityTokenProvider.
  2. Crea un StandardIntegrityTokenRequest, fornendo l'hash della richiesta dell'azione utente che vuoi proteggere tramite il metodo setRequestHash.
  3. Utilizza il fornitore di token di integrità per chiamare request(), fornendo il StandardIntegrityTokenRequest.

Java

import com.google.android.gms.tasks.Task;

StandardIntegrityTokenProvider integrityTokenProvider;

// See above how to prepare integrityTokenProvider.

// Request integrity token by providing a user action request hash. Can be called
// several times for different user actions.
String requestHash = "2cp24z...";
Task<StandardIntegrityToken> integrityTokenResponse =
    integrityTokenProvider.request(
        StandardIntegrityTokenRequest.builder()
            .setRequestHash(requestHash)
            .build());
integrityTokenResponse
    .addOnSuccessListener(response -> sendToServer(response.token()))
    .addOnFailureListener(exception -> handleError(exception));

Unità

IEnumerator RequestIntegrityTokenCoroutine() {
    StandardIntegrityManagerV2.StandardIntegrityTokenProvider integrityTokenProvider;

    // See above how to prepare integrityTokenProvider.

    // Request integrity token by providing a user action request hash. Can be called
    // several times for different user actions.
    String requestHash = "2cp24z...";
    var integrityTokenOperation = integrityTokenProvider.Request(
      new StandardIntegrityTokenRequest(requestHash)
    );

    // Wait for PlayAsyncOperation to complete.
    yield return integrityTokenOperation;

    // Check the resulting error code.
    if (integrityTokenOperation.Error != null &&
        integrityTokenOperation.Error.ErrorCode != StandardIntegrityErrorCode.NoError)
    {
        AppendStatusLog("StandardIntegrityAsyncOperation failed with error: " +
                integrityTokenOperation.Error.ErrorCode);
        yield break;
    }

    // Get the response.
    var integrityToken = integrityTokenOperation.GetResult();
}

Unreal Engine

// .h
void MyClass::OnRequestIntegrityTokenCompleted(
  EStandardIntegrityErrorCode ErrorCode,
  UStandardIntegrityToken* Response)
{
  // Check the resulting error code.
  if (ErrorCode == EStandardIntegrityErrorCode::StandardIntegrity_NO_ERROR)
  {
    // Get the token.
    FString Token = Response->Token;
  }
}

// .cpp
void MyClass::RequestIntegrityToken()
{
  UStandardIntegrityTokenProvider* Provider = ...

  // Prepare the UStandardIntegrityTokenProvider.

  // Request integrity token by providing a user action request hash. Can be called
  // several times for different user actions.
  FString RequestHash = ...;
  FStandardIntegrityTokenRequest Request = { RequestHash };

  // Create a delegate to bind the callback function.
  FStandardIntegrityOperationCompletedDelegate Delegate;

  // Bind the completion handler (OnRequestIntegrityTokenCompleted) to the delegate.
  Delegate.BindDynamic(this, &MyClass::OnRequestIntegrityTokenCompleted);

  // Initiate the standard integrity token request, passing the delegate to handle the result.
  Provider->Request(Request, Delegate);
}

Nativo

/// Create a StandardIntegrityTokenRequest opaque object.
const char* requestHash = ...;
StandardIntegrityTokenRequest* tokenRequest;
StandardIntegrityTokenRequest_create(&tokenRequest);
StandardIntegrityTokenRequest_setRequestHash(tokenRequest, requestHash);

/// Prepare a StandardIntegrityToken opaque type pointer and call
/// StandardIntegrityTokenProvider_request(). Can be called several times for
/// different user actions. See above how to prepare token provider.
StandardIntegrityToken* token;
StandardIntegrityErrorCode error_code =
        StandardIntegrityTokenProvider_request(tokenProvider, tokenRequest, &token);

/// ...
/// Proceed to polling iff error_code == STANDARD_INTEGRITY_NO_ERROR
if (error_code != STANDARD_INTEGRITY_NO_ERROR)
{
    /// Remember to call the *_destroy() functions.
    return;
}
/// ...
/// Use polling to wait for the async operation to complete.

IntegrityResponseStatus token_status;

/// Check for error codes.
StandardIntegrityErrorCode error_code =
        StandardIntegrityToken_getStatus(token, &token_status);
if (error_code == STANDARD_INTEGRITY_NO_ERROR
    && token_status == INTEGRITY_RESPONSE_COMPLETED)
{
    const char* integrityToken = StandardIntegrityToken_getToken(token);
}
/// ...
/// Remember to free up resources.
StandardIntegrityTokenRequest_destroy(tokenRequest);
StandardIntegrityToken_destroy(token);
StandardIntegrityTokenProvider_destroy(tokenProvider);
StandardIntegrityManager_destroy();

Se la tua app utilizza lo stesso provider di token per troppo tempo, il provider di token può scadere, causando l'errore INTEGRITY_TOKEN_PROVIDER_INVALID alla richiesta di token successiva. Devi gestire questo errore richiedendo un nuovo provider.

Decripta e verifica l'esito relativo all'integrità

Dopo aver richiesto un esito relativo all'integrità, l'API Play Integrity fornisce un token di risposta criptato. Per ottenere gli esiti di integrità del dispositivo, devi decriptare il token di integrità sui server di Google. Per farlo, segui questi passaggi:

  1. Crea un service account all'interno del progetto Google Cloud collegato alla tua app.
  2. Sul server della tua app, recupera il token di accesso dalle credenziali del service account utilizzando l'ambito playintegrity ed effettua la seguente richiesta:

    playintegrity.googleapis.com/v1/PACKAGE_NAME:decodeIntegrityToken -d \
    '{ "integrity_token": "INTEGRITY_TOKEN" }'
  3. Leggi la risposta JSON.

Il payload risultante è un token di testo normale che contiene esiti di integrità.

Protezione automatica di riproduzione

Per mitigare gli attacchi di replay, Google Play impedisce automaticamente il riutilizzo dei token di integrità più volte. Il tentativo di decriptare ripetutamente lo stesso token comporterà l'eliminazione dei verdetti come segue:

  • Il verdetto di riconoscimento del dispositivo sarà vuoto.
  • L'esito del riconoscimento app e l'esito delle licenze dell'app verranno impostati su UNEVALUATED.
  • Tutti gli esiti facoltativi attivati tramite Play Console verranno impostati su UNEVALUATED (o su un esito vuoto se si tratta di un esito con più valori).

Risolvi i problemi relativi ai verdict con un prompt di Google Play (facoltativo)

Dopo aver ricevuto un esito relativo all'integrità, il server può determinare come procedere. Se l'esito indica un problema, ad esempio l'app non ha una licenza, è stata manomessa o il dispositivo è stato compromesso, puoi dare agli utenti la possibilità di risolvere il problema autonomamente.

L'API Play Integrity offre un'opzione per mostrare una finestra di dialogo di Google Play che invita l'utente a passare all'azione, ad esempio scaricare la versione ufficiale della tua app da Google Play.

Per scoprire come attivare queste finestre di dialogo dalla tua app in base alla risposta del server, consulta Finestre di dialogo di correzione.