الربط السلس باستخدام OAuth وتسجيل الدخول إلى Google

نظرة عامة

تضيف ميزة الربط السلس لتسجيل الدخول بحساب Google OAuth ميزة "تسجيل الدخول بحساب Google" بالإضافة إلى ربط OAuth. ويوفّر ذلك تجربة ربط سلسة لمستخدمي Google، ويساعد أيضًا على إنشاء الحساب، ما يسمح للمستخدم بإنشاء حساب جديد على خدمتك باستخدام حسابه على Google.

لإجراء ربط الحساب باستخدام OAuth وتسجيل الدخول بحساب Google، يُرجى اتّباع الخطوات العامة التالية:

  1. أولاً، اطلب من المستخدم منح الموافقة للوصول إلى ملفه الشخصي على Google.
  2. استخدِم المعلومات الواردة في ملفه الشخصي للتحقق من وجود حساب المستخدم.
  3. بالنسبة إلى المستخدمين الحاليين، اربط الحسابات.
  4. إذا لم تتمكّن من العثور على تطابق لمستخدم Google في نظام المصادقة، تحقّق من الرمز المميّز للمعرّف الذي تلقّيته من Google. يمكنك بعد ذلك إنشاء مستخدم استنادًا إلى معلومات الملف الشخصي الموجودة في الرمز المميز للمعرّف.
يوضح هذا الرقم خطوات المستخدم لربط حسابه على Google باستخدام مسار الربط السلس. تعرض لقطة الشاشة الأولى كيف يمكن للمستخدم اختيار تطبيقك للربط. تتيح لقطة الشاشة الثانية للمستخدم تأكيد ما إذا كان لديه حساب حالي في خدمتك. تتيح لقطة الشاشة الثالثة للمستخدم اختيار حساب Google الذي يريد الربط به. تعرض لقطة الشاشة الرابعة تأكيدًا لربط حساب Google بتطبيقك. تعرض لقطة الشاشة الخامسة حساب مستخدم تم ربطه بنجاح في تطبيق Google.

الشكل 1. ربط الحسابات على هاتف المستخدم من خلال الربط السلس

متطلبات الربط السلس

  • نفِّذ تدفق ربط OAuth الأساسي. يجب أن تتوافق خدمتك مع نقاط نهاية التفويض والتبادل المميز المتوافقة مع OAuth 2.0.
  • يجب أن تتوافق نقطة نهاية تبادل الرموز المميّزة مع عمليات تأكيد JSON Web Token (JWT) وتنفيذ عناصر check وcreate وget.

تنفيذ خادم OAuth

يجب أن تتوافق نقطة نهاية تبادل الرموز المميّزة مع أهداف check وcreate وget. يوضّح ما يلي الخطوات المكتملة من خلال مسار ربط الحساب، ويشير إلى الحالات التي يتم فيها استدعاء الأغراض المختلفة:

  1. هل يمتلك المستخدم حسابًا في نظام المصادقة؟ (يحدد المستخدم عن طريق اختيار "نعم" أو "لا")
    1. نعم : هل يستخدم المستخدم البريد الإلكتروني المرتبط بحسابه على Google لتسجيل الدخول إلى المنصة؟ (يحدد المستخدم عن طريق اختيار "نعم" أو "لا")
      1. نعم : هل يمتلك المستخدم حسابًا مطابقًا في نظام المصادقة؟ (تم طلب check intent للتأكيد)
        1. نعم: يتم طلب get intent ويتم ربط الحساب في حال نجاح عمليات الإرجاع.
        2. لا : هل تريد إنشاء حساب جديد؟ (يحدد المستخدم عن طريق اختيار "نعم" أو "لا")
          1. نعم: يتم طلب create intent ويتم ربط الحساب إذا تم إنشاء طلب إعادة السلع بنجاح.
          2. لا : يتم تشغيل تدفق OAuth على الويب ويتم توجيه المستخدم إلى المتصفح التابع له، ويتم منح المستخدم خيار الربط بعنوان بريد إلكتروني مختلف.
      2. لا : يتم تشغيل تدفق OAuth على الويب، ويتم توجيه المستخدم إلى المتصفح التابع له، ويُمنح المستخدم خيار الربط بعنوان بريد إلكتروني مختلف.
    2. لا : هل يمتلك المستخدم حسابًا مطابقًا في نظام المصادقة؟ (تم طلب check intent للتأكيد)
      1. نعم : يتم طلب get intent ويتم ربط الحساب في حال نجاح عمليات الإرجاع.
      2. لا: create intent يتم ربطها ويتم ربط الحساب في حال إنشاء عمليات إرجاع نية الشراء بنجاح.

Check for an existing user account (check intent)

After the user gives 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 responds with account_found=true. If the Google account doesn't match an existing user, your token exchange endpoint returns an HTTP 404 Not Found error with account_found=false.

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=check&assertion=JWT&scope=SCOPES&client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET

