אימות באמצעות Yahoo ו-C++
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
אתם יכולים לאפשר למשתמשים לבצע אימות ב-Firebase באמצעות ספקי OAuth כמו Yahoo. לשם כך, משלבים באפליקציה כניסה גנרית מבוססת-אינטרנט באמצעות OAuth באמצעות Firebase SDK כדי לבצע את תהליך הכניסה מקצה לקצה. התהליך הזה מחייב
באמצעות ערכות SDK מבוססות-טלפון של Firebase, היא נתמכת רק ב-Android
פלטפורמות של Apple.
לפני שמתחילים
- מוסיפים את Firebase לפרויקט C++.
- במסוף Firebase, פותחים את הקטע אימות.
- בכרטיסייה Sign in method (שיטת כניסה), מפעילים את הספק Yahoo.
- מוסיפים את Client ID ואת Client Secret ממסוף המפתחים של הספק הזה אל
תצורת ספק:
-
כדי לרשום לקוח OAuth של Yahoo, צריך לפעול לפי ההוראות של מפתחי Yahoo
תיעוד בנושא
רישום אפליקציית אינטרנט ב-Yahoo!
חשוב לבחור את שתי ההרשאות של OpenID Connect API:
profile
וגם email
- כשרושמים אפליקציות אצל הספקים האלה, חשוב לרשום את הדומיין
*.firebaseapp.com
של הפרויקט כדומיין להפניה אוטומטית של האפליקציה.
- לוחצים על שמירה.
גישה לכיתה firebase::auth::Auth
המחלקה
Auth
היא השער לכל הקריאות ל-API.
- מוסיפים את קובצי הכותרות של 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::auth::Auth
ל-firebase::App
.
יש מיפוי אחד לאחד בין App
לבין Auth
.
firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
טיפול בתהליך הכניסה באמצעות Firebase SDK
כדי לבצע את תהליך הכניסה באמצעות Firebase SDK, צריך לבצע את השלבים הבאים:
בנייה של מופע של FederatedOAuthProviderData
שהוגדר באמצעות
את מזהה הספק המתאים ל-Yahoo.
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,
מסמכי תיעוד של OAuth ב-Yahoo
שימו לב: אי אפשר להעביר פרמטרים שנדרשים ל-Firebase באמצעות custom_parameters()
. הפרמטרים האלה הם client_id,
redirect_uri, response_type, scope ו-state.
אופציונלי: מציינים היקפי הרשאות נוספים של OAuth 2.0, מעבר ל-profile
ול-email
, שרוצים לבקש מספק האימות. אם
נדרשת גישה לנתוני משתמשים פרטיים מ-Yahoo APIs,
תצטרכו לבקש הרשאות ל-Yahoo APIs בקטע הרשאות API
Yahoo Developer Console. היקפי ה-OAuth המבוקשים חייבים להיות זהים להיקפים שהוגדרו מראש בהרשאות ה-API של האפליקציה. לדוגמה, אם מבקשים גישה לקריאה וכתיבה לאנשי הקשר של המשתמש והיא מוגדרת מראש בהרשאות ה-API של האפליקציה, צריך להעביר את הערך sdct-w
במקום את היקף הרשאת ה-OAuth לקריאה בלבד sdct-r
. אחרת,התהליך ייכשל ותוצג שגיאה
למשתמש קצה.
// 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);
מבצעים אימות עם Firebase באמצעות אובייקט של ספק האימות. שימו לב שבניגוד ל-
פעולות אחרות של FirebaseAuth – הן יכולות לשלוט בממשק המשתמש שלכם על ידי הקפצה
תצוגת אינטרנט שבה המשתמש יכול להזין את פרטי הכניסה שלו.
כדי להתחיל את תהליך הכניסה, צריך לקרוא ל-SignInWithProvider
:
firebase::Future<firebase::auth::AuthResult> result =
auth->SignInWithProvider(provider_data);
לאחר מכן הבקשה עשויה להמתין או
רישום קריאה חוזרת (callback) לגבי העתיד.
הדוגמאות שלמעלה מתמקדות בתהליכי הכניסה, אבל יש גם
יכולת לקשר ספק של Yahoo למשתמש קיים באמצעות
LinkWithProvider
לדוגמה, אפשר לקשר כמה ספקים לאותו משתמש ולאפשר לו להיכנס באמצעות כל אחד מהם.
firebase::Future<firebase::auth::AuthResult> result = user.LinkWithProvider(provider_data);
אפשר להשתמש באותו דפוס עם ReauthenticateWithProvider
, שאפשר להשתמש בו כדי לאחזר פרטי כניסה עדכניים לפעולות רגישות שדורשות כניסה לאחרונה.
firebase::Future<firebase::auth::AuthResult> result =
user.ReauthenticateWithProvider(provider_data);
לאחר מכן, האפליקציה יכולה להמתין או לרשום קריאה חוזרת בעתיד.
מתקדם: טיפול בתהליך הכניסה באופן ידני
בניגוד לספקים אחרים של OAuth שנתמכים על ידי Firebase, כמו Google, Facebook ו-Twitter, שבהם אפשר להיכנס ישירות באמצעות פרטי כניסה שמבוססים על אסימון גישה של OAuth, אימות ב-Firebase לא תומך באותה יכולת עבור ספקים כמו Yahoo, כי שרת האימות ב-Firebase לא יכול לאמת את הקהל של אסימוני הגישה של Yahoo ב-OAuth.
זוהי דרישת אבטחה קריטית, והיא עלולה לחשוף אפליקציות ואתרים למתקפות של שחזור, שבהן אפשר להשתמש באסימון גישה של Yahoo OAuth שנוצר לפרויקט אחד (תוקף) כדי להיכנס לפרויקט אחר (קורבן).
במקום זאת, Firebase Auth מאפשר לטפל בכל תהליך OAuth
החלפה של קוד ההרשאה באמצעות מזהה וסוד הלקוח של OAuth
שהוגדר במסוף Firebase. מכיוון שאפשר להשתמש בקוד ההרשאה רק בשילוב עם מזהה לקוח או סוד לקוח ספציפיים, אי אפשר להשתמש בקוד הרשאה שהתקבל לפרויקט אחד בפרויקט אחר.
אם צריך להשתמש בספקים האלה בסביבות שלא נתמכות, צריך להשתמש בספריית OAuth של צד שלישי ובאימות מותאם אישית של Firebase. הראשון נדרש כדי לבצע אימות מול הספק, והשני נדרש כדי להחליף את פרטי הכניסה של הספק באסימון מותאם אישית.
השלבים הבאים
אחרי שמשתמש נכנס לחשבון בפעם הראשונה, נוצר חשבון משתמש חדש שמקושר לפרטי הכניסה – כלומר שם המשתמש והסיסמה, מספר הטלפון או פרטי ספק האימות – שבאמצעותם המשתמש נכנס לחשבון. החדש הזה
מאוחסנים כחלק מפרויקט Firebase, וניתן להשתמש בהם כדי לזהות
משתמש בכל האפליקציות בפרויקט, בלי קשר לאופן שבו המשתמשים נכנסים לחשבון.
-
באפליקציות שלכם, תוכלו לקבל את פרטי הפרופיל הבסיסיים של המשתמש דרך
אובייקט firebase::auth::User
:
firebase::auth::User user = auth->current_user();
if (user.is_valid()) {
std::string name = user.display_name();
std::string email = user.email();
std::string photo_url = user.photo_url();
// The user's ID, unique to the Firebase project.
// Do NOT use this value to authenticate with your backend server,
// if you have one. Use firebase::auth::User::Token() instead.
std::string uid = user.uid();
}
בתוך Firebase Realtime Database ו-Cloud Storage
כללי אבטחה –
מקבלים את מזהה המשתמש הייחודי של המשתמש המחובר מהמשתנה auth
,
ולהשתמש בהם כדי לקבוע לאילו נתונים המשתמש יוכל לגשת.
אפשר לאפשר למשתמשים להיכנס לאפליקציה באמצעות מספר סוגי אימות
מספקים באמצעות קישור פרטי הכניסה של ספק האימות
קיים חשבון משתמש קיים.
כדי להוציא משתמש, קוראים לפונקציה
SignOut()
:
auth->SignOut();
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 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```"]]