リクエスト メッセージの検証を実装して、フルフィルメント エンドポイントへの「購入手続き」リクエストと「注文送信」リクエストが Google から送信されるようにし、権限のない第三者がエンドポイントを呼び出すのを防ぎます。
JWT を使用したメッセージの検証
注文エンドツーエンド サーバーからのフルフィルメント エンドポイントへのリクエストでは、セキュリティのために Authorization
ヘッダーに署名 JSON Web Token(JWT)が含まれます。トークンは、Google とフルフィルメント エンドポイント実装の両方が呼び出すことができる共有認可サービスによって生成されます。
- Google は、認可サービスと Food Ordering プロジェクトのプロジェクト ID を使用して、署名付き JWT を生成します。
- Google は、すべてのリクエストの
Authorization
ヘッダーで署名付きトークンをフルフィルメント エンドポイントに送信します。 - エンドポイントは、Google 認証ライブラリを使用して署名付きトークンをデコードする必要があります。デコードされたトークンには、プロジェクト ID、発行元、有効期限、発行時刻などの詳細が含まれています。このデータを使用して、リクエストの信頼性を判断します。
プロジェクトにリクエストの確認を実装する手順は次のとおりです。
- 受信リクエストの
Authorization
ヘッダーから JWT を抽出します。 - Google 認証ライブラリを使用してトークンをデコードします。
- トークンの
audience
をプロジェクト ID に設定します。 - 発行元、プロジェクト ID、トークン ペイロードに含まれるその他の情報が正しいことを確認して、
Google 承認ライブラリ
Ordering End-to-End からのメッセージを検証し、ウェブサービスから Google に送信するメッセージの認証コードを生成するには、選択したプログラミング言語の Google 認証ライブラリを使用します。
これらのライブラリのいずれかをダウンロードして、ウェブサービス実装コードに追加します。
リクエストの確認の例
次の例は、リクエスト検証の実装方法を示しています。
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"); }