Your token exchange endpoint must be able to handle the following parameters:

Token endpoint parameters
intent For these requests, the value of this parameter is check.
grant_type The type of token being exchanged. For these requests, this parameter has the value urn:ietf:params:oauth:grant-type:jwt-bearer.
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.
client_id The client ID you assigned to Google.
client_secret The client secret you assigned to Google.

To respond to the check intent requests, your token exchange endpoint must perform the following steps:

  • Validate and decode the JWT assertion.
  • Check if the Google account is already present in your authentication system.
تحقق من صحة تأكيد JWT وفك تشفيره

يمكنك التحقق من صحة وفك تشفير تأكيد JWT باستخدام مكتبة فك تشفير JWT للغتك . استخدم مفاتيح Google العامة ، المتوفرة بتنسيقات JWK أو PEM ، للتحقق من توقيع الرمز المميز.

عند فك التشفير ، يبدو تأكيد JWT مثل المثال التالي:

{
  "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
  "email_verified": true,   // true, if Google has verified the email address
  "hd": "example.com",      // If present, the host domain of the user's GSuite email address
                            // If present, a URL to user's profile picture
  "picture": "https://lh3.googleusercontent.com/a-/AOh14GjlTnZKHAeb94A-FmEbwZv7uJD986VOF1mJGb2YYQ",
  "locale": "en_US"         // User's locale, from browser or phone settings
}

بالإضافة إلى التحقق من توقيع الرمز المميز، تحقق من المصدر التأكيد على ( iss الميدان) هو https://accounts.google.com ، أن الجمهور ( aud الميدان) هو معرف العميل المعين، وهذا الرمز المميز لم تنته ( exp مجال).

باستخدام حقول email و email_verified و hd ، يمكنك تحديد ما إذا كانت Google تستضيف email_verified لعنوان بريد إلكتروني. في الحالات التي تكون فيها Google مخولة ، من المعروف حاليًا أن المستخدم هو المالك الشرعي للحساب ويمكنك تخطي كلمة المرور أو طرق التحقق الأخرى. بخلاف ذلك ، يمكن استخدام هذه الطرق للتحقق من الحساب قبل الربط.

الحالات التي يكون فيها Google موثوقًا:

  • يحتوي email على لاحقة @gmail.com ، وهذا حساب Gmail.
  • email_verified صحة email_verified وتم تعيين hd ، هذا حساب G Suite.

يمكن للمستخدمين التسجيل في حسابات Google دون استخدام Gmail أو G Suite. عندما لا يحتوي email على لاحقة @gmail.com ويكون hd غائبًا ، لا يكون Google موثوقًا ويوصى باستخدام كلمة المرور أو طرق التحدي الأخرى للتحقق من المستخدم. يمكن أيضًا أن يكون email_verfied صحيحًا نظرًا لأن Google قد تحققت في البداية من المستخدم عند إنشاء حساب Google ، ولكن ربما تغيرت ملكية حساب البريد الإلكتروني للجهة الخارجية.

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 sub field, 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. In that case, return a response like the following:

HTTP/1.1 200 Success
Content-Type: application/json;charset=UTF-8

{
  "account_found":"true",
}

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 needs to reply with a HTTP 404 error that specifies "account_found": "false", as in the following example:

HTTP/1.1 404 Not found
Content-Type: application/json;charset=UTF-8

{
  "account_found":"false",
}

Handle automatic linking (get intent)

After the user gives 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 linking_error error and optional login_hint.

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&scope=SCOPES&client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET

Your token exchange endpoint must be able to handle the following parameters:

Token endpoint parameters
intent For these requests, the value of this parameter is get.
grant_type The type of token being exchanged. For these requests, this parameter has the value urn:ietf:params:oauth:grant-type:jwt-bearer.
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.
scope Optional: Any scopes that you've configured Google to request from users.
client_id The client ID you assigned to Google.
client_secret The client secret you assigned to Google.

To respond to the get intent requests, your token exchange endpoint must perform the following steps:

  • Validate and decode the JWT assertion.
  • Check if the Google account is already present in your authentication system.
تحقق من صحة تأكيد JWT وفك تشفيره

يمكنك التحقق من صحة وفك تشفير تأكيد JWT باستخدام مكتبة فك تشفير JWT للغتك . استخدم مفاتيح Google العامة ، المتوفرة بتنسيقات JWK أو PEM ، للتحقق من توقيع الرمز المميز.

عند فك التشفير ، يبدو تأكيد JWT مثل المثال التالي:

