Liên kết Tài khoản Google với OAuth

Các tài khoản được liên kết bằng cách sử dụng quy trình ngầm ẩnmã uỷ quyền OAuth 2.0 tiêu chuẩn ngành. Dịch vụ của bạn phải hỗ trợ điểm cuối ủy quyền và tuân thủ giao thức trao đổi mã thông báo dựa trên OAuth 2.0.

Trong luồng ngầm ẩn, Google sẽ mở điểm cuối ủy quyền của bạn trong trình duyệt của người dùng. Sau khi đăng nhập thành công, bạn sẽ trả về một mã truy cập dài hạn cho Google. Mã truy cập này hiện nằm trong mọi yêu cầu do Google gửi.

Trong quy trình mã ủy quyền, bạn cần có hai điểm cuối:

  • Điểm cuối ủy quyền, giới thiệu giao diện người dùng đăng nhập cho người dùng chưa đăng nhập. Điểm cuối ủy quyền cũng tạo một mã ủy quyền ngắn hạn để ghi lại người dùng\39; đồng ý với quyền truy cập được yêu cầu.

  • Điểm cuối mã trao đổi, chịu trách nhiệm đối với hai loại sàn giao dịch:

    1. Trao đổi mã ủy quyền cho mã làm mới lâu dài và mã truy cập ngắn hạn. Việc trao đổi này xảy ra khi người dùng trải qua luồng liên kết tài khoản.
    2. Trao đổi một mã làm mới lâu dài để lấy mã truy cập ngắn hạn. Việc trao đổi này xảy ra khi Google cần một mã truy cập mới vì mã đó đã hết hạn.

Chọn một quy trình OAuth 2.0

Mặc dù quy trình ngầm ẩn đơn giản hơn để triển khai, nhưng Google khuyến nghị rằng mã thông báo truy cập do luồng ngầm ẩn cấp sẽ không bao giờ hết hạn. Điều này là do người dùng bắt buộc phải liên kết lại tài khoản của họ sau khi mã thông báo hết hạn với luồng ngầm ẩn. Nếu cần hết hạn mã thông báo vì lý do bảo mật, bạn nên sử dụng quy trình mã ủy quyền.

Hướng dẫn thiết kế

Phần này mô tả các yêu cầu về thiết kế và đề xuất cho màn hình người dùng mà bạn lưu trữ cho luồng liên kết OAuth. Sau khi ứng dụng Google gọi điện, nền tảng của bạn sẽ hiển thị màn hình đăng nhập vào trang Google và màn hình đồng ý liên kết tài khoản với người dùng. Người dùng được chuyển hướng lại ứng dụng của Google sau khi đồng ý liên kết tài khoản.

Hình này cho thấy các bước để người dùng liên kết Tài khoản Google của họ với hệ thống xác thực của bạn. Ảnh chụp màn hình đầu tiên cho thấy
            quá trình liên kết do người dùng tạo từ nền tảng của bạn. Hình ảnh thứ hai cho thấy
            thông tin đăng nhập của người dùng cho Google, còn hình ảnh thứ ba cho thấy sự đồng ý của người dùng và
            thông báo xác nhận đã liên kết Tài khoản Google của họ với ứng dụng của bạn. Ảnh chụp màn hình cuối cùng
            hiển thị một tài khoản người dùng đã liên kết thành công trong
            ứng dụng Google.
Hình 1. Tài khoản liên kết người dùng đăng nhập vào Google và các màn hình đồng ý.

Yêu cầu

  1. Bạn phải thông báo rằng tài khoản của người dùng sẽ được liên kết với Google, không phải một sản phẩm cụ thể của Google như Google Home hoặc Trợ lý Google.

Đề xuất

