स्टैंडर्ड एपीआई अनुरोध करें

इस पेज पर, इंटिग्रिटी के नतीजों के लिए स्टैंडर्ड एपीआई अनुरोध करने के बारे में बताया गया है. ये अनुरोध, Android 5.0 (एपीआई लेवल 21) या इसके बाद के वर्शन पर काम करते हैं. जब भी आपका ऐप्लिकेशन किसी इंटरैक्शन की जांच करने के लिए सर्वर कॉल करता है, तब इंटिग्रिटी के नतीजे के लिए स्टैंडर्ड एपीआई अनुरोध किया जा सकता है.

खास जानकारी

पहली इमेज. सीक्वेंस डायग्राम, जिसमें Play Integrity API का हाई-लेवल डिज़ाइन दिखाया गया है.

स्टैंडर्ड अनुरोध के दो हिस्से होते हैं:

  • इंटेग्रिटी टोकन प्रोवाइडर को तैयार करना (एक बार): इंटेग्रिटी का नतीजा पाने से पहले, आपको इंटेग्रिटी टोकन प्रोवाइडर को तैयार करने के लिए, Integrity API को कॉल करना होगा. उदाहरण के लिए, ऐसा तब किया जा सकता है, जब आपका ऐप्लिकेशन लॉन्च हो रहा हो या बैकग्राउंड में चल रहा हो. हालांकि, ऐसा इंटिग्रिटी वर्डिक्ट मिलने से पहले करना होगा.
  • ज़रूरत पड़ने पर इंटिग्रिटी टोकन का अनुरोध करना: जब भी आपका ऐप्लिकेशन किसी सर्वर से अनुरोध करता है, तो आपको यह जांच करनी होती है कि वह अनुरोध सही है या नहीं. इसके लिए, आपको इंटिग्रिटी टोकन का अनुरोध करना होता है और उसे अपने ऐप्लिकेशन के बैकएंड सर्वर पर भेजना होता है, ताकि उसे डिक्रिप्ट किया जा सके और उसकी पुष्टि की जा सके. इसके बाद, आपका बैकएंड सर्वर यह तय कर सकता है कि आगे क्या करना है.

इंटेग्रिटी टोकन देने वाली सेवा को सेट अप करना (सिर्फ़ एक बार):

  1. आपका ऐप्लिकेशन, आपके Google Cloud प्रोजेक्ट नंबर के साथ इंटिग्रिटी टोकन देने वाली सेवा को कॉल करता है.
  2. आपका ऐप्लिकेशन, इंटिग्रिटी टोकन देने वाले को मेमोरी में सेव रखता है, ताकि पुष्टि करने के लिए आगे की जांच की जा सके.

इंटेग्रिटी टोकन का अनुरोध करना (मांग पर):

  1. उपयोगकर्ता की जिस कार्रवाई को सुरक्षित रखना है उसके लिए, आपका ऐप्लिकेशन अनुरोध का हैश कंप्यूट करता है. इसके लिए, SHA256 जैसे किसी सही हैश एल्गोरिदम का इस्तेमाल किया जाता है.
  2. आपका ऐप्लिकेशन, अनुरोध हैश को पास करके इंटिग्रिटी टोकन का अनुरोध करता है.
  3. आपके ऐप्लिकेशन को Play Integrity API से, साइन किया गया और एन्क्रिप्ट (सुरक्षित) किया गया इंटिग्रिटी टोकन मिलता है.
  4. आपका ऐप्लिकेशन, इंटेग्रिटी टोकन को अपने बैकएंड को पास करता है.
  5. आपके ऐप्लिकेशन का बैकएंड, टोकन को Google Play सर्वर पर भेजता है. Google Play सर्वर, नतीजे को डिक्रिप्ट करता है और उसकी पुष्टि करता है. इसके बाद, वह नतीजों को आपके ऐप्लिकेशन के बैकएंड पर भेजता है.
  6. आपके ऐप्लिकेशन का बैकएंड, टोकन पेलोड में मौजूद सिग्नल के आधार पर यह तय करता है कि आगे कैसे बढ़ना है.
  7. आपके ऐप्लिकेशन का बैकएंड, फ़ैसले के नतीजों को आपके ऐप्लिकेशन पर भेजता है.

इंटेग्रिटी टोकन देने वाली सेवा को सेट अप करना (एक बार)

Google Play से इंटिग्रिटी वर्डिक्ट के लिए स्टैंडर्ड अनुरोध करने से पहले, आपको इंटिग्रिटी टोकन देने वाली सेवा को तैयार करना होगा. इससे Google Play, डिवाइस पर पुष्टि करने की कुछ जानकारी को स्मार्ट तरीके से कैश मेमोरी में सेव कर पाता है. इससे, इंटिग्रिटी वर्डिक्ट के लिए अनुरोध करने पर, इंतज़ार का समय कम हो जाता है. कैश मेमोरी में सेव की गई पुष्टि की जानकारी को अप-टू-डेट रखने के लिए, टोकन उपलब्ध कराने वाली सेवा को समय-समय पर फिर से तैयार किया जा सकता है.

