處理重複登入

這是 Classroom 外掛程式系列的第三逐步操作說明。

在本逐步操作說明中,您會自動擷取使用者先前授予的憑證,以處理重複造訪的流程。接著,您需要將使用者導向可以立即發出 API 要求的網頁。這是 Classroom 外掛程式的必要行為。

在本逐步操作說明中,您將完成下列步驟:

  • 為使用者憑證實作永久儲存空間。
  • 擷取及評估 login_hint 外掛程式查詢參數。這是已登入使用者的專屬 Google ID 號碼。

完成後,您就可以對網頁應用程式中的使用者取得完整授權,並發出 Google API 呼叫。

瞭解 iframe 查詢參數

Classroom 會在開啟時載入外掛程式的附件設定 URI。Classroom 會在 URI 中附加數個 GET 查詢參數,當中包含實用的情境資訊。舉例來說,如果您的附件探索 URI 為 https://example.com/addon,Classroom 會在來源網址設為 https://example.com/addon?courseId=XXX&itemId=YYY&itemType=courseWork&addOnToken=ZZZ 時建立 iframe,其中 XXXYYYZZZ 是字串 ID。如需此情境的詳細說明,請參閱 iframe 指南

探索網址有五個可用的查詢參數:

  • courseId:目前 Classroom 課程的 ID。
  • itemId:使用者正在編輯或建立的串流項目 ID。
  • itemType:使用者正在建立或編輯的串流項目種類,可以是 courseWorkcourseWorkMaterialannouncement 其中之一。
  • addOnToken:用於授權特定 Classroom 外掛程式動作的權杖。
  • login_hint:目前使用者的 Google ID。

這份逐步操作說明會介紹 login_hint。系統會根據此查詢參數 (如有提供) 或至授權流程 (如有) 或外掛程式探索頁面 (如有) 轉送使用者,

存取查詢參數

查詢參數會透過 URI 字串傳遞至網頁應用程式。將這些值儲存在工作階段中;系統會在授權流程中使用這些值,也能儲存和擷取使用者的相關資訊。這些查詢參數只會在外掛程式首次開啟時傳送。

Python

前往 Flask 路徑的定義 (如果您使用的是我們提供的範例,請按 routes.py)。在外掛程式到達路徑頂端 (本範例中為 /classroom-addon) 擷取並儲存 login_hint 查詢參數:

# If the login_hint query parameter is available, we'll store it in the session.
if flask.request.args.get("login_hint"):
    flask.session["login_hint"] = flask.request.args.get("login_hint")

確認 login_hint (如有) 儲存在工作階段中。這是儲存這些值的適當位置,這些值為暫時性質,且當您開啟外掛程式時,您就能收到新值。

# It's possible that we might return to this route later, in which case the
# parameters will not be passed in. Instead, use the values cached in the
# session.
login_hint = flask.session.get("login_hint")

# If there's still no login_hint query parameter, this must be their first
# time signing in, so send the user to the sign in page.
if login_hint is None:
    return start_auth_flow()

Java

前往控制器類別中的外掛程式到達路徑 (提供範例的 AuthController.java 中的 /addon-discovery)。在這個路徑的開頭,擷取並儲存 login_hint 查詢參數。

/** Retrieve the login_hint or hd query parameters from the request URL. */
String login_hint = request.getParameter("login_hint");
String hd = request.getParameter("hd");

確認 login_hint (如有) 儲存在工作階段中。這是儲存這些值的適當位置,這些值為暫時性質,且當您開啟外掛程式時,您就能收到新值。

/** If neither query parameter is sent, use the values in the session. */
if (login_hint == null && hd == null) {
    login_hint = (String) session.getAttribute("login_hint");
    hd = (String) session.getAttribute("hd");
}

/** If the hd query parameter is provided, add hd to the session and route
*   the user to the authorization page. */
else if (hd != null) {
    session.setAttribute("hd", hd);
    return startAuthFlow(model);
}

/** If the login_hint query parameter is provided, add it to the session. */
else if (login_hint != null) {
    session.setAttribute("login_hint", login_hint);
}

在授權流程中新增查詢參數

