總覽
我們在 2022 年 2 月 16 日 宣布,將採用更安全的 OAuth 流程,讓 Google OAuth 互動更安全。本指南可協助您瞭解從迴圈 IP 位址流程成功遷移至支援的替代方案所需的變更和步驟。
這項措施是防範措施,可在與 Google OAuth 2.0 授權端點互動時,防範網路釣魚和應用程式冒用攻擊。
迴送 IP 位址流量是什麼?
回送 IP 位址流程支援使用回送 IP 位址或localhost
做為重新導向 URI 的主機元件,在使用者核准 OAuth 同意聲明要求後,系統會將憑證傳送至該元件。這個流程容易遭受中間人攻擊,在這種攻擊中,惡意應用程式會在某些作業系統上存取相同的迴圈介面,攔截授權伺服器傳送給指定重新導向 URI 的回應,並取得授權碼。回送 IP 位址流程已淘汰原生 iOS、Android 和 Chrome OAuth 用戶端類型,但仍會繼續支援桌面應用程式。
重要法規遵循日期
- 2022 年 3 月 14 日:禁止新的 OAuth 用戶端使用迴送 IP 位址流程
- 2022 年 8 月 1 日:系統可能會針對不符合規定的 OAuth 要求,向使用者顯示警告訊息。
- 2022 年 8 月 31 日:針對 2022 年 3 月 14 日前建立的原生 Android、Chrome 應用程式和 iOS OAuth 用戶端,回送 IP 位址流程將遭到封鎖
- 2022 年 10 月 21 日 - 所有現有用戶都會遭到封鎖 (包括豁免的用戶)
系統會針對不符規定的要求,顯示使用者面向的錯誤訊息。這則訊息會向使用者說明應用程式遭到封鎖,並顯示您在 Google API 控制台 OAuth 同意畫面中註冊的支援電子郵件地址。
- 判斷是否受到影響。
- 如果受到影響,請改用支援的替代方案。
判斷是否受到影響
查看 OAuth 用戶端 ID 類型
前往 的 ,然後在「OAuth 2.0 用戶端 ID」部分查看 OAuth 用戶端 ID 類型。您可以選擇下列任一選項:網頁應用程式、Android、iOS、通用 Windows 平台 (UWP)、Chrome 應用程式、電視和輸入受限的裝置、電腦版應用程式。
如果用戶端類型為 Android、Chrome 應用程式或 iOS,且您使用回送 IP 位址流程,請繼續進行下一個步驟。
如果您在電腦應用程式 OAuth 用戶端上使用迴送 IP 位址流程,則無須採取任何與這項淘汰作業相關的行動,因為我們會繼續支援這類 OAuth 用戶端類型的用途。
如何判斷應用程式是否使用迴送 IP 位址流量
檢查應用程式程式碼或傳出網路呼叫 (如果應用程式使用 OAuth 程式庫),以判斷應用程式發出的 Google OAuth 授權要求是否使用迴送重新導向 URI 值。
檢查應用程式程式碼
redirect_uri
參數是否有下列任何值:
-
redirect_uri=http://127.0.0.1:<port>
例如:redirect_uri=http://127.0.0.1:3000
-
redirect_uri=http://[::1]:<port>
例如:redirect_uri=http://[::1]:3000
-
redirect_uri=http://localhost:<port>
例如:redirect_uri=http://localhost:3000
https://accounts.google.com/o/oauth2/v2/auth? redirect_uri=http://localhost:3000& response_type=code& scope=<SCOPES>& state=<STATE>& client_id=<CLIENT_ID>
檢查外送網路呼叫
- 網路應用程式 - 檢查 Chrome 中的網路活動
- Android - 使用網路檢查器檢查網路流量
-
Chrome 應用程式
- 前往 Chrome 擴充功能頁面
- 勾選擴充功能頁面右上角的「開發人員模式」核取方塊
- 選取要監控的擴充功能
- 按一下擴充功能頁面「檢查檢視畫面」部分中的「背景頁面」連結
- 系統會開啟「開發人員工具」彈出式視窗,您可以在「網路」分頁中監控網路流量。
- iOS - 使用 Instruments 分析 HTTP 流量
- 通用 Windows 平台 (UWP) - 在 Visual Studio 中檢查網路流量
- 電腦版應用程式: 使用網路擷取工具,適用於應用程式開發的作業系統
redirect_uri
參數是否有下列任何值:
-
redirect_uri=http://127.0.0.1:<port>
例如:redirect_uri=http://127.0.0.1:3000
-
redirect_uri=http://[::1]:<port>
例如:redirect_uri=http://[::1]:3000
-
redirect_uri=http://localhost:<port>
例如:redirect_uri=http://localhost:3000
https://accounts.google.com/o/oauth2/v2/auth? redirect_uri=http://localhost:3000& response_type=code& scope=<SCOPES>& state=<STATE>& client_id=<CLIENT_ID>
改用支援的替代方案
行動用戶端 (Android / iOS)
如果您判斷應用程式使用的是 Android 或 iOS OAuth 用戶端類型的迴送 IP 位址流量,則應改用建議的 SDK (Android、iOS)。
這個 SDK 可讓您輕鬆存取 Google API,並處理所有對 Google OAuth 2.0 授權端點的呼叫。
下方的說明文件連結提供資訊,說明如何使用建議的 SDK 存取 Google API,而不需要使用迴圈 IP 位址重新導向 URI。
在 Android 上存取 Google API
用戶端存取
以下範例說明如何使用建議的 Google Identity 服務 Android 程式庫,在 Android 用戶端存取 Google API。
ListrequestedScopes = Arrays.asList(DriveScopes.DRIVE_APPDATA); AuthorizationRequest authorizationRequest = AuthorizationRequest.builder().setRequestedScopes(requestedScopes).build(); Identity.getAuthorizationClient(activity) .authorize(authorizationRequest) .addOnSuccessListener( authorizationResult -> { if (authorizationResult.hasResolution()) { // Access needs to be granted by the user PendingIntent pendingIntent = authorizationResult.getPendingIntent(); try { startIntentSenderForResult(pendingIntent.getIntentSender(), REQUEST_AUTHORIZE, null, 0, 0, 0, null); } catch (IntentSender.SendIntentException e) { Log.e(TAG, "Couldn't start Authorization UI: " + e.getLocalizedMessage()); } } else { // Access already granted, continue with user action saveToDriveAppFolder(authorizationResult); } }) .addOnFailureListener(e -> Log.e(TAG, "Failed to authorize", e));
將 authorizationResult
傳遞至您定義的方法,即可將內容儲存至使用者的雲端硬碟資料夾。authorizationResult
具有會傳回存取權杖的
getAccessToken()
方法。
伺服器端 (離線) 存取
以下範例說明如何在 Android 的伺服器端存取 Google API。ListrequestedScopes = Arrays.asList(DriveScopes.DRIVE_APPDATA); AuthorizationRequest authorizationRequest = AuthorizationRequest.builder() .requestOfflineAccess(webClientId) .setRequestedScopes(requestedScopes) .build(); Identity.getAuthorizationClient(activity) .authorize(authorizationRequest) .addOnSuccessListener( authorizationResult -> { if (authorizationResult.hasResolution()) { // Access needs to be granted by the user PendingIntent pendingIntent = authorizationResult.getPendingIntent(); try { startIntentSenderForResult(pendingIntent.getIntentSender(), REQUEST_AUTHORIZE, null, 0, 0, 0, null); } catch (IntentSender.SendIntentException e) { Log.e(TAG, "Couldn't start Authorization UI: " + e.getLocalizedMessage()); } } else { String authCode = authorizationResult.getServerAuthCode(); } }) .addOnFailureListener(e -> Log.e(TAG, "Failed to authorize", e));
authorizationResult
具有
getServerAuthCode()
方法,可傳回授權碼,您可以將該授權碼傳送至後端,以取得存取權和更新權杖。
在 iOS 應用程式中存取 Google API
用戶端存取
以下範例說明如何在 iOS 用戶端上存取 Google API。
user.authentication.do { authentication, error in guard error == nil else { return } guard let authentication = authentication else { return } // Get the access token to attach it to a REST or gRPC request. let accessToken = authentication.accessToken // Or, get an object that conforms to GTMFetcherAuthorizationProtocol for // use with GTMAppAuth and the Google APIs client library. let authorizer = authentication.fetcherAuthorizer() }
使用存取權杖呼叫 API,方法是將存取權杖加入 REST 或 gRPC 要求 (Authorization: Bearer ACCESS_TOKEN
) 的標頭,或是使用擷取器授權者 (GTMFetcherAuthorizationProtocol
) 搭配
REST 適用的 Objective-C 版 Google API 用戶端程式庫。
請參閱用戶端存取權指南,瞭解如何在用戶端存取 Google API。如何在用戶端存取 Google API。
伺服器端 (離線) 存取權
以下範例說明如何在伺服器端存取 Google API,以支援 iOS 用戶端。GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in guard error == nil else { return } guard let user = user else { return } // request a one-time authorization code that your server exchanges for // an access token and refresh token let authCode = user.serverAuthCode }
請參閱伺服器端存取權指南,瞭解如何從伺服器端存取 Google API。
Chrome 應用程式用戶端
如果您判斷應用程式在 Chrome 應用程式用戶端上使用回送 IP 位址流程,應改用 Chrome Identity API。
以下範例說明如何在不使用迴圈 IP 位址重新導向 URI 的情況下,取得所有使用者聯絡人。
window.onload = function() { document.querySelector('button').addEventListener('click', function() { // retrieve access token chrome.identity.getAuthToken({interactive: true}, function(token) { // .......... // the example below shows how to use a retrieved access token with an appropriate scope // to call the Google People API contactGroups.get endpoint fetch( 'https://people.googleapis.com/v1/contactGroups/all?maxMembers=20&key=API_KEY', init) .then((response) => response.json()) .then(function(data) { console.log(data) }); }); }); };
請參閱 Chrome Identity API 指南,進一步瞭解如何存取已驗證的使用者,以及如何透過 Chrome Identity API 呼叫 Google 端點。