架構外 (OOB) 流程遷移指南

總覽

我們在 2022 年 2 月 16 日 宣布計劃採用更安全的 OAuth 流程,藉此提高 Google OAuth 互動的安全性。本指南將協助您瞭解必要變更及步驟,以便順利從架構外 (OOB) 流程遷移至支援的替代方案。

我們希望藉由這項措施,在與 Google 的 OAuth 2.0 授權端點互動期間,防範網路釣魚和應用程式冒用攻擊。

什麼是 OOB?

OAuth 架構外 (OOB) 是一種舊版流程,也稱為手動複製/貼上選項,是開發的舊版流程,支援沒有重新導向 URI 的原生用戶端,可讓使用者在核准 OAuth 同意要求後接受憑證。OOB 流程造成遠端網路釣魚風險,因此客戶必須改用其他方法來防範這個安全漏洞。

所有用戶端類型 (網頁應用程式、Android、iOS、通用 Windows 平台 (UWP)、Chrome 應用程式、電視和限量輸入裝置、電腦應用程式) 都將淘汰 OOB 流程。

主要法規遵循日期

  • 2022 年 2 月 28 日:OOB 流程禁止新的 OAuth 使用情形
  • 2022 年 9 月 5 日 - 不符規定的 OAuth 要求可能會顯示向使用者顯示的警告訊息
  • 2022 年 10 月 3 日 - OOB 流程不適用於 2022 年 2 月 28 日前建立的 OAuth 用戶端
  • 2023 年 1 月 31 日 - 封鎖所有現有用戶端 (包括豁免的客戶)

不符規定的要求,系統會向使用者顯示錯誤訊息。這則訊息會向使用者說明應用程式遭到封鎖,並顯示您在 Google API 控制台中的 OAuth 同意畫面中註冊的支援電子郵件。

遷移程序有兩個主要步驟:
  1. 判斷您是否受到影響。
  2. 如果會受到影響,請改用更安全的替代方案。

判斷您是否受到影響

這項淘汰功能僅適用於正式版應用程式 (即發布狀態設為「 正式版」的應用程式)。處於 測試發布狀態的應用程式仍可繼續使用此流程。

請在 Google API Console 的 OAuth Consent Screen page中查看發布狀態,並在狀態為「運作中」的專案中使用 OOB 流程,然後繼續進行下一個步驟。

如何判斷應用程式是否使用 OOB 流程

檢查應用程式程式碼傳出網路呼叫 (如果應用程式使用 OAuth 程式庫),藉此判斷應用程式使用的 Google OAuth 授權要求是否使用 OOB 重新導向 URI 值。

檢查應用程式程式碼

查看應用程式程式碼的相關部分,其中會呼叫 Google OAuth 授權端點,並判斷 redirect_uri 參數是否具有下列任一值:
  • redirect_uri=urn:ietf:wg:oauth:2.0:oob
  • redirect_uri=urn:ietf:wg:oauth:2.0:oob:auto
  • redirect_uri=oob
OOB 重新導向流程要求範例如下所示:
https://accounts.google.com/o/oauth2/v2/auth?
response_type=code&
scope=<SCOPES>&
state=<STATE>&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
client_id=<CLIENT_ID>

檢查撥出網路通話

檢查網路呼叫的方法會因應用程式用戶端類型而異。
檢查網路呼叫時,請找出傳送至 Google OAuth 授權端點的要求,並判斷 redirect_uri 參數是否具有下列任一值:
  • redirect_uri=urn:ietf:wg:oauth:2.0:oob
  • redirect_uri=urn:ietf:wg:oauth:2.0:oob:auto
  • redirect_uri=oob
OOB 重新導向流程要求範例如下所示:
https://accounts.google.com/o/oauth2/v2/auth?
response_type=code&
scope=<SCOPES>&
state=<STATE>&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
client_id=<CLIENT_ID>

改用安全的替代方案

行動裝置用戶端 (Android / iOS)

如果您確定應用程式使用的是 Android 或 iOS OAuth 用戶端類型的 OOB 流程,則應改用 Google 登入 Mobile SDK (AndroidiOS)。

這個 SDK 可讓您輕鬆存取 Google API,並處理對 Google OAuth 2.0 授權端點的所有呼叫。

您可透過下方的說明文件連結,瞭解如何在不使用 OOB 重新導向 URI 的情況下,透過 Google 登入 SDK 存取 Google API。

在 Android 上存取 Google API

伺服器端 (離線) 存取
以下範例說明如何在 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 用戶端存取 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-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 應用程式用戶端的 OOB 流程,則應改用 Chrome Identity API

以下範例說明如何在不使用 OOB 重新導向 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 呼叫 Google 端點,請參閱 Chrome Identity API 指南

網頁應用程式

如果您判斷自己的應用程式使用的是網頁應用程式的 OOB 流程,則應改用我們的其中一個 Google API 用戶端程式庫。請參閱不同程式設計語言的用戶端程式庫

程式庫可讓您輕鬆存取 Google API,並處理對 Google 端點的所有呼叫。

伺服器端 (離線) 存取
使用伺服器端 (離線) 存取模式時,您必須執行下列操作:

以下程式碼片段是 NodeJS 範例,說明如何使用 Google Drive API 在伺服器端列出使用者的 Google 雲端硬碟檔案,而不必使用 OOB 重新導向 URI。

async function main() {
  const server = http.createServer(async function (req, res) {

  if (req.url.startsWith('/oauth2callback')) {
    let q = url.parse(req.url, true).query;

    if (q.error) {
      console.log('Error:' + q.error);
    } else {
      
      // Get access and refresh tokens (if access_type is offline)
      let { tokens } = await oauth2Client.getToken(q.code);
      oauth2Client.setCredentials(tokens);

      // Example of using Google Drive API to list filenames in user's Drive.
      const drive = google.drive('v3');
      drive.files.list({
        auth: oauth2Client,
        pageSize: 10,
        fields: 'nextPageToken, files(id, name)',
      }, (err1, res1) => {
        // TODO(developer): Handle response / error.
      });
    }
  }
}

請參閱 伺服器端網頁應用程式指南,瞭解如何從伺服器端存取 Google API。

用戶端存取權

以下 JavaScript 程式碼片段範例示範如何使用 Google API 在用戶端存取使用者的日曆活動。


// initTokenClient() initializes a new token client with your
// web app's client ID and the scope you need access to

const client = google.accounts.oauth2.initTokenClient({
  client_id: 'YOUR_GOOGLE_CLIENT_ID',
  scope: 'https://www.googleapis.com/auth/calendar.readonly',
  
  // callback function to handle the token response
  callback: (tokenResponse) => {
    if (tokenResponse && tokenResponse.access_token) { 
      gapi.client.setApiKey('YOUR_API_KEY');
      gapi.client.load('calendar', 'v3', listUpcomingEvents);
    }
  },
});

function listUpcomingEvents() {
  gapi.client.calendar.events.list(...);
}

請參閱 用戶端網頁應用程式指南,瞭解如何從用戶端存取 Google API。

電腦用戶端

如果您確定自己的應用程式會在電腦版用戶端上使用 OOB 流程,則應改用 回送 IP 位址 (localhost127.0.0.1) 流程