OAuth による Google アカウントのリンク(インプリシット フロー - アーカイブ済み)

サービスで OAuth 2.0 インプリシット フローをサポートするには、HTTPS 経由で認可エンドポイントにアクセスできるようにする必要があります。このエンドポイントは、ユーザーの認証を行い、ユーザーからデータアクセスへの同意を取得します。認可エンドポイントは、ログインしていないユーザーにログイン用の UI を表示し、リクエストされたアクセスへの同意を記録します。

Google アプリケーションからサービスの API を呼び出す必要がある場合、Google はこのエンドポイントを使用して、API の呼び出し許可をユーザーから取得します。

Google アカウントのリンク: OAuth 暗黙的フロー

次のシーケンス図は、ユーザー、Google、サービスの各エンドポイント間のインタラクションの詳細を示しています。

ユーザー Google アプリ / ブラウザ 認証エンドポイント 1. ユーザーがリンクを開始する 2. 認証エンドポイントにリダイレクト(GET) client_id、redirect_uri、state、scope 3. ログイン画面と同意画面を表示する 4. ユーザーが認証して同意を付与する 5. トークンを使用して Google にリダイレクト(GET) access_token、state 6. ユーザー トークンを保存する 7. ユーザー リソースにアクセスする
図 1. Google アカウントのリンク用の OAuth 2.0 暗黙的フローのイベント シーケンス。

役割と責任

次の表に、Google アカウントのリンク(GAL)OAuth 暗黙的フローにおけるアクターの役割と責任を示します。GAL では、Google は OAuth クライアントとして機能し、サービスはID/サービス プロバイダとして機能します。

アクター / コンポーネント GAL の役割 責任
Google アプリ / サーバー OAuth クライアント フローを開始し、ブラウザのリダイレクトを使用してアクセス トークンを受け取り、サービス API にアクセスするために安全に保存します。
認可エンドポイント 認可サーバー ユーザーを認証し、ユーザーの同意を取得して、有効期間の長いアクセス トークンを Google に直接発行します。
Google リダイレクト URI コールバック エンドポイント URL フラグメントに access_token 値と state 値を含む、承認サービスからのユーザー リダイレクトを受け取ります。

通常、Google が開始する OAuth 2.0 暗黙的フローのセッションは次のような流れになります。

  1. Google がユーザーのブラウザで認可エンドポイントを開きます。ユーザーがログインし(ログインしていない場合)、Google が API を使用してデータにアクセスすることを承諾します(まだ許可していない場合)。
  2. サービスがアクセス トークンを作成し、Google に返します。そのためには、アクセス トークンをリクエストに付加して、ユーザーのブラウザを Google にリダイレクトします。
  3. Google がサービスの API を呼び出し、リクエストごとにアクセス トークンを関連付けます。サービスは、アクセス トークンによって API へのアクセスが Google に許可されていることを確認し、API 呼び出しを完了します。

実装レシピ

暗黙的フローを実装する手順は次のとおりです。

ステップ 1: 認可リクエストを処理する

Google がアカウントのリンクを開始すると、ユーザーは認証エンドポイントにリダイレクトされます。プロトコル コントラクトとパラメータ要件の詳細については、認証エンドポイントをご覧ください。

リクエストを処理するには、次の操作を行います。

  1. リクエストを検証します

    • client_id が Google に割り当てられたクライアント ID と一致することを確認します。
    • redirect_uri が想定される Google リダイレクト URL と一致していることを確認します。 none https://oauth-redirect.googleusercontent.com/r/YOUR_PARTNER_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PARTNER_ID
    • response_typetoken であることを確認します。
  2. ユーザーを認証する:

    • ユーザーがサービスにログインしているかどうかを確認します。
    • ユーザーがログインしていない場合は、ログインまたは登録フローを完了するよう促します。
  3. アクセス トークンを生成する:

    • ユーザーとクライアントに関連付けられた、推測不可能な一意のアクセス トークンを作成します。
  4. Google にリダイレクトする:

    • ブラウザを redirect_uri で指定された URL にリダイレクトします。
    • 次のパラメータを URL フラグメント(ハッシュ)に追加します。
      • access_token: 生成したアクセス トークン。
      • token_type: bearer でなければなりません。
      • state: Google から受信した変更されていない状態値。
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.