概要
OAuth ベースの Google でログインの簡素化されたリンクは、OAuth リンクの上層に Google でログインを追加します。これにより、Google ユーザーにシームレスなリンク エクスペリエンスを提供し、アカウントの作成も可能になります。ユーザーは Google アカウントを使用してサービスに新しいアカウントを作成できます。
OAuth と Google でログインを使用してアカウント リンクを行うには、次の一般的な手順に従います。
- まず、ユーザーの Google プロフィールにアクセスすることについてユーザーに同意を求めます。
- プロフィールの情報を使用して、ユーザー アカウントが存在するかどうかを確認します。
- 既存のユーザーの場合は、アカウントをリンクします。
- 認証システムで Google ユーザーに一致するユーザーが見つからない場合は、Google から受け取った ID トークンを検証します。その後、ID トークンに含まれているプロフィール情報に基づいてユーザー アカウントを作成できます。
図 1. ユーザーのスマートフォンでの簡素化されたリンクによるアカウント リンク
簡素化されたリンク: OAuth + 「Google でログイン」フロー
次のシーケンス図は、Streamlined Linking におけるユーザー、Google、トークン交換エンドポイント間のインタラクションの詳細を示しています。
役割と責任
次の表に、Streamlined Linking フローにおけるアクターの役割と責任を定義します。
| アクター / コンポーネント | GAL ロール | 責任 |
|---|---|---|
| Google アプリ / サーバー | OAuth クライアント | Google でログインのユーザーの同意を取得し、ID アサーション(JWT)をサーバーに渡し、結果のトークンを安全に保存します。 |
| トークン交換エンドポイント | ID プロバイダ / 認可サーバー | ID アサーションを検証し、既存のアカウントを確認し、アカウント リンク インテント(check、get、create)を処理し、リクエストされたインテントに基づいてトークンを発行します。 |
| サービス API | リソース サーバー | 有効なアクセス トークンが提示された場合にユーザーデータへのアクセスを提供します。 |
簡素化されたリンクの要件
- 基本的な OAuth リンクフローを実装します。サービスは、OAuth 2.0 準拠の認証エンドポイントとトークン交換エンドポイントをサポートする必要があります。
- トークン交換エンドポイントは、JSON Web Token(JWT)アサーションをサポートし、
check、create、getインテントを実装する必要があります。
簡素化されたリンクの決定ロジック
次のロジックは、簡略化されたリンクフローでインテントが呼び出される方法を決定します。
- ユーザーが認証システムにアカウントを持っているかどうか。(ユーザーが [はい] または [いいえ] を選択して決定します)
- はい : ユーザーは Google アカウントに関連付けられているメールアドレスを使用してプラットフォームにログインしますか?(ユーザーが [はい] または [いいえ] を選択して決定します)
- YES : 認証システムに一致するアカウントがユーザーにありますか?(確認のために
check intentが呼び出されます)- YES :
get intentが呼び出され、インテントの取得が成功した場合、アカウントがリンクされます。 - いいえ : 新しいアカウントを作成しますか?(ユーザーが [はい] または [いいえ] を選択して決定します)
- YES :
create intentが呼び出され、作成インテントが正常に返された場合、アカウントがリンクされます。 - いいえ : OAuth リンクフローがトリガーされ、ユーザーはブラウザにリダイレクトされます。ユーザーは別のメールアドレスでリンクするオプションを選択できます。
- YES :
- YES :
- いいえ : OAuth リンクフローがトリガーされ、ユーザーはブラウザにリダイレクトされ、別のメールアドレスでリンクするオプションが表示されます。
- YES : 認証システムに一致するアカウントがユーザーにありますか?(確認のために
- NO : 認証システムに一致するアカウントがユーザーにありますか?(確認のために
check intentが呼び出されます)- YES :
get intentが呼び出され、インテントの取得が成功した場合、アカウントがリンクされます。 - NO : 作成インテントが正常に返された場合、
create intentが呼び出され、アカウントがリンクされます。
- YES :
- はい : ユーザーは Google アカウントに関連付けられているメールアドレスを使用してプラットフォームにログインしますか?(ユーザーが [はい] または [いいえ] を選択して決定します)
実装レシピ
トークン交換エンドポイントでは、Streamlined Linking をサポートするために check、get、create インテントを実装する必要があります。
さまざまなインテントを処理する手順は次のとおりです。
Check for an existing user account (check intent)
Google calls your token exchange endpoint to verify if the Google user exists in your system. For parameter details, see Streamlined Linking Intents.
Implementation Recipe
To handle the check intent, perform the following actions:
Validate the request:
- Verify
client_id,client_secret, andgrant_type(must beurn:ietf:params:oauth:grant-type:jwt-bearer). - Validate the
assertion(JWT) using the criteria in JWT Validation.
- Verify
Lookup user:
- Check if the Google Account ID (
sub) or email address in the JWT matches a user in your database.
- Check if the Google Account ID (
Respond:
- If found: Return HTTP
200 OKwith{"account_found": "true"}. - If not found: Return HTTP
404 Not Foundwith{"account_found": "false"}.
- If found: Return HTTP
Handle automatic linking (get intent)
If the account exists, Google calls your endpoint with intent=get to retrieve tokens. For parameter details, see Streamlined Linking Intents.
Implementation Recipe
To handle the get intent, perform the following actions:
Validate the request:
- Verify
client_id,client_secret, andgrant_type. - Validate the
assertion(JWT).
- Verify
Lookup user:
- Verify the user exists using the
suboremailclaim.
- Verify the user exists using the
Respond:
- If successful: Generate and return
access_token,refresh_token, andexpires_inin a JSON response (HTTP200 OK). - If linking fails: Return HTTP
401 Unauthorizedwith{"error": "linking_error"}and an optionallogin_hintto fall back to standard OAuth linking.
- If successful: Generate and return
Handle account creation using Sign in with Google (create intent)
If no account exists, Google calls your endpoint with intent=create to create a new user. For parameter details, see Streamlined Linking Intents.
Implementation Recipe
To handle the create intent, perform the following actions:
Validate the request:
- Verify
client_id,client_secret, andgrant_type. - Validate the
assertion(JWT).
- Verify
Verify user does not exist:
- Check if the
suboremailis already in your database. - If the user does exist: Return HTTP
401 Unauthorizedwith{"error": "linking_error", "login_hint": "USER_EMAIL"}to force fallback to OAuth linking.
- Check if the
Create account:
- Use the
sub,email,name, andpictureclaims from the JWT to create a new user record.
- Use the
Respond:
- Generate and return tokens in a JSON response (HTTP
200 OK).
- Generate and return tokens in a JSON response (HTTP
Google API クライアント ID を取得する
アカウント リンクの登録プロセスで、Google API クライアント ID を指定する必要があります。OAuth リンクの手順を完了する際に作成したプロジェクトを使用して API クライアント ID を取得します。手順は次のとおりです。
- [クライアント] ページに移動します。
Google APIs プロジェクトを作成または選択します。
プロジェクトにウェブ アプリケーション タイプのクライアント ID がない場合は、[クライアントを作成] をクリックして作成します。[承認済みの JavaScript 生成元] ボックスに、サイトのドメインを必ず含めてください。ローカルテストや開発を行う場合は、[承認済みの JavaScript の生成元] フィールドに
http://localhostとhttp://localhost:<port_number>の両方を追加する必要があります。
実装を検証する
You can validate your implementation by using the OAuth 2.0 Playground tool.
In the tool, do the following steps:
- Click Configuration to open the OAuth 2.0 Configuration window.
- In the OAuth flow field, select Client-side.
- In the OAuth Endpoints field, select Custom.
- Specify your OAuth 2.0 endpoint and the client ID you assigned to Google in the corresponding fields.
- In the Step 1 section, don't select any Google scopes. Instead, leave this field blank or type a scope valid for your server (or an arbitrary string if you don't use OAuth scopes). When you're done, click Authorize APIs.
- In the Step 2 and Step 3 sections, go through the OAuth 2.0 flow and verify that each step works as intended.
You can validate your implementation by using the Google Account Linking Demo tool.
In the tool, do the following steps:
- Click the Sign in with Google button.
- Choose the account you'd like to link.
- Enter the service ID.
- Optionally enter one or more scopes that you will request access for.
- Click Start Demo.
- When prompted, confirm that you may consent and deny the linking request.
- Confirm that you are redirected to your platform.