इंटिग्रिटी टोकन की सेवा देने वाली कंपनी को तैयार किया जा सकता है:

  • जब आपका ऐप्लिकेशन लॉन्च होता है (यानी कि कोल्ड स्टार्ट अप पर). टोकन प्रोवाइडर को तैयार करना एसिंक्रोनस है. इसलिए, इससे स्टार्टअप के समय पर कोई असर नहीं पड़ेगा. यह विकल्प तब बेहतर तरीके से काम करेगा, जब आपको ऐप्लिकेशन लॉन्च होने के तुरंत बाद, सुरक्षा जांच के नतीजे का अनुरोध करना हो. उदाहरण के लिए, जब कोई उपयोगकर्ता साइन इन करता है या कोई खिलाड़ी किसी गेम में शामिल होता है.
  • जब आपका ऐप्लिकेशन खोला जाता है (यानी कि वॉर्म स्टार्ट अप पर). हालांकि, ध्यान दें कि ऐप्लिकेशन का हर इंस्टेंस, इंटिग्रिटी टोकन को हर मिनट में सिर्फ़ पांच बार तैयार कर सकता है.
  • बैकग्राउंड में किसी भी समय, जब आपको इंटिग्रिटी वर्डिक्ट के अनुरोध से पहले टोकन तैयार करना हो.

इंटिग्रिटी टोकन प्रोवाइडर को तैयार करने के लिए, यह तरीका अपनाएं:

  1. StandardIntegrityManager बनाएं, जैसा कि इन उदाहरणों में दिखाया गया है.
  2. PrepareIntegrityTokenRequest बनाएं. इसके लिए, setCloudProjectNumber() तरीके का इस्तेमाल करके, Google Cloud प्रोजेक्ट नंबर दें.
  3. मैनेजर का इस्तेमाल करके, prepareIntegrityToken() को कॉल करें और 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));

Unity

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);
}

नेटिव

/// 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);

अनुरोधों को छेड़छाड़ से बचाना (सुझाया गया)

Play Integrity API की मदद से, अपने ऐप्लिकेशन में उपयोगकर्ता की किसी कार्रवाई की जांच करते समय, requestHash फ़ील्ड का इस्तेमाल किया जा सकता है. इससे, छेड़छाड़ वाले हमलों से बचा जा सकता है. उदाहरण के लिए, कोई गेम, खिलाड़ी के स्कोर की जानकारी गेम के बैकएंड सर्वर को भेज सकता है. साथ ही, आपका सर्वर यह पक्का करना चाहता है कि इस स्कोर में किसी प्रॉक्सी सर्वर ने छेड़छाड़ न की हो. Play Integrity API, साइन किए गए इंटिग्रिटी रिस्पॉन्स में, requestHash फ़ील्ड में सेट की गई वैल्यू दिखाता है. requestHash के बिना, इंटिग्रिटी टोकन सिर्फ़ डिवाइस से बाउंड होगा, न कि किसी खास अनुरोध से. इससे, हमले की संभावना बढ़ जाती है. यहां दिए गए निर्देशों में, requestHash फ़ील्ड का असरदार तरीके से इस्तेमाल करने का तरीका बताया गया है:

पूरी सुरक्षा की जांच के नतीजे का अनुरोध करने पर:

  • उपयोगकर्ता की कार्रवाई या सर्वर के अनुरोध से, काम के सभी अनुरोध पैरामीटर का डाइजेस्ट कंप्यूट करें. जैसे, अनुरोध के स्टेबल सीरियलाइज़ेशन का SHA256. requestHash फ़ील्ड में सेट की गई वैल्यू की लंबाई, ज़्यादा से ज़्यादा 500 बाइट हो सकती है. requestHash में ऐप्लिकेशन के अनुरोध का ऐसा डेटा शामिल करें जो उस कार्रवाई के लिए ज़रूरी या काम का हो जिसकी जांच की जा रही है या जिसे सुरक्षित किया जा रहा है. requestHash फ़ील्ड को इंटिग्रिटी टोकन में हूबहू शामिल किया जाता है. इसलिए, लंबी वैल्यू की वजह से अनुरोध का साइज़ बढ़ सकता है.
  • डाइजेस्ट को Play Integrity API के requestHash फ़ील्ड के तौर पर दें और इंटिग्रिटी टोकन पाएं.

पूरी सुरक्षा की जांच का नतीजा मिलने पर:

  • इंटिग्रिटी टोकन को डिकोड करें और requestHash फ़ील्ड को निकालें.
  • अनुरोध का डाइजेस्ट उसी तरह से कंप्यूट करें जिस तरह से ऐप्लिकेशन में किया जाता है. उदाहरण के लिए, अनुरोध के स्टेबल सीरियलाइज़ेशन का SHA256.
  • ऐप्लिकेशन-साइड और सर्वर-साइड डाइजेस्ट की तुलना करें. अगर वे मेल नहीं खाते हैं, तो अनुरोध भरोसेमंद नहीं है.