login_hint 參數也應傳遞至 Google 的驗證伺服器。這樣有助於完成驗證程序;如果應用程式知道嘗試驗證的使用者,伺服器會在登入表單中預先填入電子郵件欄位,藉此簡化登入流程。

Python

前往 Flask 伺服器檔案中的授權路徑 (在我們提供的範例中,/authorize)。將 login_hint 引數新增至對 flow.authorization_url 的呼叫。

authorization_url, state = flow.authorization_url(
    # Enable offline access so that you can refresh an access token without
    # re-prompting the user for permission. Recommended for web server apps.
    access_type="offline",
    # Enable incremental authorization. Recommended as a best practice.
    include_granted_scopes="true",
    # The user will automatically be selected if we have the login_hint.
    login_hint=flask.session.get("login_hint"),

Java

前往 AuthService.java 類別中的 authorize() 方法。將 login_hinthd 新增為方法的參數,並將 login_hinthd 引數新增至授權網址產生器。

String authUrl = flow
    .newAuthorizationUrl()
    .setState(state)
    .set("login_hint", login_hint)
    .set("hd", hd)
    .setRedirectUri(REDIRECT_URI)
    .build();

為使用者憑證新增永久儲存空間

如果您在外掛程式載入時收到 login_hint 做為查詢參數,即代表使用者已完成應用程式的授權流程。您應擷取使用者先前的憑證,而不是強制要求他們再次登入。

提醒您,在授權流程完成後,您已收到「更新權杖」。請儲存此憑證,以便重複使用來取得使用 Google API 的短期「存取權杖」。您之前在工作階段中儲存了這些憑證,但需要儲存憑證,才能處理重複的造訪記錄。

定義使用者結構定義並設定資料庫

設定 User 的資料庫結構定義。

Python

定義使用者架構

User 包含下列屬性:

  • id:使用者的 Google ID。這個值應與 login_hint 查詢參數提供的值相符。
  • display_name:使用者的姓名,例如「Alex Smith」。
  • email:使用者的電子郵件地址。
  • portrait_url:使用者個人資料相片的網址。
  • refresh_token:先前取得的更新權杖。

此範例使用 SQLite 實作儲存空間,而 SQLite 是 Python 的原生支援。它會使用 flask_sqlalchemy 模組來協助管理資料庫。

設定資料庫

首先,指定資料庫的檔案位置。前往您的伺服器設定檔 (我們提供的範例 config.py),並新增以下內容。

import os

# Point to a database file in the project root.
DATABASE_FILE_NAME = os.path.join(
    os.path.abspath(os.path.dirname(__file__)), 'data.sqlite')

class Config(object):
    SQLALCHEMY_DATABASE_URI = f"sqlite:///{DATABASE_FILE_NAME}"
    SQLALCHEMY_TRACK_MODIFICATIONS = False

這會將 Flask 指向 main.py 檔案所在目錄中的 data.sqlite 檔案。

接著,前往模組目錄,建立新的 models.py 檔案。如果您按照我們提供的範例,這就是 webapp/models.py。將以下內容新增至新檔案,以定義 User 資料表。如果不同,請將 webapp 替換成您的模組名稱。

from webapp import db

# Database model to represent a user.
class User(db.Model):
    # The user's identifying information:
    id = db.Column(db.String(120), primary_key=True)
    display_name = db.Column(db.String(80))
    email = db.Column(db.String(120), unique=True)
    portrait_url = db.Column(db.Text())

    # The user's refresh token, which will be used to obtain an access token.
    # Note that refresh tokens will become invalid if:
    # - The refresh token has not been used for six months.
    # - The user revokes your app's access permissions.
    # - The user changes passwords.
    # - The user belongs to a Google Cloud organization
    #   that has session control policies in effect.
    refresh_token = db.Column(db.Text())

最後,在模組的 __init__.py 檔案中新增以下內容,以匯入新模型並建立資料庫。

from webapp import models
from os import path
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(app)

# Initialize the database file if not created.
if not path.exists(config.DATABASE_FILE_NAME):
    db.create_all()

Java

定義使用者架構

User 包含下列屬性:

  • id:使用者的 Google ID。這個值應與 login_hint 查詢參數中提供的值相符。
  • email:使用者的電子郵件地址。

在模組的 resources 目錄中建立 schema.sql 檔案。Spring 讀取這個檔案,並據此為資料庫產生結構定義。定義資料表時,請使用資料表名稱、users,以及代表 User 屬性 (idemail) 的資料欄。

CREATE TABLE IF NOT EXISTS users (
    id VARCHAR(255) PRIMARY KEY, -- user's unique Google ID
    email VARCHAR(255), -- user's email address
);

建立 Java 類別,定義資料庫的 User 模型。這是提供的範例中的 User.java

新增 @Entity 註解,表示這是可儲存至資料庫的 POJO。新增 @Table 註解,其中包含您在 schema.sql 中設定的對應資料表名稱。

請注意,程式碼範例包含這兩個屬性的建構函式和 setter。AuthController.java 中的建構函式和 setter 可用來在資料庫中建立或更新使用者。視需要加入 getter 和 toString 方法,但為求簡潔,請不要在本特定逐步操作說明中採用這些方法,且會省略本頁面的程式碼範例。

/** An entity class that provides a model to store user information. */
@Entity
@Table(name = "users")
public class User {
    /** The user's unique Google ID. The @Id annotation specifies that this
     *   is the primary key. */
    @Id
    @Column
    private String id;

    /** The user's email address. */
    @Column
    private String email;

    /** Required User class no args constructor. */
    public User() {
    }

    /** The User class constructor that creates a User object with the
    *   specified parameters.
    *   @param id the user's unique Google ID
    *   @param email the user's email address
    */
    public User(String id, String email) {
        this.id = id;
        this.email = email;
    }

    public void setId(String id) { this.id = id; }

    public void setEmail(String email) { this.email = email; }
}

建立名為 UserRepository.java 的介面,處理對資料庫的 CRUD 作業。這個介面會擴充 CrudRepository 介面。

/** Provides CRUD operations for the User class by extending the
 *   CrudRepository interface. */
@Repository
public interface UserRepository extends CrudRepository<User, String> {
}

控制器類別可協助用戶端與存放區之間的通訊。因此,請更新控制器類別建構函式,插入 UserRepository 類別。

/** Declare UserRepository to be used in the Controller class constructor. */
private final UserRepository userRepository;

/**
*   ...
*   @param userRepository the class that interacts with User objects stored in
*   persistent storage.
*/
public AuthController(AuthService authService, UserRepository userRepository) {
    this.authService = authService;
    this.userRepository = userRepository;
}

設定資料庫

如要儲存使用者相關資訊,請使用 Spring Boot 確實支援的 H2 資料庫。這個資料庫也適用於儲存其他 Classroom 相關資訊。如要設定 H2 資料庫,必須將下列設定新增至 application.properties

# Enable configuration for persistent storage using an H2 database
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:./h2/userdb
spring.datasource.username=<USERNAME>
spring.datasource.password=<PASSWORD>
spring.jpa.hibernate.ddl-auto=update
spring.jpa.open-in-view=false

spring.datasource.url 設定會建立名為 h2 的目錄,內含 userdb 檔案。將 H2 資料庫的路徑新增至 .gitignore。您必須先更新 spring.datasource.usernamespring.datasource.password,再執行應用程式,以自行選擇的使用者名稱和密碼設定資料庫。如要在執行應用程式後更新資料庫的使用者名稱和密碼,請刪除產生的 h2 目錄、更新設定,然後重新執行應用程式。

spring.jpa.hibernate.ddl-auto 設定設為 update,可確保應用程式在重新啟動時保留資料庫中儲存的資料。如要在每次重新啟動應用程式時清除資料庫,請將此設定設為 create

spring.jpa.open-in-view 設定設為 false。這項設定預設為啟用,因為此設定可能會產生難以在實際工作環境中診斷的效能問題。

如前所述,您必須能夠擷取重複使用者的憑證。這由 GoogleAuthorizationCodeFlow 提供的內建憑證存放區支援推動。

AuthService.java 類別中,定義儲存憑證類別的檔案路徑。在此範例中,檔案是在 /credentialStore 目錄中建立。將憑證儲存庫的路徑新增至 .gitignore。在使用者啟動授權流程後,系統就會產生這個目錄。

private static final File dataDirectory = new File("credentialStore");

接下來,請在 AuthService.java 檔案中建立方法,建立並傳回 FileDataStoreFactory 物件。這是儲存憑證的資料儲存庫。

/** Creates and returns FileDataStoreFactory object to store credentials.
 *   @return FileDataStoreFactory dataStore used to save and obtain users ids
 *   mapped to Credentials.
 *   @throws IOException if creating the dataStore is unsuccessful.
 */
public FileDataStoreFactory getCredentialDataStore() throws IOException {
    FileDataStoreFactory dataStore = new FileDataStoreFactory(dataDirectory);
    return dataStore;
}

更新 AuthService.java 中的 getFlow() 方法,將 setDataStoreFactory 納入 GoogleAuthorizationCodeFlow Builder() 方法,並呼叫 getCredentialDataStore() 來設定資料儲存庫。

GoogleAuthorizationCodeFlow authorizationCodeFlow =
    new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT,
        JSON_FACTORY,
        getClientSecrets(),
        getScopes())
    .setAccessType("offline")
    .setDataStoreFactory(getCredentialDataStore())
    .build();

