बेयर टोकन की पुष्टि करें

Bearer Token को हर इन-ऐप्लिकेशन ऐक्शन एचटीटीपी अनुरोध के Authorization हेडर में सेट किया जाता है. उदाहरण के लिए:

POST /approve?expenseId=abc123 HTTP/1.1
Host: your-domain.com
Authorization: Bearer AbCdEf123456
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/1.0 (KHTML, like Gecko; Gmail Actions)

confirmed=Approved

ऊपर दिए गए उदाहरण में, "AbCdEf123456" स्ट्रिंग, बेयरर ऑथराइज़ेशन टोकन है. यह Google का बनाया हुआ क्रिप्टोग्राफ़िक टोकन है. कार्रवाइयों के साथ भेजे गए सभी बेयरर टोकन में, azp (आधिकारिक पक्ष) फ़ील्ड, gmail@system.gserviceaccount.com के तौर पर होता है. इसमें audience फ़ील्ड में, भेजने वाले के डोमेन को फ़ॉर्म के यूआरएल के तौर पर https:// बताया जाता है. उदाहरण के लिए, अगर ईमेल noreply@example.com से आया है, तो ऑडियंस https://example.com है.

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

बेयरर टोकन, OAuth V2 स्टैंडर्ड का हिस्सा हैं. Google API ज़्यादातर इन्हें इस्तेमाल करता है.

बेयरर टोकन की पुष्टि करना

बेयरर टोकन की पुष्टि करने के लिए, सेवाओं को ओपन सोर्स Google API क्लाइंट लाइब्रेरी का इस्तेमाल करने के लिए बढ़ावा दिया जाता है:

Java

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collections;

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;

public class TokenVerifier {
    // Bearer Tokens from Gmail Actions will always be issued to this authorized party.
    private static final String GMAIL_AUTHORIZED_PARTY = "gmail@system.gserviceaccount.com";

    // Intended audience of the token, based on the sender's domain
    private static final String AUDIENCE = "https://example.com";

    public static void main(String[] args) throws GeneralSecurityException, IOException {
        // Get this value from the request's Authorization HTTP header.
        // For example, for "Authorization: Bearer AbCdEf123456" use "AbCdEf123456"
        String bearerToken = "AbCdEf123456";

        GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new ApacheHttpTransport(), new JacksonFactory())
                .setAudience(Collections.singletonList(AUDIENCE))
                .build();

        GoogleIdToken idToken = verifier.verify(bearerToken);
        if (idToken == null || !idToken.getPayload().getAuthorizedParty().equals(GMAIL_AUTHORIZED_PARTY)) {
            System.out.println("Invalid token");
            System.exit(-1);
        }

        // Token originates from Google and is targeted to a specific client.
        System.out.println("The token is valid");

        System.out.println("Token details:");
        System.out.println(idToken.getPayload().toPrettyString());
    }
}

Python

import sys

from oauth2client import client

# Bearer Tokens from Gmail Actions will always be issued to this authorized party.
GMAIL_AUTHORIZED_PARTY = 'gmail@system.gserviceaccount.com'

# Intended audience of the token, based on the sender's domain
AUDIENCE = 'https://example.com'

try:
  # Get this value from the request's Authorization HTTP header.
  # For example, for "Authorization: Bearer AbCdEf123456" use "AbCdEf123456"
  bearer_token = 'AbCdEf123456'

  # Verify valid token, signed by google.com, intended for a third party.
  token = client.verify_id_token(bearer_token, AUDIENCE)
  print('Token details: %s' % token)

  if token['azp'] != GMAIL_AUTHORIZED_PARTY:
    sys.exit('Invalid authorized party')
except:
  sys.exit('Invalid token')

# Token originates from Google and is targeted to a specific client.
print('The token is valid')