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

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

JWT का उपयोग करके संदेश सत्यापन

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

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

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

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

Google की ऑथराइज़ेशन लाइब्रेरी

Order with 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");
}