概览
2022 年 2 月 16 日,我们 宣布使用更安全的 OAuth 流程来提高 Google OAuth 互动的安全性。本指南可帮助您了解必要的更改和步骤,以便成功从环回 IP 地址流迁移到受支持的替代方案。
这项措施是一项防范措施,可在与 Google 的 OAuth 2.0 授权端点互动期间防范钓鱼式攻击和应用冒充攻击。
什么是环回 IP 地址流程?
环回 IP 地址流程支持将环回 IP 地址或localhost
用作重定向 URI 的主机组件,其中在用户批准 OAuth 权限请求后将向其发送凭据。此流程容易受到中间人攻击,如果恶意应用在某些操作系统上访问相同的环回接口,则可能会截获授权服务器对指定重定向 URI 的响应,并获取授权代码的访问权限。
原生 iOS、Android 和 Chrome OAuth 客户端类型将不再支持环回 IP 地址流,但桌面设备应用中仍将支持该流。
重要合规日期
- 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 类型
转到 Google API Console 的 Credentials page ,然后在 OAuth 2.0 客户端 ID 部分下查看您的 OAuth 客户端 ID 类型。应用是以下任意一种:Web 应用、Android、iOS、Universal Windows Platform (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>
检查传出的网络调用
- Web 应用 - 检查 Chrome 上的网络活动
- Android - 使用 Network Inspector 检查网络流量
-
Chrome 应用
- 导航到 Chrome 扩展程序页面
- 选中扩展程序页面右上角的开发者模式复选框
- 选择要监控的扩展程序
- 在扩展程序页面的检查视图部分中点击后台页面链接
- 系统会打开开发者工具弹出式窗口,您可以在其中通过 “网络”标签页监控网络流量
- iOS - 使用付款方式分析 HTTP 流量
- Universal Windows Platform (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)
确定您的应用使用的环回 IP 地址流是 Android 或 iOS OAuth 客户端类型时,应迁移至我们的 Google 登录移动 SDK(Android、iOS)。
借助该 SDK,您可以轻松访问 Google API 并处理对 Google OAuth 2.0 授权端点的所有调用。
以下文档链接介绍了如何在不使用环回 IP 地址重定向 URI 的情况下,使用 Google 登录 SDK 访问 Google API。
访问 Google API (Android)
服务器端(离线)访问
以下示例展示了如何在 Android 的服务器端访问 Google API。Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); // request a one-time authorization code that your server exchanges for an // access token and sometimes refresh token String authCode = account.getServerAuthCode(); // Show signed-in UI updateUI(account); // TODO(developer): send code to server and exchange for access/refresh/ID tokens } catch (ApiException e) { Log.w(TAG, "Sign-in failed", e); updateUI(null); }
查看服务器端访问指南,了解如何从服务器端访问 Google API。
在 iOS 应用中访问 Google API
客户端访问
以下示例展示了如何在 iOS 设备上访问客户端 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
) 中添加访问令牌,或者通过
适用于 REST 的 Objective-C 的 Google API 客户端库使用提取工具授权方 (GTMFetcherAuthorizationProtocol
)。
查看客户端访问指南,了解如何在客户端访问 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 端点。