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

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

खास जानकारी

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

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

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

इंटिग्रिटी टोकन प्रोवाइडर तैयार करें (एक बंद):

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

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

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

इंटिग्रिटी टोकन प्रोवाइडर तैयार करें (एक बार)

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

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

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

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

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

यूनिटी

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

    // Create an instance of a standard integrity manager.
    var standardIntegrityManager = new StandardIntegrityManager();

    // 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 != StandardIntegrityErrorCode.NoError)
    {
        AppendStatusLog("StandardIntegrityAsyncOperation failed with error: " +
                integrityTokenProviderOperation.Error);
        yield break;
    }

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

मूल भाषा वाला

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

यूनिटी

IEnumerator RequestIntegrityTokenCoroutine() {
    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 != StandardIntegrityErrorCode.NoError)
    {
        AppendStatusLog("StandardIntegrityAsyncOperation failed with error: " +
                integrityTokenOperation.Error);
        yield break;
    }

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

मूल भाषा वाला

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

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

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

  1. सेवा खाता बनाना आपके ऐप्लिकेशन से जुड़े हुए Google Cloud प्रोजेक्ट में.
  2. अपने ऐप्लिकेशन के सर्वर पर, अपने सेवा खाते से ऐक्सेस टोकन पाएं Playintegrity के स्कोप का इस्तेमाल करने वाले क्रेडेंशियल और यह अनुरोध करें:

    playintegrity.googleapis.com/v1/PACKAGE_NAME:decodeIntegrityToken -d \
    '{ "integrity_token": "INTEGRITY_TOKEN" }'
  3. JSON का जवाब पढ़ें.

नतीजे के तौर पर मिलने वाला पेलोड, सादा टेक्स्ट वाला टोकन होता है. इसमें पूरी सुरक्षा देने की सुविधा होती है नतीजे.

ऑटोमैटिक फिर से चलाने की सुरक्षा

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