{
  "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
  "email_verified": true,   // true, if Google has verified the email address
  "hd": "example.com",      // If present, the host domain of the user's GSuite email address
                            // If present, a URL to user's profile picture
  "picture": "https://lh3.googleusercontent.com/a-/AOh14GjlTnZKHAeb94A-FmEbwZv7uJD986VOF1mJGb2YYQ",
  "locale": "en_US"         // User's locale, from browser or phone settings
}

بالإضافة إلى التحقق من توقيع الرمز المميز، تحقق من المصدر التأكيد على ( iss الميدان) هو https://accounts.google.com ، أن الجمهور ( aud الميدان) هو معرف العميل المعين، وهذا الرمز المميز لم تنته ( exp مجال).

باستخدام حقول email و email_verified و hd ، يمكنك تحديد ما إذا كانت Google تستضيف email_verified لعنوان بريد إلكتروني. في الحالات التي تكون فيها Google مخولة ، من المعروف حاليًا أن المستخدم هو المالك الشرعي للحساب ويمكنك تخطي كلمة المرور أو طرق التحقق الأخرى. بخلاف ذلك ، يمكن استخدام هذه الطرق للتحقق من الحساب قبل الربط.

الحالات التي يكون فيها Google موثوقًا:

  • يحتوي email على لاحقة @gmail.com ، وهذا حساب Gmail.
  • email_verified صحة email_verified وتم تعيين hd ، هذا حساب G Suite.

يمكن للمستخدمين التسجيل في حسابات Google دون استخدام Gmail أو G Suite. عندما لا يحتوي email على لاحقة @gmail.com ويكون hd غائبًا ، لا يكون Google موثوقًا ويوصى باستخدام كلمة المرور أو طرق التحدي الأخرى للتحقق من المستخدم. يمكن أيضًا أن يكون email_verfied صحيحًا نظرًا لأن Google قد تحققت في البداية من المستخدم عند إنشاء حساب Google ، ولكن ربما تغيرت ملكية حساب البريد الإلكتروني للجهة الخارجية.

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 sub field, is in your user database.
  • The email address in the assertion matches a user in your user database.

If an account is found for the user, 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",

  "refresh_token": "REFRESH_TOKEN",

  "expires_in": SECONDS_TO_EXPIRATION
}

In some cases, account linking based on ID token might fail for the user. If it does so for any reason, your token exchange endpoint needs to reply with a HTTP 401 error that specifies error=linking_error, as the following example shows:

HTTP/1.1 401 Unauthorized
Content-Type: application/json;charset=UTF-8

{
  "error":"linking_error",
  "login_hint":"foo@bar.com"
}

When Google receives a 401 error response with linking_error, Google sends the user to your authorization endpoint with login_hint as a parameter. The user completes account linking using the OAuth linking flow in their browser.

Handle account creation via Google Sign-In (create intent)

When a user needs to create an account on your service, Google makes a request to your token exchange endpoint that specifies intent=create.

The request has the following form:

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&assertion=JWT&client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET

Your token exchange endpoint must able to handle the following parameters:

Token endpoint parameters
intent For these requests, the value of this parameter is create.
grant_type The type of token being exchanged. For these requests, this parameter has the value urn:ietf:params:oauth:grant-type:jwt-bearer.
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.
client_id The client ID you assigned to Google.
client_secret The client secret you assigned to Google.

The JWT within the assertion parameter contains 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 the create intent requests, your token exchange endpoint must perform the following steps:

  • Validate and decode the JWT assertion.
  • Validate user information and create new account.
تحقق من صحة تأكيد JWT وفك تشفيره

يمكنك التحقق من صحة وفك تشفير تأكيد JWT باستخدام مكتبة فك تشفير JWT للغتك . استخدم مفاتيح Google العامة ، المتوفرة بتنسيقات JWK أو PEM ، للتحقق من توقيع الرمز المميز.

عند فك التشفير ، يبدو تأكيد JWT مثل المثال التالي:

{
  "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
  "email_verified": true,   // true, if Google has verified the email address
  "hd": "example.com",      // If present, the host domain of the user's GSuite email address
                            // If present, a URL to user's profile picture
  "picture": "https://lh3.googleusercontent.com/a-/AOh14GjlTnZKHAeb94A-FmEbwZv7uJD986VOF1mJGb2YYQ",
  "locale": "en_US"         // User's locale, from browser or phone settings
}

بالإضافة إلى التحقق من توقيع الرمز المميز، تحقق من المصدر التأكيد على ( iss الميدان) هو https://accounts.google.com ، أن الجمهور ( aud الميدان) هو معرف العميل المعين، وهذا الرمز المميز لم تنته ( exp مجال).

باستخدام حقول email و email_verified و hd ، يمكنك تحديد ما إذا كانت Google تستضيف email_verified لعنوان بريد إلكتروني. في الحالات التي تكون فيها Google مخولة ، من المعروف حاليًا أن المستخدم هو المالك الشرعي للحساب ويمكنك تخطي كلمة المرور أو طرق التحقق الأخرى. بخلاف ذلك ، يمكن استخدام هذه الطرق للتحقق من الحساب قبل الربط.