接下來,請更新 getAndSaveCredentials(String authorizationCode) 方法。這個方法先前是在沒有位置儲存憑證的情況下取得憑證。更新方法,將憑證儲存在由使用者 ID 建立索引的資料儲存庫中。

您可利用 id_tokenTokenResponse 物件取得使用者 ID,但「必須」先驗證此使用者 ID。否則,用戶端應用程式只要將修改的使用者 ID 傳送至伺服器,就能冒用使用者的身分。建議您使用 Google API 用戶端程式庫驗證 id_token。詳情請參閱 [Google Identity 權杖驗證頁面]。

// Obtaining the id_token will help determine which user signed in to the application.
String idTokenString = tokenResponse.get("id_token").toString();

// Validate the id_token using the GoogleIdTokenVerifier object.
GoogleIdTokenVerifier googleIdTokenVerifier = new GoogleIdTokenVerifier.Builder(
        HTTP_TRANSPORT,
        JSON_FACTORY)
    .setAudience(Collections.singletonList(
        googleClientSecrets.getWeb().getClientId()))
    .build();

GoogleIdToken idToken = googleIdTokenVerifier.verify(idTokenString);

if (idToken == null) {
    throw new Exception("Invalid ID token.");
}

驗證 id_token 後,請取得 userId 來儲存以及取得的憑證。

