Yahoo と C ++ を使用して認証する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Firebase SDK を使用してウェブベースの汎用 OAuth ログインをアプリに統合し、エンドツーエンドのログインフローを実行することで、ユーザーが Firebase での認証に Yahoo などの OAuth プロバイダを使用できるようになります。このフローでは、スマートフォン ベースの Firebase SDK を使用する必要があるため、Android プラットフォームと Apple プラットフォームでのみサポートされます。
始める前に
- Firebase を C++ プロジェクトに追加します。
- Firebase コンソールで [Auth] セクションを開きます。
- [Sign-in method] タブで、[Yahoo!] プロバイダを有効にします。
- そのプロバイダのデベロッパー コンソールで取得したクライアント ID とクライアント シークレットをプロバイダ構成に追加します。
-
Yahoo OAuth クライアントを登録するには、Yahoo でのウェブ アプリケーションの登録に関する Yahoo のデベロッパー向けドキュメントの手順を実施してください。
必ず、OpenID Connect API の 2 つの権限(profile
と email
)を選択します。
- こうしたプロバイダにアプリを登録するときは、必ずプロジェクトの
*.firebaseapp.com
ドメインをアプリのリダイレクト ドメインとして登録してください。
- [保存] をクリックします。
firebase::auth::Auth
クラスへのアクセス
すべての API 呼び出しは
Auth
クラスを使用して行われます。
- Auth ヘッダー ファイルと App ヘッダー ファイルを追加します。
#include "firebase/app.h"
#include "firebase/auth.h"
- 初期化コードで
firebase::App
クラスを作成します。
#if defined(__ANDROID__)
firebase::App* app =
firebase::App::Create(firebase::AppOptions(), my_jni_env, my_activity);
#else
firebase::App* app = firebase::App::Create(firebase::AppOptions());
#endif // defined(__ANDROID__)
firebase::App
の firebase::auth::Auth
クラスを取得します。App
と Auth
は、1 対 1 で対応しています。firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
Firebase SDK を使用したログインフローの処理
Firebase SDK でログインフローを処理する手順は次のとおりです。
Yahoo に適したプロバイダ ID で構成された FederatedOAuthProviderData
のインスタンスを作成します。
firebase::auth::FederatedOAuthProviderData
provider_data(firebase::auth::YahooAuthProvider::kProviderId);
省略可: OAuth リクエストと一緒に送信したい追加のカスタム OAuth パラメータを指定します。
// Prompt user to re-authenticate to Yahoo.
provider_data.custom_parameters["prompt"] = "login";
// Localize to French.
provider_data.custom_parameters["language"] = "fr";
Yahoo がサポートするパラメータについては、Yahoo の OAuth に関するドキュメントをご覧ください。custom_parameters()
で Firebase の必須パラメータを渡すことはできません。該当するパラメータは、client_id、redirect_uri、response_type、scope、state です。
省略可: 認証プロバイダにリクエストする、profile
と email
を超える追加の OAuth 2.0 スコープを指定します。アプリケーションで Yahoo API から限定公開のユーザーデータへのアクセスが必要な場合は、Yahoo のデベロッパー コンソールの [API Permissions] で、Yahoo API に権限をリクエストする必要があります。リクエストされた OAuth スコープは、アプリの API 権限で事前構成されたものと完全一致する必要があります。たとえば、読み取り / 書き込みアクセスがユーザーの連絡先にリクエストされ、それがアプリの API 権限で事前構成されている場合は、読み取り専用の OAuth スコープ sdct-r
の代わりに sdct-w
を渡す必要があります。渡さないと、フローは失敗し、エンドユーザーにエラーが表示されます。
// Request access to Yahoo Mail API.
provider_data.scopes.push_back("mail-r");
// This must be preconfigured in the app's API permissions.
provider_data.scopes.push_back("sdct-w");
詳しくは、Yahoo のスコープに関するドキュメントをご覧ください。
プロバイダのデータを設定したら、それを使用して FederatedOAuthProvider を作成します。
// Construct a FederatedOAuthProvider for use in Auth methods.
firebase::auth::FederatedOAuthProvider provider(provider_data);
Auth プロバイダ オブジェクトを使用して Firebase での認証を行います。こうすると、他の FirebaseAuth オペレーションとは異なり、ユーザーが認証情報を入力できるウェブビューをポップアップ表示して UI を制御することになります。
ログインフローを開始するには、SignInWithProvider
を呼び出します。
firebase::Future<firebase::auth::AuthResult> result =
auth->SignInWithProvider(provider_data);
その後、プログラムは待機するか、Future にコールバックを登録します。
上記の例ではログインフローを中心に説明していますが、LinkWithProvider
を使用して Yahoo プロバイダを既存のユーザーにリンクすることもできます。たとえば、複数のプロバイダを同じユーザーにリンクして、どれでもログインを可能にできます。
firebase::Future<firebase::auth::AuthResult> result = user.LinkWithProvider(provider_data);
同じパターンを ReauthenticateWithProvider
でも使用できます。これは、ログインしてから短時間のうちに行うことが求められる機密性の高いオペレーションのための、最新の認証情報の取得に使用できます。
firebase::Future<firebase::auth::AuthResult> result =
user.ReauthenticateWithProvider(provider_data);
その後、アプリケーションは待機するか、Future にコールバックを登録します。
高度: 手動によるログインフローの処理
Firebase でサポートされている他の OAuth プロバイダ(Google、Facebook、Twitter など)では OAuth アクセス トークンに基づいた認証情報を使用して直接ログインできますが、Yahoo などのプロバイダでは Firebase Auth が同様の機能をサポートしていません。これは Yahoo OAuth アクセス トークンの対象デバイスを検証するのに Firebase Auth サーバーを使用できないためです。これは重要なセキュリティ要件です。こうしなければ、アプリケーションやウェブサイトをリプレイ攻撃にさらすことになり、攻撃者があるプロジェクト用に取得した Yahoo OAuth アクセス トークンを使用して別のプロジェクト(被害者)にログインする恐れがあります。代わりに Firebase Auth では、Firebase コンソールで構成した OAuth のクライアント ID とクライアント シークレットを使用して、OAuth フロー全体と認証コード交換を処理する機能を提供しています。認証コードは特定のクライアント ID / シークレットの組み合わせでのみ使用できるため、あるプロジェクトで取得した認証コードを別のプロジェクトで使用することはできません。
このようなプロバイダを、サポートされていない環境で使用する必要がある場合は、サードパーティの OAuth ライブラリと Firebase カスタム認証を使用する必要があります。前者はプロバイダとの認証に、後者はプロバイダの認証情報をカスタム トークンと交換するために必要です。
次のステップ
ユーザーが初めてログインすると、新しいユーザー アカウントが作成され、ユーザーがログイン時に使用した認証情報(ユーザー名とパスワード、電話番号、または認証プロバイダ情報)にアカウントがリンクされます。この新しいアカウントは Firebase プロジェクトの一部として保存され、ユーザーのログイン方法にかかわらず、プロジェクトのすべてのアプリでユーザーを識別するために使用できます。
既存のユーザー アカウントに認証プロバイダの認証情報をリンクすることで、ユーザーは複数の認証プロバイダを使用してアプリにログインできるようになります。
ユーザーのログアウトを行うには、SignOut()
を呼び出します。
auth->SignOut();
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-16 UTC。
[null,null,["最終更新日 2025-08-16 UTC。"],[],[],null,["You can let your users authenticate with Firebase using OAuth providers like\nYahoo by integrating web-based generic OAuth Login into your app using the\nFirebase SDK to carry out the end to end sign-in flow. Since this flow requires\nthe use of the phone-based Firebase SDKs, it is only supported on Android and\nApple platforms.\n\nBefore you begin\n\n1. [Add Firebase to your C++ project](/docs/cpp/setup#note_select_platform).\n2. In the [Firebase console](//console.firebase.google.com/), open the **Auth** section.\n3. On the **Sign in method** tab, enable the **Yahoo** provider.\n4. Add the **Client ID** and **Client Secret** from that provider's developer console to the provider configuration:\n 1. To register a Yahoo OAuth client, follow the Yahoo developer\n documentation on [registering a web application with Yahoo](https://developer.yahoo.com/oauth2/guide/openid_connect/getting_started.html).\n\n Be sure to select the two OpenID Connect API permissions:\n `profile` and `email`.\n 2. When registering apps with these providers, be sure to register the `*.firebaseapp.com` domain for your project as the redirect domain for your app.\n5. Click **Save**.\n\nAccess the `firebase::auth::Auth` class The `Auth` class is the gateway for all API calls.\n\n1. Add the Auth and App header files: \n\n ```c++\n #include \"firebase/app.h\"\n #include \"firebase/auth.h\"\n ```\n2. In your initialization code, create a [`firebase::App`](/docs/reference/cpp/class/firebase/app) class. \n\n ```c++\n #if defined(__ANDROID__)\n firebase::App* app =\n firebase::App::Create(firebase::AppOptions(), my_jni_env, my_activity);\n #else\n firebase::App* app = firebase::App::Create(firebase::AppOptions());\n #endif // defined(__ANDROID__)\n ```\n3. Acquire the `firebase::auth::Auth` class for your `firebase::App`. There is a one-to-one mapping between `App` and `Auth`. \n\n ```c++\n firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);\n ```\n\nHandle the sign-in flow with the Firebase SDK\n\nTo handle the sign-in flow with the Firebase SDK, follow these steps:\n\n1. Construct an instance of a `FederatedOAuthProviderData` configured with\n the provider ID appropriate for Yahoo.\n\n firebase::auth::FederatedOAuthProviderData\n provider_data(firebase::auth::YahooAuthProvider::kProviderId);\n\n2. **Optional**: Specify additional custom OAuth parameters that you want to\n send with the OAuth request.\n\n // Prompt user to re-authenticate to Yahoo.\n provider_data.custom_parameters[\"prompt\"] = \"login\";\n\n // Localize to French.\n provider_data.custom_parameters[\"language\"] = \"fr\";\n\n For the parameters Yahoo supports, see the\n [Yahoo OAuth documentation](https://developer.yahoo.com/oauth2/guide/openid_connect/getting_started.html).\n Note that you can't pass Firebase-required parameters with\n `custom_parameters()`. These parameters are **client_id** ,\n **redirect_uri** , **response_type** , **scope** and **state**.\n3. **Optional** : Specify additional OAuth 2.0 scopes beyond `profile` and\n `email` that you want to request from the authentication provider. If your\n application requires access to private user data from Yahoo APIs, you'll\n need to request permissions to Yahoo APIs under **API Permissions** in the\n Yahoo developer console. Requested OAuth scopes must be exact matches to the\n preconfigured ones in the app's API permissions. For example if, read/write\n access is requested to user contacts and preconfigured in the app's API\n permissions, `sdct-w` has to be passed instead of the readonly OAuth scope\n `sdct-r`. Otherwise,the flow will fail and an error would be shown to the\n end user.\n\n // Request access to Yahoo Mail API.\n provider_data.scopes.push_back(\"mail-r\");\n // This must be preconfigured in the app's API permissions.\n provider_data.scopes.push_back(\"sdct-w\");\n\n To learn more, refer to the\n [Yahoo scopes documentation](https://developer.yahoo.com/oauth2/guide/yahoo_scopes/).\n4. Once your provider data has been configured, use it to create a\n FederatedOAuthProvider.\n\n // Construct a FederatedOAuthProvider for use in Auth methods.\n firebase::auth::FederatedOAuthProvider provider(provider_data);\n\n5. Authenticate with Firebase using the Auth provider object. Note that unlike\n other FirebaseAuth operations, this will take control of your UI by popping\n up a web view in which the user can enter their credentials.\n\n To start the sign in flow, call `SignInWithProvider`: \n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result =\n auth-\u003eSignInWithProvider(provider_data);\n\n Your application may then wait or [register a callback on the Future](#register_callback_on_future).\n6. While the above examples focus on sign-in flows, you also have the\n ability to link a Yahoo provider to an existing user using\n `LinkWithProvider`. For example, you can link multiple\n providers to the same user allowing them to sign in with either.\n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result = user.LinkWithProvider(provider_data);\n\n7. The same pattern can be used with `ReauthenticateWithProvider` which can be\n used to retrieve fresh credentials for sensitive operations that require\n recent login.\n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result =\n user.ReauthenticateWithProvider(provider_data);\n\n Your application may then wait or [register a callback on\n the Future](#register_callback_on_future).\n\nAdvanced: Handle the sign-in flow manually\n\nUnlike other OAuth providers supported by Firebase such as Google, Facebook,\nand Twitter, where sign-in can directly be achieved with OAuth access token\nbased credentials, Firebase Auth does not support the same capability for\nproviders such as Yahoo due to the inability of the Firebase\nAuth server to verify the audience of Yahoo OAuth access tokens.\nThis is a critical security requirement and could expose applications and\nwebsites to replay attacks where a Yahoo OAuth access token obtained for\none project (attacker) can be used to sign in to another project (victim).\nInstead, Firebase Auth offers the ability to handle the entire OAuth flow and\nthe authorization code exchange using the OAuth client ID and secret\nconfigured in the Firebase Console. As the authorization code can only be used\nin conjunction with a specific client ID/secret, an authorization code\nobtained for one project cannot be used with another.\n\nIf these providers are required to be used in unsupported environments, a\nthird party OAuth library and\n[Firebase custom authentication](../admin/create-custom-tokens)\nwould need to be used. The former is needed to authenticate with the provider\nand the latter to exchange the provider's credential for a custom token.\n\nNext steps\n\nAfter a user signs in for the first time, a new user account is created and\nlinked to the credentials---that is, the user name and password, phone\nnumber, or auth provider information---the user signed in with. This new\naccount is stored as part of your Firebase project, and can be used to identify\na user across every app in your project, regardless of how the user signs in.\n\n- In your apps, you can get the user's basic profile information from the\n [`firebase::auth::User`](/docs/reference/cpp/class/firebase/auth/user) object:\n\n ```c++\n firebase::auth::User user = auth-\u003ecurrent_user();\n if (user.is_valid()) {\n std::string name = user.display_name();\n std::string email = user.email();\n std::string photo_url = user.photo_url();\n // The user's ID, unique to the Firebase project.\n // Do NOT use this value to authenticate with your backend server,\n // if you have one. Use firebase::auth::User::Token() instead.\n std::string uid = user.uid();\n }\n ```\n- In your Firebase Realtime Database and Cloud Storage\n [Security Rules](/docs/database/security/user-security), you can\n get the signed-in user's unique user ID from the `auth` variable,\n and use it to control what data a user can access.\n\nYou can allow users to sign in to your app using multiple authentication\nproviders by [linking auth provider credentials to an\nexisting user account.](/docs/auth/cpp/account-linking)\n\nTo sign out a user, call [`SignOut()`](/docs/reference/cpp/class/firebase/auth/auth#signout): \n\n```c++\nauth-\u003eSignOut();\n```"]]