الحالات التي يكون فيها Google موثوقًا:

  • يحتوي email على لاحقة @gmail.com ، وهذا حساب Gmail.
  • email_verified صحة email_verified وتم تعيين hd ، هذا حساب G Suite.

يمكن للمستخدمين التسجيل في حسابات Google دون استخدام Gmail أو G Suite. عندما لا يحتوي email على لاحقة @gmail.com ويكون hd غائبًا ، لا يكون Google موثوقًا ويوصى باستخدام كلمة المرور أو طرق التحدي الأخرى للتحقق من المستخدم. يمكن أيضًا أن يكون email_verfied صحيحًا نظرًا لأن Google قد تحققت في البداية من المستخدم عند إنشاء حساب Google ، ولكن ربما تغيرت ملكية حساب البريد الإلكتروني للجهة الخارجية.

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 sub field, 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. To do so, respond to the request with an HTTP 401 error that specifies error=linking_error and gives the user's email address as the login_hint. The following is a sample response:

HTTP/1.1 401 Unauthorized
Content-Type: application/json;charset=UTF-8

{
  "error":"linking_error",
  "login_hint":"foo@bar.com"
}

When Google receives a 401 error response with linking_error, Google sends the user to your authorization endpoint with login_hint as a parameter. The user completes account linking using the OAuth linking flow in their browser.

If neither condition is true, create a new user account with the information provided in the JWT. New accounts don't typically have a password set. It's recommended that you add Google Sign-In to other platforms to enable users to log in with 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 to sign in on other platforms.

When the creation is completed, issue an access token and refresh 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",

  "refresh_token": "REFRESH_TOKEN",

  "expires_in": SECONDS_TO_EXPIRATION
}

الحصول على معرِّف عميل Google API

سيُطلب منك تقديم معرّف عميل Google API أثناء عملية تسجيل ربط الحساب.

للحصول على معرِّف عميل واجهة برمجة التطبيقات باستخدام المشروع الذي أنشأته أثناء إكمال خطوات ربط OAuth. ولإجراء ذلك، أكمِل الخطوات التالية:

  1. افتح صفحة بيانات الاعتماد في وحدة تحكّم Google API.
  2. أنشئ مشروع Google APIs أو اختَره.

    إذا لم يكن مشروعك يحتوي على معرِّف عميل لنوع تطبيق الويب، انقر على إنشاء بيانات اعتماد &gt؛ معرِّف عميل بروتوكول OAuth لإنشاء معرِّف. وتأكّد من تضمين نطاق موقعك الإلكتروني في المربّع مصادر JavaScript المعتمَدة. عند إجراء اختبارات أو تطوير محلي، عليك إضافة كل من http://localhost وhttp://localhost:<port_number> إلى حقل مصادر JavaScript المعتمَدة.

التحقّق من صحة عملية التنفيذ

يمكنك التحقق من صحة التطبيق الخاص بك باستخدام ملعب أوث 2.0 الأداة.

في الأداة ، قم بالخطوات التالية:

  1. انقر فوق تكوين لفتح نافذة تكوين أوث 2.0.
  2. في مجال تدفق أوث، اختر من جانب العميل.
  3. في مجال أوث النهايات، حدد مخصص.
  4. حدد نقطة نهاية OAuth 2.0 ومعرف العميل الذي عينته لـ Google في الحقول المقابلة.
  5. في القسم الخطوة 1، لا تحدد أي نطاقات جوجل. بدلاً من ذلك ، اترك هذا الحقل فارغًا أو اكتب نطاقًا صالحًا لخادمك (أو سلسلة عشوائية إذا كنت لا تستخدم نطاقات OAuth). عند الانتهاء من ذلك، انقر فوق تخويل واجهات برمجة التطبيقات.
  6. في الأقسام الخطوة 2 و الخطوة 3، انتقل من خلال تدفق أوث 2.0 والتحقق من أن كل خطوة تعمل على النحو المنشود.

يمكنك التحقق من صحة التطبيق الخاص بك باستخدام حساب Google ربط تجريبي الأداة.

في الأداة ، قم بالخطوات التالية:

  1. انقر على تسجيل الدخول باستخدام زر جوجل.
  2. اختر الحساب الذي ترغب في ربطه.
  3. أدخل معرف الخدمة.
  4. اختياريًا ، أدخل نطاقًا واحدًا أو أكثر ستطلب الوصول إليه.
  5. انقر فوق ابدأ تجريبي.
  6. عند المطالبة ، أكد أنه يمكنك الموافقة ورفض طلب الربط.
  7. تأكد من إعادة توجيهك إلى النظام الأساسي الخاص بك.