अनुमति देना और पुष्टि करना

अनुरोध के मैसेज की पुष्टि लागू करें, ताकि यह पक्का किया जा सके कि आपके फ़ुलफ़िलमेंट एंडपॉइंट के लिए चेकआउट और सबमिट करने के अनुरोध Google से मिले हैं. साथ ही, ऐसे तीसरे पक्षों को आपके एंडपॉइंट पर कॉल करने से रोका जा सकता है जिन्हें इसकी अनुमति नहीं है.

JWT का इस्तेमाल करके मैसेज की पुष्टि करना

ऑर्डरिंग एंड-टू-एंड सर्वर से मिलने वाले आपके फ़ुलफ़िलमेंट एंडपॉइंट के अनुरोध में, सुरक्षा के मकसद से Authorization हेडर में हस्ताक्षर किया गया JSON Web Token (JWT) शामिल होता है. यह टोकन, शेयर की गई अनुमति देने वाली सेवा से जनरेट होता है. Google और लागू करने वाले आपके फ़ुलफ़िलमेंट एंडपॉइंट , दोनों को इस टोकन का इस्तेमाल किया जा सकता है.

  1. Google, अनुमति देने वाली सेवा और आपके फ़ूड ऑर्डरिंग प्रोजेक्ट के प्रोजेक्ट आईडी का इस्तेमाल करके, हस्ताक्षर किया गया JWT जनरेट करता है.
  2. Google, आपके फ़ुलफ़िलमेंट एंडपॉइंट पर हर अनुरोध के Authorization हेडर में हस्ताक्षर किया गया टोकन भेजता है.
  3. आपके एंडपॉइंट को, Google Auth लाइब्रेरी का इस्तेमाल करके साइन किए गए टोकन को डिकोड करना होगा. डिकोड किए गए टोकन में, प्रोजेक्ट आईडी, उसे जारी करने वाले, जारी करने वाले की जानकारी, उसके खत्म होने का समय, और जारी करने का समय जैसी जानकारी शामिल होती है. अनुरोध सही है या नहीं, इसका पता लगाने के लिए इस डेटा का इस्तेमाल करें.

अपने प्रोजेक्ट के लिए अनुरोध की पुष्टि लागू करने के लिए, यह तरीका अपनाएं:

  1. आने वाले अनुरोधों के Authorization हेडर से JWT निकालें.
  2. Google Auth लाइब्रेरी का इस्तेमाल करके टोकन को डीकोड करें.
  3. टोकन के audience को अपने प्रोजेक्ट आईडी पर सेट करें.
  4. टोकन पेलोड में दी गई अन्य जानकारी, प्रोजेक्ट आईडी, और जारी करने वाली कंपनी की पुष्टि करें. इससे पता चलेगा कि सही जानकारी है या नहीं.

Google की अनुमति लाइब्रेरी

'ऑर्डरिंग एंड-टू-एंड' सुविधा के मैसेज की पुष्टि करने और आपकी वेब सेवा से Google को भेजे जाने वाले मैसेज के लिए ऑथराइज़ेशन कोड जनरेट करने के लिए, अपनी पसंद की प्रोग्रामिंग भाषा में Google Auth लाइब्रेरी का इस्तेमाल करें:

अपनी वेब सेवा लागू करने के कोड में इनमें से कोई लाइब्रेरी डाउनलोड करें और जोड़ें.

अनुरोध की पुष्टि करने के उदाहरण

नीचे दिए गए उदाहरणों में, अनुरोध की पुष्टि की प्रोसेस को लागू करने का तरीका बताया गया है:

Node.js

const auth = require('google-auth-library')
const authClient = new auth.OAuth2Client()

/**
 * Verifies that an incoming request came from Google.
 * @param {String} idToken - The ID token used to verify the request
 * (i.e. The value found in the Authorization header of an incoming request).
 * @param {String} audience - The expected audience of the request
 * (i.e. The project ID for your project).
 * @return {boolean} True if request came from Google, false otherwise.
 */
function isRequestFromGoogle(idToken, audience) {
  authClient.verifyIdToken({idToken, audience}, (err, info) => {
    return !(err || info['iss'] !== 'https://accounts.google.com')
  })
}
    

Python

from google.oauth2 import id_token
from google.auth.transport import requests

def isRequestFromGoogle(audience, token):
    """ Verifies that an incoming request came from Google.

    Args:
        audience (str): The expected audience of the request
                        (i.e. The project ID for your project)
        token (str): The ID token used to verify the request
                     (i.e. The value found in the Authorization
                     header of an incoming request)
    Returns:
        True if the request came from Google, False otherwise.
    """
    id_info = id_token.verify_oauth2_token(token, requests.Request(), audience)
    return id_info['iss'] == 'https://accounts.google.com'
    

Java

/**
 * Verifies that an incoming request came from Google.
 * @param audience The expected audience of the request
 *                 (i.e. The project ID for your project)
 * @param token The ID token used to verify the request
 *              (i.e. The value found in the Authorization
 *              header of an incoming request)
 * @return {@code true} if request is from Google, else {@code false}
 */
public boolean isRequestFromGoogle(String audience, String token) {
  GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier
      .Builder(transport, jsonFactory)
      .setAudience(Collections.singletonList(audience))
      .build();

  GoogleIdToken idToken = verifier.verify(token);
  if (idToken == null) return false;
  Payload payload = idToken.getPayload();
  String issuer = (String) payload.get("iss");
  return issuer.equals("https://accounts.google.com");
}