승인 및 확인

요청 메시지 확인을 구현하여 처리 엔드포인트에 대한 결제 및 주문 제출 요청이 Google에서 전송되도록 하고 승인되지 않은 제3자가 엔드포인트에 호출하지 못하도록 합니다.

JWT를 사용한 메시지 확인

엔드 투 엔드 서버에서 오는 처리 엔드포인트에 대한 요청의 경우 보안을 위해 Authorization 헤더에 서명된 JSON 웹 토큰 (JWT)이 포함됩니다. 토큰은 Google 및 fulfillment 엔드포인트 구현 모두에서 호출할 수 있는 공유 승인 서비스에 의해 생성됩니다.

  1. Google은 승인 서비스 및 음식 주문 프로젝트의 프로젝트 ID를 사용하여 서명된 JWT를 생성합니다.
  2. Google은 모든 요청의 Authorization 헤더에 있는 서명된 토큰을 처리 엔드포인트로 전송합니다.
  3. 엔드포인트는 Google 인증 라이브러리를 사용하여 서명된 토큰을 디코딩해야 합니다. 디코딩된 토큰에는 프로젝트 ID, 발급기관, 만료 시간, 발급 시간과 같은 세부정보가 포함됩니다. 이 데이터를 사용하여 요청의 진위성을 확인합니다.

프로젝트의 인증 요청을 구현하려면 다음 단계를 따르세요.

  1. 수신 요청의 Authorization 헤더에서 JWT를 추출합니다.
  2. Google 인증 라이브러리를 사용하여 토큰을 디코딩합니다.
  3. 토큰의 audience를 프로젝트 ID로 설정합니다.
  4. 토큰 페이로드에 포함된 발급기관, 프로젝트 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");
}