Bạn nên làm như sau:

  1. Hiển thị Chính sách quyền riêng tư của Google. Thêm một đường liên kết đến Chính sách quyền riêng tư của Google trên màn hình đồng ý.

  2. Dữ liệu cần chia sẻ. Sử dụng ngôn ngữ rõ ràng và súc tích để cho người dùng biết dữ liệu của họ mà Google yêu cầu và lý do.

  3. Lời kêu gọi hành động rõ ràng. Nêu rõ lời kêu gọi hành động rõ ràng trên màn hình đồng ý của bạn, chẳng hạn như "Đồng ý và liên kết". Điều này là do người dùng cần phải hiểu được dữ liệu mà họ cần chia sẻ với Google để liên kết tài khoản.

  4. Khả năng hủy. Cung cấp cho người dùng một cách để quay lại hoặc hủy, nếu họ chọn không liên kết.

  5. Xóa quy trình đăng nhập. Đảm bảo rằng người dùng có phương thức rõ ràng để đăng nhập vào Tài khoản Google của họ, chẳng hạn như các trường cho tên người dùng và mật khẩu hoặc Đăng nhập bằng Google.

  6. Khả năng hủy liên kết. Cung cấp một cơ chế để người dùng huỷ liên kết, chẳng hạn như URL đến chế độ cài đặt tài khoản của họ trên nền tảng của bạn. Ngoài ra, bạn có thể bao gồm đường liên kết đến Tài khoản Google để người dùng có thể quản lý tài khoản được liên kết.

  7. Khả năng thay đổi tài khoản người dùng. Đề xuất một phương thức để người dùng chuyển(các) tài khoản của họ. Điều này đặc biệt có lợi nếu người dùng có xu hướng có nhiều tài khoản.

    • Nếu người dùng phải đóng màn hình đồng ý để chuyển đổi tài khoản, hãy gửi lỗi có thể khôi phục cho Google để người dùng có thể đăng nhập vào tài khoản mong muốn bằng quy trình liên kết OAuth và quy trình ngầm ẩn.
  8. Bao gồm biểu trưng của bạn. Hiển thị biểu tượng công ty trên màn hình xin phép. Sử dụng nguyên tắc chọn kiểu để đặt biểu tượng của bạn. Nếu bạn muốn hiển thị biểu trưng của Google, hãy xem Biểu trưng và nhãn hiệu.

Create the project

To create your project to use account linking:

  1. Go to the Google API Console.
  2. Nhấp vào Tạo dự án .
  3. Nhập tên hoặc chấp nhận đề xuất được tạo.
  4. Xác nhận hoặc chỉnh sửa bất kỳ trường nào còn lại.
  5. Nhấp vào Tạo .

Để xem ID dự án của bạn:

  1. Go to the Google API Console.
  2. Tìm dự án của bạn trong bảng trên trang đích. ID dự án xuất hiện trong cột ID .

The Google Account Linking process includes a consent screen which tells users the application requesting access to their data, what kind of data they are asking for and the terms that apply. You will need to configure your OAuth consent screen before generating a Google API client ID.

  1. Open the OAuth consent screen page of the Google APIs console.
  2. If prompted, select the project you just created.
  3. On the "OAuth consent screen" page, fill out the form and click the “Save” button.

    Application name: The name of the application asking for consent. The name should accurately reflect your application and be consistent with the application name users see elsewhere. The application name will be shown on the Account Linking consent screen.

    Application logo: An image on the consent screen that will help users recognize your app. The logo is shown on Account linking consent screen and on account settings

    Support email: For users to contact you with questions about their consent.

    Scopes for Google APIs: Scopes allow your application to access your user's private Google data. For the Google Account Linking use case, default scope (email, profile, openid) is sufficient, you don’t need to add any sensitive scopes. It is generally a best practice to request scopes incrementally, at the time access is required, rather than up front. Learn more.

    Authorized domains: To protect you and your users, Google only allows applications that authenticate using OAuth to use Authorized Domains. Your applications' links must be hosted on Authorized Domains. Learn more.

    Application Homepage link: Home page for your application. Must be hosted on an Authorized Domain.

    Application Privacy Policy link: Shown on Google Account Linking consent screen. Must be hosted on an Authorized Domain.

    Application Terms of Service link (Optional): Must be hosted on an Authorized Domain.

    Figure 1. Google Account Linking Consent Screen for a fictitious Application, Tunery

  4. Check "Verification Status", if your application needs verification then click the "Submit For Verification" button to submit your application for verification. Refer to OAuth verification requirements for details.

Triển khai máy chủ OAuth

To support the OAuth 2.0 implicit flow, your service makes an authorization endpoint available by HTTPS. This endpoint is responsible for authentication and obtaining consent from users for data access. The authorization endpoint presents a sign-in UI to your users that aren't already signed in and records consent to the requested access.

When a Google application needs to call one of your service's authorized APIs, Google uses this endpoint to get permission from your users to call these APIs on their behalf.

A typical OAuth 2.0 implicit flow session initiated by Google has the following flow:

  1. Google opens your authorization endpoint in the user's browser. The user signs in, if not signed in already, and grants Google permission to access their data with your API, if they haven't already granted permission.
  2. Your service creates an access token and returns it to Google. To do so, redirect the user's browser back to Google with the access token attached to the request.
  3. Google calls your service's APIs and attaches the access token with each request. Your service verifies that the access token grants Google authorization to access the API and then completes the API call.

Handle authorization requests

When a Google application needs to perform account linking via an OAuth 2.0 implicit flow, Google sends the user to your authorization endpoint with a request that includes the following parameters:

Authorization endpoint parameters
client_id The client ID you assigned to Google.
redirect_uri The URL to which you send the response to this request.
state A bookkeeping value that is passed back to Google unchanged in the redirect URI.
response_type The type of value to return in the response. For the OAuth 2.0 implicit flow, the response type is always token.
user_locale The Google Account language setting in RFC5646 format used to localize your content in the user's preferred language.

