在 App Engine 中登入的使用者

本教學課程說明如何使用 Identity Platform、App Engine 標準環境和 Datastore 擷取、驗證及儲存第三方憑證。

本文件將逐步說明如何使用 Firenotes 這個簡易的筆記工具應用程式,將使用者的筆記儲存在自己的個人筆記本中。筆記本會依使用者儲存,並以每位使用者的專屬 Identity Platform ID 識別。應用程式包含下列元件:

  • 前端會設定登入使用者介面,並擷取 Identity Platform ID。此外,這個應用程式也會處理驗證狀態變更,並讓使用者查看記事。

  • FirebaseUI 是開放原始碼的置入式解決方案,可簡化驗證和 UI 工作。SDK 會處理使用者登入、將多個供應商連結至一個帳戶、復原密碼等作業。採用驗證最佳做法,提供流暢安全的登入體驗。

  • 後端會驗證使用者的驗證狀態,並傳回使用者個人資料和其筆記。

應用程式會使用 NDB 用戶端程式庫將使用者憑證儲存在 Datastore 中,但您也可以將憑證儲存在自選的資料庫中。

Firenotes 是以 Flask 網頁應用程式架構為基礎。範例應用程式使用 Flask 是因為它簡單易用。但不論您使用哪一個架構,本文探討的概念和技術皆適用。

目標

完成本教學課程後,您將完成以下目標:

  • 使用 Identity Platform 的 FirebaseUI 設定使用者介面。
  • 取得 Identity Platform ID 權杖,並使用伺服器端驗證機制進行驗證。
  • 將使用者憑證和相關聯的資料儲存在 Datastore。
  • 使用 NDB 用戶端程式庫查詢資料庫。
  • 將應用程式部署至 App Engine。

費用

本教學課程使用 Google Cloud的計費元件,包括:

  • Datastore
  • Identity Platform

使用 Pricing Calculator,根據預測用量估算費用。

初次使用 Google Cloud 的使用者可能符合免費試用期資格。

事前準備

  1. 安裝 Git、Python 2.7 和 virtualenv。如要進一步瞭解如何設定 Python 開發環境 (例如安裝最新版本 Python),請參閱 Google Cloud的「設定 Python 開發環境」。
  2. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  3. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  4. Install the Google Cloud CLI.

  5. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  6. To initialize the gcloud CLI, run the following command:

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init

如果您已在其他專案中安裝並初始化 SDK,請將 gcloud 專案設為您用於 Firenotes 的 App Engine 專案 ID。如需使用 gcloud 工具更新專案的特定指令,請參閱「管理 Google Cloud SDK 設定」。

複製範例應用程式

如要將範例應用程式下載到您的本機電腦上:

  1. 將範例應用程式存放區複製到本機電腦:

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

    您也可以下載 ZIP 檔案格式的範例,然後將檔案解壓縮。

  2. 前往包含程式碼範例的目錄:

    cd python-docs-samples/appengine/standard/firebase/firenotes
    

新增使用者介面

