توکن های حامل را تأیید کنید

یک Bearer Token در سرصفحه Authorization هر درخواست HTTP اقدام درون‌برنامه تنظیم می‌شود. مثلا:

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" در مثال بالا نشانه مجوز حامل است. این یک توکن رمزنگاری است که توسط گوگل تولید شده است. تمام نشانه‌های حامل ارسال شده با اکشن‌ها دارای فیلد azp (حزب مجاز) به‌عنوان gmail@system.gserviceaccount.com هستند که در قسمت audience ، دامنه فرستنده به‌عنوان نشانی اینترنتی فرم https:// مشخص می‌شود. برای مثال، اگر ایمیل از noreply@example.com باشد، مخاطب https://example.com است.

اگر از نشانه‌های حامل استفاده می‌کنید، بررسی کنید که درخواست از طرف Google است و برای دامنه فرستنده در نظر گرفته شده است. اگر رمز تأیید نشد، سرویس باید با کد پاسخ HTTP 401 (Unauthorized) به درخواست پاسخ دهد.

توکن های حامل بخشی از استاندارد OAuth V2 هستند و به طور گسترده توسط API های Google پذیرفته شده اند.

تأیید توکن های حامل

خدمات تشویق می شوند از کتابخانه منبع باز Google API Client برای تأیید توکن های حامل استفاده کنند:

جاوا

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

پایتون

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')