पूरी सुरक्षा की जांच के नतीजे का अनुरोध करना (मांग पर)

इंटेग्रिटी टोकन देने वाली सेवा को सेट अप करने के बाद, Google Play से इंटेग्रिटी के नतीजों का अनुरोध किया जा सकता है. ऐसा करने के लिए, यह तरीका अपनाएं:

  1. StandardIntegrityTokenProvider पाना.
  2. StandardIntegrityTokenRequest बनाएं. इसके लिए, उपयोगकर्ता की उस कार्रवाई का अनुरोध हैश दें जिसे आपको setRequestHash तरीके से सुरक्षित करना है.
  3. request() को कॉल करने के लिए, इंटिग्रिटी टोकन प्रोवाइडर का इस्तेमाल करें. साथ ही, 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));

Unity

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);
}

नेटिव

/// 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();

अगर आपका ऐप्लिकेशन लंबे समय तक एक ही टोकन प्रोवाइडर का इस्तेमाल करता है, तो टोकन प्रोवाइडर की समयसीमा खत्म हो सकती है. इससे, टोकन के लिए किए जाने वाले अगले अनुरोध पर, INTEGRITY_TOKEN_PROVIDER_INVALID गड़बड़ी का मैसेज दिखता है. इस गड़बड़ी को ठीक करने के लिए, आपको नए प्रोवाइडर का अनुरोध करना चाहिए.

इंटेग्रिटी के नतीजे को डिक्रिप्ट करना और उसकी पुष्टि करना

पूरी सुरक्षा की जांच के नतीजे का अनुरोध करने के बाद, Play Integrity API एन्क्रिप्ट (सुरक्षित) किया गया रिस्पॉन्स टोकन उपलब्ध कराता है. डिवाइस इंटिग्रिटी के नतीजे पाने के लिए, आपको Google के सर्वर पर इंटिग्रिटी टोकन को डिक्रिप्ट करना होगा. ऐसा करने के लिए, यह तरीका अपनाएं:

  1. अपने ऐप्लिकेशन से लिंक किए गए Google क्लाउड प्रोजेक्ट में, सेवा खाता बनाएं.
  2. अपने ऐप्लिकेशन के सर्वर पर, playintegrity स्कोप का इस्तेमाल करके, अपने सेवा खाते के क्रेडेंशियल से ऐक्सेस टोकन फ़ेच करें. इसके बाद, यह अनुरोध करें:

    playintegrity.googleapis.com/v1/PACKAGE_NAME:decodeIntegrityToken -d \
    '{ "integrity_token": "INTEGRITY_TOKEN" }'
  3. JSON रिस्पॉन्स को पढ़ें.

नतीजे के तौर पर मिलने वाला पेलोड, एक सामान्य टेक्स्ट टोकन होता है. इसमें इंटेग्रिटी के फ़ैसले शामिल होते हैं.

अपने-आप रीप्ले होने से रोकने की सुविधा

रीप्ले अटैक से बचने के लिए, Google Play अपने-आप इंटिग्रिटी टोकन को कई बार इस्तेमाल होने से रोकता है. एक ही टोकन को बार-बार डिक्रिप्ट करने की कोशिश करने पर, इस तरह के नतीजे दिखेंगे:

  • डिवाइस की पहचान करने का फ़ैसला खाली रहेगा.
  • ऐप्लिकेशन की पहचान की पुष्टि और ऐप्लिकेशन के लिए लाइसेंस की प्रोसेस के नतीजे, UNEVALUATED पर सेट हो जाएंगे.
  • Play Console का इस्तेमाल करके चालू किए गए किसी भी वैकल्पिक नतीजे को UNEVALUATED पर सेट किया जाएगा. अगर नतीजे में कई वैल्यू शामिल हैं, तो उसे खाली नतीजे पर सेट किया जाएगा.

Google Play के प्रॉम्प्ट की मदद से, फ़ैसले से जुड़ी समस्याएं ठीक करना (ज़रूरी नहीं)

सर्वर को इंटिग्रिटी वर्डिक्ट मिलने के बाद, वह यह तय कर सकता है कि आगे क्या करना है. अगर नतीजे से पता चलता है कि कोई समस्या है, जैसे कि ऐप्लिकेशन के पास लाइसेंस नहीं है, उसमें छेड़छाड़ की गई है या डिवाइस को हैक किया गया है, तो उपयोगकर्ताओं को समस्या को खुद ठीक करने का मौका दिया जा सकता है.

Play Integrity API, Google Play का एक डायलॉग बॉक्स दिखाने का विकल्प देता है. इससे उपयोगकर्ता को कार्रवाई करने के लिए कहा जाता है. उदाहरण के लिए, Google Play से आपके ऐप्लिकेशन का आधिकारिक वर्शन डाउनलोड करने के लिए.

सर्वर के जवाब के आधार पर, अपने ऐप्लिकेशन से इन डायलॉग को ट्रिगर करने का तरीका जानने के लिए, समस्या हल करने वाले डायलॉग देखें.