每個應用程式內動作 HTTP 要求的 Authorization
標頭中都會設定 Bearer Token
。例如:
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://
。舉例來說,如果電子郵件來自 noreply@example.com
,
目標對象為 https://example.com
。
如果使用的是不記名權杖,請確認要求來自 Google
適用於寄件者網域。如果權杖未通過驗證,服務應
請以 HTTP 回應代碼 401 (Unauthorized)
回應要求。
不記名權杖是 OAuth V2 標準的一部分,並且被 Google API 廣泛採用。
驗證不記名權杖
建議使用開放原始碼 Google API 用戶端程式庫來驗證不記名權杖:
- Java:https://github.com/google/google-api-java-client
- Python:https://github.com/google/google-api-python-client
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')