For example, if your authorization endpoint is available at https://myservice.example.com/auth, a request might look like the following:

GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&response_type=token&user_locale=LOCALE

For your authorization endpoint to handle sign-in requests, do the following steps:

  1. Verify the client_id and redirect_uri values to prevent granting access to unintended or misconfigured client apps:

    • Confirm that the client_id matches the client ID you assigned to Google.
    • Confirm that the URL specified by the redirect_uri parameter has the following form:
      https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
      https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
      
  2. Check if the user is signed in to your service. If the user isn't signed in, complete your service's sign-in or sign-up flow.

  3. Generate an access token for Google to use to access your API. The access token can be any string value, but it must uniquely represent the user and the client the token is for and must not be guessable.

  4. Send an HTTP response that redirects the user's browser to the URL specified by the redirect_uri parameter. Include all of the following parameters in the URL fragment:

    • access_token: The access token you just generated
    • token_type: The string bearer
    • state: The unmodified state value from the original request

    The following is an example of the resulting URL:

    https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID#access_token=ACCESS_TOKEN&token_type=bearer&state=STATE_STRING

Google's OAuth 2.0 redirect handler receives the access token and confirms that the state value hasn't changed. After Google has obtained an access token for your service, Google attaches the token to subsequent calls to your service APIs.

Handle userinfo requests

The userinfo endpoint is an OAuth 2.0 protected resource that return claims about the linked user. Implementing and hosting the userinfo endpoint is optional, except for the following use cases:

After the access token has been successfully retrieved from your token endpoint, Google sends a request to your userinfo endpoint to retrieve basic profile information about the linked user.

userinfo endpoint request headers
Authorization header The access token of type Bearer.

For example, if your userinfo endpoint is available at https://myservice.example.com/userinfo, a request might look like the following:

GET /userinfo HTTP/1.1
Host: myservice.example.com
Authorization: Bearer ACCESS_TOKEN

For your userinfo endpoint to handle requests, do the following steps:

  1. Extract access token from the Authorization header and return information for the user associated with the access token.
  2. If the access token is invalid, return an HTTP 401 Unauthorized error with using the WWW-Authenticate Response Header. Below is an example of a userinfo error response:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    If a 401 Unauthorized, or any other unsuccessful error response is returned during the linking process, the error will be non-recoverable, the retrieved token will be discarded and the user will have to initiate the linking process again.
  3. If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    
    If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.

    userinfo endpoint response
    sub A unique ID that identifies the user in your system.
    email Email address of the user.
    given_name Optional: First name of the user.
    family_name Optional: Last name of the user.
    name Optional: Full name of the user.
    picture Optional: Profile picture of the user.

Xác thực quá trình triển khai

Bạn có thể xác nhận thực hiện của bạn bằng cách sử dụng các sân chơi OAuth 2.0 công cụ.

Trong công cụ, hãy thực hiện các bước sau:

  1. Nhấp vào Cấu hình để mở cửa sổ OAuth 2.0 Configuration.
  2. Trong lĩnh vực dòng chảy OAuth, chọn Client-side.
  3. Trong lĩnh vực OAuth thiết bị đầu cuối, chọn Custom.
  4. Chỉ định điểm cuối OAuth 2.0 của bạn và ID khách hàng mà bạn đã chỉ định cho Google trong các trường tương ứng.
  5. Trong phần Bước 1, không chọn bất kỳ phạm vi của Google. Thay vào đó, hãy để trống trường này hoặc nhập phạm vi hợp lệ cho máy chủ của bạn (hoặc một chuỗi tùy ý nếu bạn không sử dụng phạm vi OAuth). Khi bạn đã hoàn tất, nhấn Authorize API.
  6. Trong các phần Bước 2Bước 3, đi qua các dòng chảy OAuth 2.0 và xác minh rằng mỗi bước hoạt động như dự kiến.

Bạn có thể xác nhận thực hiện của bạn bằng cách sử dụng các tài khoản Google Liên kết Demo công cụ.

Trong công cụ, hãy thực hiện các bước sau:

  1. Nhấp vào Sign-in với nút Google.
  2. Chọn tài khoản bạn muốn liên kết.
  3. Nhập ID dịch vụ.
  4. Tùy ý nhập một hoặc nhiều phạm vi mà bạn sẽ yêu cầu quyền truy cập.
  5. Nhấp vào Bắt đầu Demo.
  6. Khi được nhắc, hãy xác nhận rằng bạn có thể đồng ý và từ chối yêu cầu liên kết.
  7. Xác nhận rằng bạn được chuyển hướng đến nền tảng của mình.