署名なしトークンの確認

Bearer Token は、すべてのアプリ内アクションの HTTP リクエストの 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:// 形式の URL として送信者ドメインを指定します。たとえば、メールが noreply@example.com から送信されている場合、オーディエンスは https://example.com です。

署名なしトークンを使用する場合は、リクエストが Google から送信されており、送信者ドメイン宛てであることを確認します。トークンが検証されない場合、サービスは HTTP レスポンス コード 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')