如要設定 Identity Platform 的 FirebaseUI 並啟用識別資訊提供者,請按照下列步驟操作:

  1. 請按照下列步驟,將 Identity Platform 新增至應用程式:

    1. 前往 Google Cloud 控制台。
      前往 Google Cloud 控制台
    2. 選取要使用的 Google Cloud 專案:
      • 如果您已有專案,請在頁面頂端的「Select organization」(選取組織) 下拉式選單中選取該專案。
      • 如果沒有 Google Cloud 專案,請在Google Cloud 控制台中建立新專案。
    3. 前往 Google Cloud 控制台的「Identity Platform Marketplace」頁面。
      前往 Identity Platform Marketplace 頁面
    4. 在 Identity Platform Marketplace 頁面,按一下「Enable Customer Identity」(啟用 Customer Identity)。
    5. 前往 Google Cloud 控制台的「Customer Identity Users」(客戶身分使用者) 頁面。
      前往「使用者」頁面
    6. 按一下右上方的「應用程式設定詳細資料」。
    7. 將應用程式設定詳細資料複製到網頁應用程式。

      // This code is for illustration purposes only.
      
      // Obtain the following from the "Add Firebase to your web app" dialogue
      // Initialize Firebase
      var config = {
        apiKey: "<API_KEY>",
        authDomain: "<PROJECT_ID>.firebaseapp.com",
        databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
        projectId: "<PROJECT_ID>",
        storageBucket: "<BUCKET>.appspot.com",
        messagingSenderId: "<MESSAGING_SENDER_ID>"
      };
  2. 編輯 backend/app.yaml 檔案,在 env_variables 區段中新增 GOOGLE_CLOUD_PROJECT : 'PROJECT_ID':

    # Copyright 2021 Google LLC
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    # This code is designed for Python 2.7 and
    # the App Engine first-generation Runtime which has reached End of Support.
    
    runtime: python27
    api_version: 1
    threadsafe: true
    service: backend
    
    handlers:
    - url: /.*
      script: main.app
    
    env_variables:
      GAE_USE_SOCKETS_HTTPLIB : 'true'
    
  3. 在 frontend/main.js 檔案中,選取要提供給使用者的供應商,設定 FirebaseUI 登入小工具。

    // This code is for illustration purposes only.
    
    // Firebase log-in widget
    function configureFirebaseLoginWidget() {
      var uiConfig = {
        'signInSuccessUrl': '/',
        'signInOptions': [
          // Leave the lines as is for the providers you want to offer your users.
          firebase.auth.GoogleAuthProvider.PROVIDER_ID,
          firebase.auth.FacebookAuthProvider.PROVIDER_ID,
          firebase.auth.TwitterAuthProvider.PROVIDER_ID,
          firebase.auth.GithubAuthProvider.PROVIDER_ID,
          firebase.auth.EmailAuthProvider.PROVIDER_ID
        ],
        // Terms of service url
        'tosUrl': '<your-tos-url>',
      };
    
      var ui = new firebaseui.auth.AuthUI(firebase.auth());
      ui.start('#firebaseui-auth-container', uiConfig);
    }
  4. 在 Google Cloud 控制台,啟用您選擇保留的供應商:

    1. 前往 Google Cloud 控制台的「Customer Identity Providers」(客戶識別資訊提供者) 頁面。
      前往「供應商」頁面
    2. 按一下「Add A Provider」。
    3. 在「Select a provider」(選取提供者) 下拉式選單中,選取要使用的提供者。
    4. 按一下「啟用」旁的按鈕,啟用供應商。
      • 如果是第三方識別資訊提供者,請輸入提供者開發人員網站上的提供者 ID 和密鑰。Firebase 文件在 Facebook、Twitter 和 GitHub 指南的「事前準備」部分提供了具體操作說明。
      • 如要整合 SAML 和 OIDC,請參閱 IdP 的設定。
  5. 將網域新增至 Identity Platform 的授權網域清單:

    1. 前往 Google Cloud 控制台的「Customer Identity Settings」(客戶身分設定) 頁面。
      前往「設定」頁面
    2. 在「已授權網域」下方,按一下「新增網域」。
    3. 請用以下格式輸入應用程式的網域:

      [PROJECT_ID].appspot.com
      

      網域名稱前請勿加上 http://。

安裝依附元件

  1. 前往 backend 目錄,完成應用程式設定:

    cd backend/
    
  2. 將依附元件安裝到專案的 lib 目錄中:

    pip install -t lib -r requirements.txt
    
  3. 在 appengine_config.py 中,vendor.add() 方法會註冊 lib 目錄中的程式庫。

在本機執行應用程式

如要在本機執行應用程式,請使用 App Engine 本機開發伺服器:

  1. 在 main.js 中,將下列網址新增為 backendHostURL:

    http://localhost:8081

  2. 前往應用程式的根目錄,然後啟動開發伺服器:

    dev_appserver.py frontend/app.yaml backend/app.yaml
    
  3. 使用網路瀏覽器前往 http://localhost:8080/。

在伺服器上驗證使用者

您已設定專案並初始化開發應用程式,現在可以逐步查看程式碼,瞭解如何在伺服器上擷取及驗證 Identity Platform ID 權杖。

從 Identity Platform 取得 ID 權杖

伺服器端驗證的第一步是擷取要驗證的存取憑證。驗證要求會由 Identity Platform 的 onAuthStateChanged() 監聽器處理:

firebase.auth().onAuthStateChanged(function (user) {
  if (user) {
    $('#logged-out').hide();
    var name = user.displayName;

    /* If the provider gives a display name, use the name for the
    personal welcome message. Otherwise, use the user's email. */
    var welcomeName = name ? name : user.email;

    user.getIdToken().then(function (idToken) {
      userIdToken = idToken;

      /* Now that the user is authenicated, fetch the notes. */
      fetchNotes();

      $('#user').text(welcomeName);
      $('#logged-in').show();

    });

  } else {
    $('#logged-in').hide();
    $('#logged-out').show();

  }
});

使用者登入後,回呼中的 Identity Platform getToken() 方法會以 JSON Web Token (JWT) 的形式傳回 Identity Platform ID 權杖。

在伺服器上驗證憑證

使用者登入後,前端服務會透過 AJAX GET 要求,擷取使用者筆記本中的任何現有記事。這項操作需要授權才能存取使用者資料,因此 JWT 會使用 Bearer 結構定義,在要求的 Authorization 標頭中傳送:

