ใช้การยืนยันข้อความคำขอเพื่อให้แน่ใจว่าคำขอชำระเงินและส่งคำขอคำสั่งซื้อไปยังปลายทางการดำเนินการตามคำสั่งซื้อมาจาก Google และป้องกันไม่ให้บุคคลที่สามที่ไม่ได้รับอนุญาตเรียกใช้ปลายทางของคุณ
การยืนยันข้อความโดยใช้ JWT
คำขอที่ส่งไปยังปลายทาง Fulfillment ที่มาจากเซิร์ฟเวอร์การสั่งซื้อจากต้นทางถึงปลายทางมี JSON Web Token (JWT) ที่ลงนามแล้วในส่วนหัว Authorization
เพื่อความปลอดภัย โทเค็นจะสร้างขึ้นโดยบริการการให้สิทธิ์ที่แชร์ ซึ่งทั้ง Google และการใช้งานปลายทาง Fulfillment ของคุณได้
- Google จะสร้าง JWT ที่ลงนามโดยใช้บริการการให้สิทธิ์และรหัสโปรเจ็กต์ของโปรเจ็กต์การสั่งอาหาร
- Google จะส่งโทเค็นที่ลงนามแล้วในส่วนหัว
Authorization
ของคำขอทุกรายการไปยังปลายทาง Fulfillment - ปลายทางต้องถอดรหัสโทเค็นที่ลงนามโดยใช้ Google Auth Library โทเค็นที่ถอดรหัสแล้วมีรายละเอียดต่างๆ เช่น รหัสโปรเจ็กต์ ผู้ออก เวลาหมดอายุ และเวลาที่ออก ใช้ข้อมูลนี้เพื่อตรวจสอบความถูกต้องของคำขอ
หากต้องการใช้การยืนยันคำขอสำหรับโปรเจ็กต์ของคุณ ให้ทำตามขั้นตอนต่อไปนี้
- ดึงข้อมูล JWT จากส่วนหัว
Authorization
ของคำขอที่เข้ามาใหม่ - ถอดรหัสโทเค็นโดยใช้ไลบรารีการตรวจสอบสิทธิ์ของ Google
- ตั้งค่า
audience
ของโทเค็นเป็นรหัสโปรเจ็กต์ - ยืนยันผู้ออกใบรับรอง รหัสโปรเจ็กต์ และข้อมูลอื่นๆ ที่มีอยู่ในเพย์โหลดโทเค็นเพื่อความถูกต้อง
ไลบรารีการให้สิทธิ์ของ Google
หากต้องการยืนยันข้อความจากการสั่งซื้อจากต้นทางถึงปลายทาง และสร้างรหัสการให้สิทธิ์สำหรับข้อความที่บริการเว็บของคุณส่งไปยัง Google ให้ใช้ Google Auth Library ในภาษาโปรแกรมที่ต้องการ
- ไลบรารีการตรวจสอบสิทธิ์ของ Google สำหรับ Node.js
- ไลบรารีการตรวจสอบสิทธิ์ของ Google สำหรับ Python
- ไลบรารีการตรวจสอบสิทธิ์ของ Google สำหรับ Java
ดาวน์โหลดและเพิ่มไลบรารีเหล่านี้ลงในโค้ดการใช้งานบริการบนเว็บ
ขอตัวอย่างการยืนยัน
ตัวอย่างต่อไปนี้แสดงวิธีใช้งานการยืนยันคำขอ
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"); }