يضيف نوع الربط OAuth و"تسجيل الدخول باستخدام Google" تسجيل الدخول بحساب Google إلى جانب بروتوكول OAuth المستند إلى ربط الحسابات. يوفّر ذلك إمكانية الربط الصوتي بسلاسة لمستخدمي Google. مع تفعيل إمكانية ربط الحسابات للمستخدمين الذين سجّلوا في خدمتك باستخدام هوية غير تابعة لـ Google
ويبدأ هذا النوع من الربط بتسجيل الدخول بحساب Google، الذي يتيح لك معرفة ما إذا كان تتوفر معلومات الملف الشخصي في حساب Google في نظامك. إذا لم تكن معلومات المستخدم في نظامك، يبدأ تدفق OAuth العادي. يمكن للمستخدم أيضًا اختيار إنشاء حساب جديد باستخدام معلومات الملف الشخصي في Google.
لإجراء عملية ربط للحساب باستخدام OAuth وتسجيل الدخول باستخدام حساب Google، يُرجى اتّباع الخطوات التالية الخطوات:
- أولاً، اطلب من المستخدم منح موافقته على الوصول إلى ملفه الشخصي في Google.
- استخدِم المعلومات الواردة في ملفه الشخصي لتحديد هوية المستخدم.
- إذا لم تتمكن من العثور على مطابقة لمستخدم Google في نظام المصادقة لديك،
يعتمد المسار على ما إذا كنت قد ضبطت مشروع الإجراءات
في وحدة تحكم الإجراءات للسماح بإنشاء حسابات المستخدمين عبر الصوت أو فقط
موقعك الإلكتروني.
- إذا سمحت بإنشاء الحساب من خلال الصوت، يجب التحقّق من صحة رقم التعريف. الذي تم استلامه من Google. ويمكنك بعد ذلك إنشاء مستخدم استنادًا إلى معلومات الملف الشخصي المضمنة في الرمز المميز للمعرّف.
- إذا لم تكن تسمح بإنشاء الحساب عبر الصوت، سيتم نقل المستخدم إلى متصفّح يمكنهم من خلاله تحميل صفحة التفويض وإكمال المستخدم عملية صناعة المحتوى
دعم إنشاء الحسابات عبر الصوت
إذا سمحت بإنشاء حسابات المستخدمين من خلال الصوت، سيسأل "مساعد Google" المستخدم ما إذا كان يريد إجراء ما يلي:
- إنشاء حساب جديد على نظامك باستخدام معلومات حسابه على Google، أو
- سجِّل الدخول إلى نظام المصادقة باستخدام حساب مختلف إذا كان لديه حساب حالي غير تابع لـ Google
يوصى بالسماح بإنشاء الحسابات عبر الصوت إذا كنت تريد تقليل مع عملية إنشاء الحساب يحتاج المستخدم فقط إلى مغادرة تدفق الصوت إذا أراد تسجيل الدخول باستخدام حساب حالي غير تابع لـ Google
عدم السماح بإنشاء حسابات عبر الصوت
إذا لم تسمح بإنشاء حسابات المستخدمين عبر الصوت، سيفتح "مساعد Google" عنوان URL على الموقع الإلكتروني الذي قدمته لمصادقة المستخدم. إذا كان التفاعل يحدث على جهاز لا يحتوي على شاشة، يوجّه "مساعد Google" المستخدم إلى هاتف. لمواصلة عملية ربط الحسابات
يُنصح بعدم السماح بإنشاء المحتوى في الحالات التالية:
أنت لا تريد السماح للمستخدمين الذين لديهم حسابات غير تابعة لـ Google بإنشاء حساب حساب مستخدم وتريد أن يتم ربطه بحسابات المستخدمين الحالية في نظام المصادقة بدلاً من ذلك. على سبيل المثال، إذا كنت تقدم برنامج ولاء، التأكد من عدم خسار المستخدم للنقاط التي تم جمعها في حساب حالي.
ستحتاج إلى التحكم الكامل في عملية إنشاء الحساب. على سبيل المثال، يمكنك عدم السماح بالإنشاء إذا كنت بحاجة إلى عرض بنود الخدمة للمستخدم أثناء إنشاء حساب.
تنفيذ عملية ربط بروتوكول OAuth بحساب "تسجيل الدخول بحساب Google"
ترتبط الحسابات بمسارات OAuth 2.0 العادية في المجال. تتوافق "المهام مع مساعد Google" مع تدفّقات الرموز الضمنية ورموز التفويض.
في مسار الرمز الضمني، تفتح Google نقطة نهاية التفويض في متصفّح المستخدم. بعد تسجيل الدخول بنجاح، يمكنك إرجاع رمز الدخول الطويل الأمد إلى Google. يتم الآن تضمين رمز الدخول هذا في كل طلب يتم إرساله من "مساعد Google" إلى الإجراء.
في مسار رمز التفويض، ستحتاج إلى نقطتَي نهاية:
- نقطة نهاية التفويض المسؤولة عن تقديم واجهة مستخدم تسجيل الدخول للمستخدمين الذين لم يسجّلوا الدخول وتسجيل الموافقة على الوصول المطلوب في شكل رمز تفويض قصير الأجل.
- نقطة نهاية تبادل الرموز المسؤولة عن نوعَين من عمليات التبادل:
- تبادل رمز التفويض لرمز مميز لإعادة التحميل طويل الأجل ورمز دخول قصير الأجل وتتم عملية التبادل هذه عندما يجري المستخدم عملية ربط الحساب.
- لتبادل رمز مميز لإعادة التحميل طويل الأمد لرمز دخول قصير الأجل وتتم عملية التبادل هذه عندما تحتاج Google إلى رمز دخول جديد لأنّ الرمز الذي انتهت صلاحيته.
على الرغم من بساطة تنفيذ الرمز الضمني، تنصح Google بعدم انتهاء صلاحية رموز الدخول الضمنية التي تم إصدارها باستخدام التدفّق الضمني، لأنّ انتهاء صلاحية الرمز المميّز مع المسار الضمني يفرض على المستخدم ربط حسابه مرة أخرى. إذا كنت بحاجة إلى انتهاء صلاحية الرمز المميَّز لأسباب تتعلق بالأمان، ننصحك بشدة باستخدام تدفق رمز المصادقة بدلاً من ذلك.
ضبط المشروع
لضبط مشروعك على استخدام OAuth وحساب "تسجيل الدخول بحساب Google" الربط، اتبع الخطوات التالية:
- افتح وحدة تحكّم المهام واختَر المشروع الذي تريد استخدامه.
- انقر على علامة التبويب التطوير واختر ربط الحساب.
- فعِّل مفتاح التبديل بجانب ربط الحساب.
- في القسم إنشاء الحساب، اختَر نعم.
في نوع الربط، اختَر OAuth تسجيل الدخول باستخدام حساب Google والتطبيق الضمني
في معلومات العميل، عليك إجراء ما يلي:
- حدِّد قيمةً للرقم التعريفي للعميل الصادر عن خدمة "المهام مع مساعد Google" لتحديد الطلبات الواردة من Google.
- أدرِج عناوين URL لنقاط نهاية التفويض وToken Exchange.
انقر على حفظ.
تنفيذ خادم OAuth
لدعم تدفق OAuth 2.0 الضمني، تُجري خدمتك تفويضًا. نقطة النهاية متوفرة عبر HTTPS. نقطة النهاية هذه مسؤولة عن مصادقة الحصول على موافقة من المستخدمين للوصول إلى البيانات. نقطة نهاية التفويض توفّر واجهة مستخدم لتسجيل الدخول للمستخدمين الذين لم يسجّلوا الدخول إلى حساباتهم الموافقة على الوصول المطلوب.
عندما يحتاج الإجراء الخاص بك إلى استدعاء إحدى واجهات برمجة التطبيقات المعتمَدة لخدمتك، تستخدمها Google. نقطة النهاية هذه للحصول على إذن من المستخدمين بطلب واجهات برمجة التطبيقات هذه على بالنيابة عنك.
تتضمن جلسة مسار OAuth 2.0 الضمني النموذجية التي تبدأها Google التدفق التالي:
- تفتح Google نقطة نهاية التفويض في متصفّح المستخدم. تشير رسالة الأشكال البيانية تسجيل دخول المستخدم إذا لم يكن مسجّلاً الدخول، ويمنح Google إذنًا بالوصول بياناته من خلال واجهة برمجة التطبيقات إذا لم يسبق له منحها الإذن بذلك.
- تنشئ الخدمة رمز دخول وتعيده إلى Google عن طريق إعادة توجيه متصفّح المستخدم إلى Google باستخدام رمز الدخول المرفق بالطلب.
- تستدعي Google واجهات برمجة التطبيقات التابعة لخدمتك، وترفق رمز الدخول المميز لكل طلب. تتحقّق خدمتك من أنّ رمز الدخول يمنح Google. للوصول إلى واجهة برمجة التطبيقات ثم يُكمل طلب البيانات من واجهة برمجة التطبيقات.
التعامل مع طلبات التفويض
عندما يحتاج الإجراء الخاص بك إلى ربط الحساب من خلال تدفق OAuth2 الضمني، ترسل Google المستخدم إلى نقطة نهاية التفويض من خلال طلب يتضمن المعلمات التالية:
| مَعلمات نقطة نهاية التفويض | |
|---|---|
client_id |
معرِّف العميل الذي عيّنته لـ Google. |
redirect_uri |
عنوان URL الذي ترسل إليه الرد على هذا الطلب. |
state |
يشير هذا المصطلح إلى قيمة محاسبة يتم إرسالها إلى Google بدون أي تغيير في معرّف الموارد المنتظم (URI) لإعادة التوجيه. |
response_type |
نوع القيمة المطلوب عرضها في الرد. بالنسبة إلى بروتوكول OAuth 2.0 الضمني
يكون نوع الاستجابة دائمًا token. |
على سبيل المثال، إذا كانت نقطة نهاية التفويض متاحة على https://myservice.example.com/auth،
قد يبدو الطلب كما يلي:
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&response_type=token
لكي تعالج نقطة نهاية التفويض طلبات تسجيل الدخول، عليك اتّباع الخطوات التالية:
يُرجى التحقّق من القيمتَين
client_idوredirect_uriمن أجل: منع منح الوصول إلى تطبيقات العميل غير المقصودة أو التي تم إعدادها بشكلٍ غير صحيح:- تأكَّد من أنّ
client_idيتطابق مع معرّف العميل الذي أدخلته. تعيينه إلى Google. - تأكَّد من أنّ عنوان URL المحدّد من قِبل
redirect_uriعلى النحو التالي: YOUR_PROJECT_ID هو رقم التعريف الوارد في صفحة إعدادات المشروع. من وحدة التحكم في المهام.https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
- تأكَّد من أنّ
تحقق مما إذا كان المستخدم قد سجّل الدخول إلى خدمتك. في حال لم يكن المستخدم مسجِّلاً الدخول أو أكمل عملية تسجيل الدخول أو الاشتراك في الخدمة.
أنشئ رمز الدخول المميز الذي ستستخدمه Google للوصول إلى واجهة برمجة التطبيقات الخاصة بك. تشير رسالة الأشكال البيانية يمكن أن يكون رمز الدخول أي قيمة سلسلة، ولكن يجب أن يمثل المستخدم والعميل الذي يرتبط به الرمز المميز ويجب ألا يكون من الممكن تخمينه.
إرسال استجابة HTTP تؤدي إلى إعادة توجيه متصفّح المستخدم إلى عنوان URL التي تحددها المعلمة
redirect_uri. تضمين جميع المعلمات التالية في جزء عنوان URL:access_token: رمز الدخول الذي أنشأته للتوtoken_type: السلسلةbearerstate: قيمة الحالة غير المعدَّلة من القيمة الأصلية طلب في ما يلي مثال على عنوان URL الناتج:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID#access_token=ACCESS_TOKEN&token_type=bearer&state=STATE_STRING
سيستلم معالج إعادة التوجيه OAuth 2.0 من Google رمز الدخول وسيتأكد من ذلك
أن قيمة state لم تتغير. بعد حصول Google على
رمز الدخول لخدمتك، فستلحق Google الرمز المميز بالمكالمات اللاحقة
إلى الإجراء الخاص بك كجزء من AppRequest.
Handle automatic linking
After the user gives your Action consent to access their Google profile, Google sends a request that contains a signed assertion of the Google user's identity. The assertion contains information that includes the user's Google Account ID, name, and email address. The token exchange endpoint configured for your project handles that request.
If the corresponding Google account is already present in your authentication system,
your token exchange endpoint returns a token for the user. If the Google account doesn't
match an existing user, your token exchange endpoint returns a user_not_found error.
The request has the following form:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&intent=get&assertion=JWT&consent_code=CONSENT_CODE&scope=SCOPES
Your token exchange endpoint must be able to handle the following parameters:
| Token endpoint parameters | |
|---|---|
grant_type |
The type of token being exchanged. For these requests, this
parameter has the value urn:ietf:params:oauth:grant-type:jwt-bearer. |
intent |
For these requests, the value of this parameter is `get`. |
assertion |
A JSON Web Token (JWT) that provides a signed assertion of the Google user's identity. The JWT contains information that includes the user's Google Account ID, name, and email address. |
consent_code |
Optional: When present, a one-time code that indicates that the user has granted consent for your Action to access the specified scopes. |
scope |
Optional: Any scopes you configured Google to request from users. |
When your token exchange endpoint receives the linking request, it should do the following:
Validate and decode the JWT assertion
You can validate and decode the JWT assertion by using a JWT-decoding library for your language. Use Google's public keys (available in JWK or PEM format) to verify the token's signature.
When decoded, the JWT assertion looks like the following example:
{ "sub": 1234567890, // The unique ID of the user's Google Account "iss": "https://accounts.google.com", // The assertion's issuer "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID "iat": 233366400, // Unix timestamp of the assertion's creation time "exp": 233370000, // Unix timestamp of the assertion's expiration time "name": "Jan Jansen", "given_name": "Jan", "family_name": "Jansen", "email": "jan@gmail.com", // If present, the user's email address "locale": "en_US" }
In addition to verifying the token's signature, verify that the assertion's issuer
(iss field) is https://accounts.google.com and that the audience (aud field)
is the client ID assigned to your Action.
Check if the Google account is already present in your authentication system
Check whether either of the following conditions are true:
- The Google Account ID, found in the assertion's
subfield, is in your user database. - The email address in the assertion matches a user in your user database.
If either condition is true, the user has already signed up and you can issue an access token.
If neither the Google Account ID nor the email address specified in the assertion
matches a user in your database, the user hasn't signed up yet. In this case, your
token exchange endpoint should reply with a HTTP 401 error, that specifies error=user_not_found,
as in the following example:
HTTP/1.1 401 Unauthorized
Content-Type: application/json;charset=UTF-8
{
"error":"user_not_found",
}
user_not_found error, Google
calls your token exchange endpoint with the value of the intent parameter
set to create and sending an ID token that contains the user's profile information
with the request.
Handle account creation via Google Sign-In
When a user needs to create an account on your service, Google makes a
request to your token exchange endpoint that specifies
intent=create, as in the following example:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded response_type=token&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&scope=SCOPES&intent=create&consent_code=CONSENT_CODE&assertion=JWT[&NEW_ACCOUNT_INFO]
The assertion parameter contains A JSON Web Token (JWT) that provides
a signed assertion of the Google user's identity. The JWT contains information
that includes the user's Google Account ID, name, and email address, which you can use
to create a new account on your service.
To respond to account creation requests, your token exchange endpoint must do the following:
Validate and decode the JWT assertion
You can validate and decode the JWT assertion by using a JWT-decoding library for your language. Use Google's public keys (available in JWK or PEM format) to verify the token's signature.
When decoded, the JWT assertion looks like the following example:
{ "sub": 1234567890, // The unique ID of the user's Google Account "iss": "https://accounts.google.com", // The assertion's issuer "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID "iat": 233366400, // Unix timestamp of the assertion's creation time "exp": 233370000, // Unix timestamp of the assertion's expiration time "name": "Jan Jansen", "given_name": "Jan", "family_name": "Jansen", "email": "jan@gmail.com", // If present, the user's email address "locale": "en_US" }
In addition to verifying the token's signature, verify that the assertion's issuer
(iss field) is https://accounts.google.com and that the audience (aud field)
is the client ID assigned to your Action.
Validate user information and create new account
Check whether either of the following conditions are true:
- The Google Account ID, found in the assertion's
subfield, is in your user database. - The email address in the assertion matches a user in your user database.
If either condition is true, prompt the user to link their existing account with
their Google Account by responding to the request with an HTTP 401 error, specifying
error=linking_error and the user's email address as the login_hint, as in the
following example:
HTTP/1.1 401 Unauthorized
Content-Type: application/json;charset=UTF-8
{
"error":"linking_error",
"login_hint":"foo@bar.com"
}
If neither condition is true, create a new user account using the information provided in the JWT. New accounts do not typically have a password set. It is recommended that you add Google Sign In to other platforms to enable users to log in via Google across the surfaces of your application. Alternatively, you can email the user a link that starts your password recovery flow to allow the user to set a password for signing in on other platforms.
When the creation is completed, issue an access token and return the values in a JSON object in the body of your HTTPS response, like in the following example:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
بدء مسار المصادقة
استخدِم هدف المساعدة الخاص بتسجيل الدخول إلى الحساب. لبدء عملية المصادقة
const app = dialogflow({ // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT clientId: CLIENT_ID, }) // Intent that starts the account linking flow. app.intent('Start Signin', conv => { conv.ask(new SignIn('To get your account details')) })
private String clientId = "<your_client_id>"; @ForIntent("Start Signin") public ActionResponse text(ActionRequest request) { ResponseBuilder rb = getResponseBuilder(request); return rb.add(new SignIn().setContext("To get your account details")).build(); }
const app = actionssdk({ clientId: CLIENT_ID, }) app.intent('Start Signin', conv => { conv.ask(new SignIn('To get your account details')) })
private String clientId = "<your_client_id>"; @ForIntent("actions.intent.TEXT") public ActionResponse text(ActionRequest request) { ResponseBuilder rb = getResponseBuilder(request); return rb.add(new SignIn().setContext("To get your account details")).build(); }
التعامل مع طلبات الوصول إلى البيانات
إذا كان طلب "مساعد Google" يحتوي على رمز دخول، تحقق أولاً من أن رمز الدخول صالح وغير منتهي الصلاحية ثم استرجعه من قاعدة بيانات حساب المستخدم، وحساب المستخدم المرتبط بالرمز المميز.