// Fetch notes from the backend.
function fetchNotes() {
  $.ajax(backendHostUrl + '/notes', {
    /* Set header for the XMLHttpRequest to get data from the web server
    associated with userIdToken */
    headers: {
      'Authorization': 'Bearer ' + userIdToken
    }
  })

用戶端必須先驗證權杖是由 Identity Platform 簽署,才能存取伺服器資料。您可以使用 Python 適用的 Google 驗證程式庫驗證這個權杖。使用驗證程式庫的 verify_firebase_token 函式驗證不記名權杖,並擷取要求:

# This code is for illustration purposes only.

id_token = request.headers["Authorization"].split(" ").pop()
claims = google.oauth2.id_token.verify_firebase_token(
    id_token, HTTP_REQUEST, audience=os.environ.get("GOOGLE_CLOUD_PROJECT")
)
if not claims:
    return "Unauthorized", 401

每個識別資訊提供者都會傳送一組不同的聲明,但每個提供者至少會有一個包含不重複使用者 ID 的 sub 聲明,以及一個提供部分設定檔資訊 (例如 name 或 email) 的聲明,您可以使用這些資訊在應用程式中提供個人化使用者體驗。

管理 Datastore 中的使用者資料

驗證使用者後,您必須儲存他們的資料,才能在登入工作階段結束後保留資料。以下各節說明如何將記事儲存為 Datastore 實體,並依使用者 ID 區隔實體。

建立實體以儲存使用者資料

您可以在 Datastore 中建立實體,方法是宣告具有特定屬性 (例如整數或字串) 的 NDB 模型類別。Datastore 資料儲存庫索引會依「種類」為實體建立索引;以 Firenotes 來說,每個實體的種類都是 Note。為了方便查詢,每個 Note 都會與金鑰名稱一起儲存,也就是從上一節的 sub 宣告中取得的使用者 ID。

以下程式碼會示範如何設定實體的屬性,包括在建立實體時,如何使用模型類別的建構函式方法進行設定,以及建立實體後,如何指派個別屬性的方式:

# This code is for illustration purposes only.

data = request.get_json()

# Populates note properties according to the model,
# with the user ID as the key name.
note = Note(parent=ndb.Key(Note, claims["sub"]), message=data["message"])

# Some providers do not provide one of these so either can be used.
note.friendly_id = claims.get("name", claims.get("email", "Unknown"))

如要將新建立的 Note 寫入 Datastore,請在 note 物件上呼叫 put() 方法。

擷取使用者資料

如要擷取與特定使用者 ID 相關聯的使用者資料,請使用 NDB query() 方法,在資料庫中搜尋相同實體群組中的記事。同一群組或祖系路徑中的實體會共用一個索引鍵名稱,在本例中為使用者 ID。

# This code is for illustration purposes only.

def query_database(user_id):
    """Fetches all notes associated with user_id.

    Notes are ordered them by date created, with most recent note added
    first.
    """
    ancestor_key = ndb.Key(Note, user_id)
    query = Note.query(ancestor=ancestor_key).order(-Note.created)
    notes = query.fetch()

    note_messages = []

    for note in notes:
        note_messages.append(
            {
                "friendly_id": note.friendly_id,
                "message": note.message,
                "created": note.created,
            }
        )

    return note_messages

您可以接著擷取查詢資料,並在用戶端顯示筆記:

// This code is for illustration purposes only.

// Fetch notes from the backend.
function fetchNotes() {
  $.ajax(backendHostUrl + '/notes', {
    /* Set header for the XMLHttpRequest to get data from the web server
    associated with userIdToken */
    headers: {
      'Authorization': 'Bearer ' + userIdToken
    }
  }).then(function (data) {
    $('#notes-container').empty();
    // Iterate over user data to display user's notes from database.
    data.forEach(function (note) {
      $('#notes-container').append($('<p>').text(note.message));
    });
  });
}

部署您的應用程式

您已成功將 Identity Platform 與 App Engine 應用程式整合。如要查看在實際正式環境中執行的應用程式,請按照下列步驟操作:

  1. 將 main.js 中的後端主機網址變更為 https://backend-dot-[PROJECT_ID].appspot.com。請將 [PROJECT_ID] 替換為專案 ID。
  2. 使用 Google Cloud SDK 指令列介面部署應用程式:

    gcloud app deploy backend/index.yaml frontend/app.yaml backend/app.yaml
    
  3. 前往 https://[PROJECT_ID].appspot.com 查看應用程式。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請刪除 App Engine 專案:

刪除專案

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

刪除專案的方法如下:

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

後續步驟