總覽
以 OAuth 為基礎的 Google 登入簡化連結功能除了加入 Google 登入外, OAuth 連結。這樣就能順暢連結 Google 使用者並可建立帳戶,讓使用者透過自己的 Google 帳戶在您的服務中建立新帳戶。
如要使用 OAuth 和 Google 登入功能進行帳戶連結,請遵循下列一般做法 步驟:
- 請先徵得使用者同意,允許存取自己的 Google 個人資料。
- 使用設定檔中的資訊檢查使用者帳戶是否存在。
- 如果是現有使用者,請連結這些帳戶。
- 如果在驗證系統中找不到對應的 Google 使用者, 驗證從 Google 收到的 ID 權杖。接著您可以根據需求建立使用者 儲存在 ID 權杖所含設定檔資訊上
圖 1. 透過簡化連結程序在使用者手機上進行帳戶連結
精簡連結規定
- 執行基本網站 OAuth 連結流程。您的服務必須支援 OAuth 2.0 規範 授權和憑證交換端點。
- 您的權杖交換端點必須支援 JSON Web Token (JWT) 斷言,並實作
check
、create
和get
意圖。
實作 OAuth 伺服器
您的權杖交換端點必須支援 check
、create
、get
意圖。下方顯示完成帳戶連結流程的步驟,並指出呼叫不同意圖的時機:
- 使用者是否在驗證系統中擁有帳戶?(使用者選取「是」或「否」)
- 是:使用者是否使用與其 Google 帳戶相關聯的電子郵件地址登入平台?(使用者選取「是」或「否」)
- 是 :使用者是否在驗證系統中擁有相符的帳戶?(呼叫
check intent
以確認)- 是:系統會呼叫
get intent
,如果成功傳回意圖,就會連結帳戶。 - 否 :建立新帳戶?(使用者選取「是」或「否」)
- 是:系統會呼叫
create intent
,如果建立意圖成功傳回,就會連結帳戶。 - 否 :系統會觸發網路 OAuth 流程、將使用者導向瀏覽器,並提供使用者其他電子郵件的連結選項。
- 是:系統會呼叫
- 是:系統會呼叫
- 否:系統會觸發網路 OAuth 流程、將使用者導向瀏覽器,並提供其他電子郵件的連結選項。
- 是 :使用者是否在驗證系統中擁有相符的帳戶?(呼叫
- 否 :使用者是否在驗證系統中擁有相符的帳戶?(呼叫
check intent
以確認)- 是:系統會呼叫
get intent
,如果成功傳回意圖,就會連結帳戶。 - 否:如果建立意圖成功傳回,系統會呼叫
create intent
,並連結帳戶。
- 是:系統會呼叫
- 是:使用者是否使用與其 Google 帳戶相關聯的電子郵件地址登入平台?(使用者選取「是」或「否」)
檢查現有的使用者帳戶 (檢查意圖)
使用者同意存取自己的 Google 個人資料後,Google 會傳送 要求,包含已簽署的 Google 使用者身分識別資訊。 聲明包含使用者的 Google 帳戶 ID、 姓名、姓名和電子郵件地址為叢集設定的憑證交換端點 就會處理該要求
如果驗證資訊中已有對應的 Google 帳戶
系統,權杖交換端點會以 account_found=true
回應。如果
Google 帳戶與現有使用者不相符,您的權杖交換端點
會傳回 account_found=false
的 HTTP 404 找不到錯誤。
這項要求的格式如下:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&intent=check&assertion=JWT&scope=SCOPES&client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET
您的權杖交換端點必須能處理下列參數:
權杖端點參數 | |
---|---|
intent |
在這些要求中,這個參數的值為
check 。 |
grant_type |
要交換的權杖類型。對於這些要求,這個
參數值為 urn:ietf:params:oauth:grant-type:jwt-bearer 。 |
assertion |
JSON Web Token (JWT),提供已簽署 識別使用者的身分JWT 所含資訊包括使用者的 Google 帳戶 ID、名稱和電子郵件地址。 |
client_id |
您指派給 Google 的用戶端 ID。 |
client_secret |
您指派給 Google 的用戶端密鑰。 |
如要回應 check
意圖要求,您的權杖交換端點必須執行下列步驟:
- 驗證並解碼 JWT 斷言。
- 檢查驗證系統是否已有 Google 帳戶。
Validate and decode the JWT assertion
You can validate and decode the JWT assertion by using a JWT-decoding library for your language. Use Google's public keys, available in JWK or PEM formats, to verify the token's signature.
When decoded, the JWT assertion looks like the following example:
{ "sub": "1234567890", // The unique ID of the user's Google Account "iss": "https://accounts.google.com", // The assertion's issuer "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID "iat": 233366400, // Unix timestamp of the assertion's creation time "exp": 233370000, // Unix timestamp of the assertion's expiration time "name": "Jan Jansen", "given_name": "Jan", "family_name": "Jansen", "email": "jan@gmail.com", // If present, the user's email address "email_verified": true, // true, if Google has verified the email address "hd": "example.com", // If present, the host domain of the user's GSuite email address // If present, a URL to user's profile picture "picture": "https://lh3.googleusercontent.com/a-/AOh14GjlTnZKHAeb94A-FmEbwZv7uJD986VOF1mJGb2YYQ", "locale": "en_US" // User's locale, from browser or phone settings }
In addition to verifying the token's signature, verify that the assertion's
issuer (iss
field) is https://accounts.google.com
, that the audience
(aud
field) is your assigned client ID, and that the token has not expired
(exp
field).
Using the email
, email_verified
and hd
fields you can determine if
Google hosts and is authoritative for an email address. In cases where Google is
authoritative the user is currently known to be the legitimate account owner
and you may skip password or other challenges methods. Otherwise, these methods
can be used to verify the account prior to linking.
Cases where Google is authoritative:
email
has a@gmail.com
suffix, this is a Gmail account.email_verified
is true andhd
is set, this is a G Suite account.
Users may register for Google Accounts without using Gmail or G Suite. When
email
does not contain a @gmail.com
suffix and hd
is absent Google is not
authoritative and password or other challenge methods are recommended to verify
the user. email_verified
can also be true as Google initially verified the
user when the Google account was created, however ownership of the third party
email account may have since changed.
檢查驗證系統是否已有 Google 帳戶
確認是否符合下列任一條件:
- 在聲明的「
sub
」欄位中,有 Google 帳戶 ID 代表您的使用者 資料庫 - 斷言中的電子郵件地址與您使用者資料庫中的使用者相符。
如果其中一個條件為 true,使用者已經註冊。在此情況下 會傳回類似以下的回應:
HTTP/1.1 200 Success Content-Type: application/json;charset=UTF-8 { "account_found":"true", }
如果 Google 帳戶 ID 和
斷言與資料庫中的使用者相符,表示使用者尚未註冊。於
在此情況下,您的權杖交換端點必須回覆 HTTP 404 錯誤
指定 "account_found": "false"
,如以下範例所示:
HTTP/1.1 404 Not found Content-Type: application/json;charset=UTF-8 { "account_found":"false", }
處理自動連結 (取得意圖)
使用者同意存取自己的 Google 個人資料後,Google 會傳送 要求,包含已簽署的 Google 使用者身分識別資訊。 聲明包含使用者的 Google 帳戶 ID、 姓名、姓名和電子郵件地址為叢集設定的憑證交換端點 就會處理該要求
如果驗證資訊中已有對應的 Google 帳戶
系統,您的權杖交換端點會傳回使用者權杖。如果
Google 帳戶與現有使用者不相符,您的權杖交換端點
會傳回 linking_error
錯誤和選用的 login_hint
。
這項要求的格式如下:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&intent=get&assertion=JWT&scope=SCOPES&client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET
您的權杖交換端點必須能處理下列參數:
權杖端點參數 | |
---|---|
intent |
在這些要求中,這個參數的值為 get 。 |
grant_type |
要交換的權杖類型。對於這些要求,這個
參數值為 urn:ietf:params:oauth:grant-type:jwt-bearer 。 |
assertion |
JSON Web Token (JWT),提供已簽署 識別使用者的身分JWT 所含資訊包括使用者的 Google 帳戶 ID、名稱和電子郵件地址。 |
scope |
選用:您設為 Google 要求存取範圍的任何範圍 使用者。 |
client_id |
您指派給 Google 的用戶端 ID。 |
client_secret |
您指派給 Google 的用戶端密鑰。 |
如要回應 get
意圖要求,您的權杖交換端點必須執行下列步驟:
- 驗證並解碼 JWT 斷言。
- 檢查驗證系統是否已有 Google 帳戶。
Validate and decode the JWT assertion
You can validate and decode the JWT assertion by using a JWT-decoding library for your language. Use Google's public keys, available in JWK or PEM formats, to verify the token's signature.
When decoded, the JWT assertion looks like the following example:
{ "sub": "1234567890", // The unique ID of the user's Google Account "iss": "https://accounts.google.com", // The assertion's issuer "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID "iat": 233366400, // Unix timestamp of the assertion's creation time "exp": 233370000, // Unix timestamp of the assertion's expiration time "name": "Jan Jansen", "given_name": "Jan", "family_name": "Jansen", "email": "jan@gmail.com", // If present, the user's email address "email_verified": true, // true, if Google has verified the email address "hd": "example.com", // If present, the host domain of the user's GSuite email address // If present, a URL to user's profile picture "picture": "https://lh3.googleusercontent.com/a-/AOh14GjlTnZKHAeb94A-FmEbwZv7uJD986VOF1mJGb2YYQ", "locale": "en_US" // User's locale, from browser or phone settings }
In addition to verifying the token's signature, verify that the assertion's
issuer (iss
field) is https://accounts.google.com
, that the audience
(aud
field) is your assigned client ID, and that the token has not expired
(exp
field).
Using the email
, email_verified
and hd
fields you can determine if
Google hosts and is authoritative for an email address. In cases where Google is
authoritative the user is currently known to be the legitimate account owner
and you may skip password or other challenges methods. Otherwise, these methods
can be used to verify the account prior to linking.
Cases where Google is authoritative:
email
has a@gmail.com
suffix, this is a Gmail account.email_verified
is true andhd
is set, this is a G Suite account.
Users may register for Google Accounts without using Gmail or G Suite. When
email
does not contain a @gmail.com
suffix and hd
is absent Google is not
authoritative and password or other challenge methods are recommended to verify
the user. email_verified
can also be true as Google initially verified the
user when the Google account was created, however ownership of the third party
email account may have since changed.
檢查驗證系統是否已有 Google 帳戶
確認是否符合下列任一條件:
- 在聲明的「
sub
」欄位中,有 Google 帳戶 ID 代表您的使用者 資料庫 - 斷言中的電子郵件地址與您使用者資料庫中的使用者相符。
如果找到使用者的帳戶,請核發存取權杖,並以 JSON 物件傳回 HTTPS 回應內文中的值,如以下範例所示:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
在某些情況下,根據 ID 權杖進行帳戶連結的使用者可能會失敗。如果是
因為無論出於什麼原因,您的權杖交換端點都必須以 HTTP
指定 error=linking_error
的 401 錯誤,如以下範例所示:
HTTP/1.1 401 Unauthorized Content-Type: application/json;charset=UTF-8 { "error":"linking_error", "login_hint":"foo@bar.com" }
當 Google 收到含有 linking_error
的 401 錯誤回應時,
將使用者傳送至您的授權端點,並以 login_hint
做為參數。
使用者在瀏覽器中使用 OAuth 連結流程完成帳戶連結。
Handle account creation via Google Sign-In (create intent)
When a user needs to create an account on your service, Google makes a request
to your token exchange endpoint that specifies intent=create
.
The request has the following form:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded response_type=token&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&scope=SCOPES&intent=create&assertion=JWT&client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET
Your token exchange endpoint must able to handle the following parameters:
Token endpoint parameters | |
---|---|
intent |
For these requests, the value of this parameter is create . |
grant_type |
The type of token being exchanged. For these requests, this
parameter has the value urn:ietf:params:oauth:grant-type:jwt-bearer . |
assertion |
A JSON Web Token (JWT) that provides a signed assertion of the Google user's identity. The JWT contains information that includes the user's Google Account ID, name, and email address. |
client_id |
The client ID you assigned to Google. |
client_secret |
The client secret you assigned to Google. |
The JWT within the assertion
parameter contains the user's Google Account ID,
name, and email address, which you can use to create a new account on your
service.
To respond to the create
intent requests, your token exchange endpoint must perform the following steps:
- Validate and decode the JWT assertion.
- Validate user information and create new account.
Validate and decode the JWT assertion
You can validate and decode the JWT assertion by using a JWT-decoding library for your language. Use Google's public keys, available in JWK or PEM formats, to verify the token's signature.
When decoded, the JWT assertion looks like the following example:
{ "sub": "1234567890", // The unique ID of the user's Google Account "iss": "https://accounts.google.com", // The assertion's issuer "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID "iat": 233366400, // Unix timestamp of the assertion's creation time "exp": 233370000, // Unix timestamp of the assertion's expiration time "name": "Jan Jansen", "given_name": "Jan", "family_name": "Jansen", "email": "jan@gmail.com", // If present, the user's email address "email_verified": true, // true, if Google has verified the email address "hd": "example.com", // If present, the host domain of the user's GSuite email address // If present, a URL to user's profile picture "picture": "https://lh3.googleusercontent.com/a-/AOh14GjlTnZKHAeb94A-FmEbwZv7uJD986VOF1mJGb2YYQ", "locale": "en_US" // User's locale, from browser or phone settings }
In addition to verifying the token's signature, verify that the assertion's
issuer (iss
field) is https://accounts.google.com
, that the audience
(aud
field) is your assigned client ID, and that the token has not expired
(exp
field).
Using the email
, email_verified
and hd
fields you can determine if
Google hosts and is authoritative for an email address. In cases where Google is
authoritative the user is currently known to be the legitimate account owner
and you may skip password or other challenges methods. Otherwise, these methods
can be used to verify the account prior to linking.
Cases where Google is authoritative:
email
has a@gmail.com
suffix, this is a Gmail account.email_verified
is true andhd
is set, this is a G Suite account.
Users may register for Google Accounts without using Gmail or G Suite. When
email
does not contain a @gmail.com
suffix and hd
is absent Google is not
authoritative and password or other challenge methods are recommended to verify
the user. email_verified
can also be true as Google initially verified the
user when the Google account was created, however ownership of the third party
email account may have since changed.
Validate user information and create new account
Check whether either of the following conditions are true:
- The Google Account ID, found in the assertion's
sub
field, is in your user database. - The email address in the assertion matches a user in your user database.
If either condition is true, prompt the user to link their existing account
with their Google Account. To do so, respond to the request with an HTTP 401 error
that specifies error=linking_error
and gives the user's email address as the
login_hint
. The following is a sample response:
HTTP/1.1 401 Unauthorized Content-Type: application/json;charset=UTF-8 { "error":"linking_error", "login_hint":"foo@bar.com" }
When Google receives a 401 error response with linking_error
, Google sends
the user to your authorization endpoint with login_hint
as a parameter. The
user completes account linking using the OAuth linking flow in their browser.
If neither condition is true, create a new user account with the information provided in the JWT. New accounts don't typically have a password set. It's recommended that you add Google Sign-In to other platforms to enable users to log in with Google across the surfaces of your application. Alternatively, you can email the user a link that starts your password recovery flow to allow the user to set a password to sign in on other platforms.
When the creation is completed, issue an access token and return the values in a JSON object in the body of your HTTPS response, like in the following example:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
取得 Google API 用戶端 ID
在帳戶連結註冊程序期間,系統會要求您提供 Google API 用戶端 ID。
如要使用您在完成 OAuth 連結步驟時建立的專案取得 API 用戶端 ID,若要這樣做,請完成下列步驟:
- 開啟 Cloud Shell 的「Credentials」(憑證) 頁面, Google API 控制台。
建立或選取 Google API 專案。
如果專案沒有網頁應用程式類型的用戶端 ID,請按一下 建立憑證 >OAuth 用戶端 ID要建立。請務必附上 在「已授權的 JavaScript 來源」方塊中找出您網站的網域。當你在 本機測試或開發,您必須同時新增
http://localhost
和 將http://localhost:<port_number>
設為「已授權的 JavaScript 來源」欄位。
驗證實作
您可以使用 OAuth 2.0 Playground 工具驗證實作結果。
請在工具中按照下列步驟操作:
- 點選「Configuration」圖示 ,開啟 OAuth 2.0 設定視窗。
- 在「OAuth 流程」欄位中,選取「用戶端」。
- 在「OAuth 端點」欄位中,選取「自訂」。
- 在對應的欄位中指定 OAuth 2.0 端點,以及您指派給 Google 的用戶端 ID。
- 在「步驟 1」部分中,請勿選取任何 Google 範圍。請改為將這個欄位留空,或輸入有效的伺服器範圍 (如果您不使用 OAuth 範圍,則輸入任意字串)。完成後,按一下「授權 API」。
- 在「步驟 2」和「步驟 3」部分,請完成 OAuth 2.0 流程,並確認每個步驟都能正常運作。
您可以使用 Google 帳戶連結示範工具驗證實作成果。
在工具中執行下列步驟:
- 按一下「使用 Google 帳戶登入」按鈕。
- 選擇要連結的帳戶。
- 輸入服務 ID。
- 您可以選擇輸入一或多個要申請存取權的範圍。
- 按一下「開始試用」。
- 系統顯示提示時,請確認您可以同意或拒絕連結要求。
- 確認系統是否會將你重新導向至平台。