// Obtain the user id from the id_token.
Payload payload = idToken.getPayload();
String userId = payload.getSubject();

將呼叫更新為 flow.createAndStoreCredential,加入 userId

// Save the user id and credentials to the configured FileDataStoreFactory.
Credential credential = flow.createAndStoreCredential(tokenResponse, userId);

AuthService.java 類別中新增方法,如果資料儲存庫中有特定使用者,就會傳回該使用者的憑證。

/** Find credentials in the datastore based on a specific user id.
*   @param userId key to find in the file datastore.
*   @return Credential object to be returned if a matching key is found in the datastore. Null if
*   the key doesn't exist.
*   @throws Exception if building flow object or checking for userId key is unsuccessful. */
public Credential loadFromCredentialDataStore(String userId) throws Exception {
    try {
        GoogleAuthorizationCodeFlow flow = getFlow();
        Credential credential = flow.loadCredential(userId);
        return credential;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

擷取憑證

定義擷取 Users 的方法。您會在 login_hint 查詢參數中提供 id,用於擷取特定使用者記錄。

Python

def get_credentials_from_storage(id):
    """
    Retrieves credentials from the storage and returns them as a dictionary.
    """
    return User.query.get(id)

Java

AuthController.java 類別中,定義一項方法,根據使用者 ID 從資料庫擷取使用者。

/** Retrieves stored credentials based on the user id.
*   @param id the id of the current user
*   @return User the database entry corresponding to the current user or null
*   if the user doesn't exist in the database.
*/
public User getUser(String id) {
    if (id != null) {
        Optional<User> user = userRepository.findById(id);
        if (user.isPresent()) {
            return user.get();
        }
    }
    return null;
}

儲存憑證

儲存憑證時有兩種情況。如果使用者的 id 已存在於資料庫中,請以任何新值更新現有記錄。否則,請建立新的 User 記錄,並將其新增至資料庫。

Python

首先,請定義實作儲存空間或更新行為的公用程式方法。

def save_user_credentials(credentials=None, user_info=None):
    """
    Updates or adds a User to the database. A new user is added only if both
    credentials and user_info are provided.

    Args:
        credentials: An optional Credentials object.
        user_info: An optional dict containing user info returned by the
            OAuth 2.0 API.
    """

    existing_user = get_credentials_from_storage(
        flask.session.get("login_hint"))

    if existing_user:
        if user_info:
            existing_user.id = user_info.get("id")
            existing_user.display_name = user_info.get("name")
            existing_user.email = user_info.get("email")
            existing_user.portrait_url = user_info.get("picture")

        if credentials and credentials.refresh_token is not None:
            existing_user.refresh_token = credentials.refresh_token

    elif credentials and user_info:
        new_user = User(id=user_info.get("id"),
                        display_name=user_info.get("name"),
                        email=user_info.get("email"),
                        portrait_url=user_info.get("picture"),
                        refresh_token=credentials.refresh_token)

        db.session.add(new_user)

    db.session.commit()

有兩種執行個體可以將憑證儲存至資料庫:使用者在授權流程結束時返回應用程式時,以及發出 API 呼叫時。這些是我們先前設定工作階段 credentials 鍵的位置。

callback 路線的結尾呼叫 save_user_credentials。保留 user_info 物件,不要只擷取使用者的名稱。

# The flow is complete! We'll use the credentials to fetch the user's info.
user_info_service = googleapiclient.discovery.build(
    serviceName="oauth2", version="v2", credentials=credentials)

user_info = user_info_service.userinfo().get().execute()

flask.session["username"] = user_info.get("name")

save_user_credentials(credentials, user_info)

您也應在呼叫 API 後更新憑證。在這種情況下,您可以在 save_user_credentials 方法中,提供更新後的憑證做為引數。

# Save credentials in case access token was refreshed.
flask.session["credentials"] = credentials_to_dict(credentials)
save_user_credentials(credentials)

Java

首先,請定義在 H2 資料庫中儲存或更新 User 物件的方法。

/** Adds or updates a user in the database.
*   @param credential the credentials object to save or update in the database.
*   @param userinfo the userinfo object to save or update in the database.
*   @param session the current session.
*/
public void saveUser(Credential credential, Userinfo userinfo, HttpSession session) {
    User storedUser = null;
    if (session != null && session.getAttribute("login_hint") != null) {
        storedUser = getUser(session.getAttribute("login_hint").toString());
    }

    if (storedUser != null) {
        if (userinfo != null) {
            storedUser.setId(userinfo.getId());
            storedUser.setEmail(userinfo.getEmail());
        }
        userRepository.save(storedUser);
    } else if (credential != null && userinfo != null) {
        User newUser = new User(
            userinfo.getId(),
            userinfo.getEmail(),
        );
        userRepository.save(newUser);
    }
}

有兩種執行個體可以將憑證儲存至資料庫:使用者在授權流程結束時返回應用程式時,以及發出 API 呼叫時。這些是我們先前設定工作階段 credentials 鍵的位置。

/callback 路線的結尾呼叫 saveUser。建議您保留 user_info 物件,而不只是擷取使用者的電子郵件。

/** This is the end of the auth flow. We should save user info to the database. */
Userinfo userinfo = authService.getUserInfo(credentials);
saveUser(credentials, userinfo, session);

您也應在呼叫 API 後更新憑證。在這種情況下,您可以在 saveUser 方法中,提供更新後的憑證做為引數。

/** Save credentials in case access token was refreshed. */
saveUser(credentials, null, session);

憑證過期

請注意,有幾項原因會導致重新整理權杖失效。其中包括:

  • 更新權杖已閒置六個月。
  • 使用者會撤銷應用程式的存取權限。
  • 使用者變更密碼。
  • 使用者所屬的 Google Cloud 機構已生效工作階段控制政策。

如果使用者的憑證失效,請再次透過授權流程獲取新權杖。

自動轉送使用者

修改外掛程式到達路徑,偵測使用者是否先前已授權我們的應用程式。如果可以,請將使用者導向我們的主要外掛程式頁面。否則,請提示使用者登入。

Python

確認已在應用程式啟動時建立資料庫檔案。將以下內容插入模組初始化器 (例如我們提供的範例中的 webapp/__init__.py) 或啟動伺服器的主要方法中。

# Initialize the database file if not created.
if not os.path.exists(DATABASE_FILE_NAME):
    db.create_all()

接著,您的方法應按照上述說明處理 login_hint 查詢參數。接著如果是重複訪客,請載入商店憑證。如果您收到 login_hint,就知道是舊訪客。 擷取這位使用者儲存的任何憑證,並載入至工作階段。

stored_credentials = get_credentials_from_storage(login_hint)

# If we have stored credentials, store them in the session.
if stored_credentials:
    # Load the client secrets file contents.
    client_secrets_dict = json.load(
        open(CLIENT_SECRETS_FILE)).get("web")

    # Update the credentials in the session.
    if not flask.session.get("credentials"):
        flask.session["credentials"] = {}

    flask.session["credentials"] = {
        "token": stored_credentials.access_token,
        "refresh_token": stored_credentials.refresh_token,
        "token_uri": client_secrets_dict["token_uri"],
        "client_id": client_secrets_dict["client_id"],
        "client_secret": client_secrets_dict["client_secret"],
        "scopes": SCOPES
    }

    # Set the username in the session.
    flask.session["username"] = stored_credentials.display_name

最後,如果沒有使用者憑證,請將使用者轉送至登入頁面。如果問題都已解決,請將使用者導向主要外掛程式頁面。

if "credentials" not in flask.session or \
    flask.session["credentials"]["refresh_token"] is None:
    return flask.render_template("authorization.html")

return flask.render_template(
    "addon-discovery.html",
    message="You've reached the addon discovery page.")

Java

前往外掛程式到達路徑 (提供的範例為 /addon-discovery)。如上方所述,這是您處理 login_hint 查詢參數的位置。

首先,請檢查工作階段中是否存在憑證。如果沒有,請呼叫 startAuthFlow 方法,透過驗證流程轉送使用者。

/** Check if the credentials exist in the session. The session could have
 *   been cleared when the user clicked the Sign-Out button, and the expected
 *   behavior after sign-out would be to display the sign-in page when the
 *   iframe is opened again. */
if (session.getAttribute("credentials") == null) {
    return startAuthFlow(model);
}

然後如果這是回訪者,請從 H2 資料庫載入使用者。如果您收到 login_hint 查詢參數,即為重複訪客。如果使用者存在於 H2 資料庫中,請從先前設定的憑證資料儲存庫載入憑證,然後在工作階段中設定憑證。如果憑證並非從憑證資料儲存庫取得,請呼叫 startAuthFlow,透過驗證流程轉送使用者。

/** At this point, we know that credentials exist in the session, but we
 *   should update the session credentials with the credentials in persistent
 *   storage in case they were refreshed. If the credentials in persistent
 *   storage are null, we should navigate the user to the authorization flow
 *   to obtain persisted credentials. */

User storedUser = getUser(login_hint);

if (storedUser != null) {
    Credential credential = authService.loadFromCredentialDataStore(login_hint);
    if (credential != null) {
        session.setAttribute("credentials", credential);
    } else {
        return startAuthFlow(model);
    }
}

最後,請將使用者轉送至外掛程式到達網頁。

/** Finally, if there are credentials in the session and in persistent
 *   storage, direct the user to the addon-discovery page. */
return "addon-discovery";

測試外掛程式

老師測試者的身分登入 Google Classroom。前往「課堂作業」分頁,然後建立新的「作業」。按一下文字區域下方的「Add-ons」按鈕,然後選取外掛程式。iframe 會開啟,外掛程式則會載入您在 Google Workspace Marketplace SDK「App Configuration」頁面中指定的連結設定 URI

恭喜!您可以繼續進行下一個步驟:建立連結及識別使用者的角色