ربط حساب Google باستخدام OAuth

يتم ربط الحسابات باستخدام مسار رمز التفويض المتوافق مع معيار OAuth 2.0 المتّبع في المجال.

بروتوكول OAuth 2.1 وPKCE للوكلاء

بالنسبة إلى وكلاء الذكاء الاصطناعي غير الاحتفاظي الحالة ومسارات العرض المتعدّد، يُنصح بفرض استخدام OAuth 2.1.

  • مفتاح الحماية لتبادل الرموز (PKCE): يجب استخدامه لتأمين مسار رمز التفويض، ما يمنع هجمات الاعتراض.
  • بدون تدفق ضمني: يعرض التدفق الضمني رموز الدخول في عنوان URL، ما يشكّل خطرًا أمنيًا على بيئات الوكيل.

يجب أن تتيح خدمتك نقاط نهاية الترخيص وتبادل الرموز المميزة المتوافقة مع OAuth 2.0/2.1.

Create the project

To create your project to use account linking:

  1. Go to the Google API Console.
  2. Click Create project.
  3. Enter a name or accept the generated suggestion.
  4. Confirm or edit any remaining fields.
  5. Click Create.

To view your project ID:

  1. Go to the Google API Console.
  2. Find your project in the table on the landing page. The project ID appears in the ID column.

The Google Account Linking process includes a consent screen which tells users the application requesting access to their data, what kind of data they are asking for and the terms that apply. You will need to configure your OAuth consent screen before generating a Google API client ID.

  1. Open the OAuth consent screen page of the Google APIs console.
  2. If prompted, select the project you just created.
  3. On the "OAuth consent screen" page, fill out the form and click the “Save” button.

    Application name: The name of the application asking for consent. The name should accurately reflect your application and be consistent with the application name users see elsewhere. The application name will be shown on the Account Linking consent screen.

    Application logo: An image on the consent screen that will help users recognize your app. The logo is shown on Account linking consent screen and on account settings

    Support email: For users to contact you with questions about their consent.

    Scopes for Google APIs: Scopes allow your application to access your user's private Google data. For the Google Account Linking use case, default scope (email, profile, openid) is sufficient, you don’t need to add any sensitive scopes. It is generally a best practice to request scopes incrementally, at the time access is required, rather than up front. Learn more.

    Authorized domains: To protect you and your users, Google only allows applications that authenticate using OAuth to use Authorized Domains. Your applications' links must be hosted on Authorized Domains. Learn more.

    Application Homepage link: Home page for your application. Must be hosted on an Authorized Domain.

    Application Privacy Policy link: Shown on Google Account Linking consent screen. Must be hosted on an Authorized Domain.

    Application Terms of Service link (Optional): Must be hosted on an Authorized Domain.

    Figure 1. Google Account Linking Consent Screen for a fictitious Application, Tunery

  4. Check "Verification Status", if your application needs verification then click the "Submit For Verification" button to submit your application for verification. Refer to OAuth verification requirements for details.

تنفيذ خادم OAuth

يتألف تنفيذ خادم OAuth 2.0 لمسار رمز التفويض من نقطتَي نهاية، يتيحهما خادمك من خلال بروتوكول HTTPS. نقطة النهاية الأولى هي نقطة نهاية التفويض، وهي مسؤولة عن العثور على موافقة المستخدمين أو الحصول عليها للوصول إلى البيانات. تعرض نقطة نهاية التفويض واجهة مستخدم لتسجيل الدخول للمستخدمين الذين لم يسبق لهم تسجيل الدخول، وتسجّل الموافقة على الوصول المطلوب. نقطة النهاية الثانية هي نقطة نهاية تبادل الرموز المميّزة، وتُستخدَم للحصول على سلاسل مشفّرة، تُعرف باسم الرموز المميّزة، التي تمنح المستخدم إذن الوصول إلى خدمتك.

عندما يحتاج أحد تطبيقات Google إلى طلب إحدى واجهات برمجة التطبيقات في خدمتك، تستخدم Google نقطتَي النهاية هاتَين معًا للحصول على إذن من المستخدمين لطلب واجهات برمجة التطبيقات هذه نيابةً عنهم.

ربط حساب Google: مسار رمز تفويض OAuth

يوضّح مخطط التسلسل التالي التفاعلات بين المستخدم وGoogle ونقطتَي نهاية خدمتك.

User Google App / Browser Google Server Your Auth Endpoint Your Token Endpoint 1. User initiates linking 2. Redirect to Auth Endpoint (GET) client_id, redirect_uri, state, scope 3. Display Sign-in & Consent Screen 4. User Authenticates & Grants Consent 5. Redirect back to Google (GET) code, state 6. Handle redirect & pass code/state 7. Token Exchange (POST) grant_type=authorization_code, code 8. Return Tokens (200 OK) access_token, refresh_token 9. Store user tokens 10. Access user resources
Figure 1. The sequence of events in the OAuth 2.0 مسار رمز التفويض for ربط حساب Google.

الأدوار والمسؤوليات

يحدّد الجدول التالي أدوار ومسؤوليات الجهات الفاعلة في مسار OAuth لربط حساب Google. يُرجى العِلم أنّه في عملية ربط حساب Google، تعمل Google بصفتها عميل OAuth، بينما تعمل خدمتك بصفتها مزوّد الهوية/الخدمة.

Actor / Component GAL Role Responsibilities
Google App / Server OAuth Client يبدأ المسار ويتلقّى رمز التفويض ويستبدله بـ رموز مميّزة ويخزّنها بشكل آمن للوصول إلى واجهات برمجة التطبيقات في خدمتك.
Your Authorization Endpoint Authorization Server يصادق على المستخدمين ويحصل على موافقتهم على مشاركة إذن الوصول إلى بياناتهم مع Google.
Your Token Exchange Endpoint Authorization Server يتحقّق من صحة رموز التفويض ورموز إعادة التحميل، ويصدر رموز الدخول إلى خادم Google.
Google Redirect URI Callback Endpoint يتلقّى عملية إعادة توجيه المستخدم من خدمة التفويض مع القيمتَين code وstate.

تتّبع جلسة مسار رمز تفويض OAuth 2.0 التي تبدأها Google المسار التالي:

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

وصفة التنفيذ

اتّبِع الخطوات التالية لتنفيذ مسار رمز التفويض.

الخطوة 1: معالجة طلبات التفويض

عندما تبدأ Google عملية ربط الحساب، تعيد توجيه المستخدم إلى نقطة نهاية التفويض. للحصول على تفاصيل حول عقود البروتوكول ومتطلبات المَعلمات، يُرجى الاطّلاع على نقطة نهاية التفويض.

لمعالجة الطلب، نفِّذ الإجراءات التالية:

  1. التحقّق من صحة الطلب:

    • تأكَّد من أنّ client_id يتطابق مع رقم تعريف العميل المخصّص لـ Google.
    • تأكَّد من أنّ redirect_uri يتطابق مع عنوان URL المتوقّع لإعادة التوجيه من Google: none https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
    • تأكَّد من أنّ response_type هو code.
  2. مصادقة المستخدم:

    • تحقَّق مما إذا كان المستخدم قد سجّل الدخول إلى خدمتك.
    • إذا لم يكن المستخدم قد سجّل الدخول، اطلب منه إكمال مسار تسجيل الدخول أو الاشتراك.
  3. إنشاء رمز تفويض:

    • أنشئ رمز تفويض فريدًا لا يمكن تخمينه مرتبطًا بالمستخدم والعميل.
    • اضبط الرمز على انتهاء الصلاحية بعد 10 دقائق تقريبًا.
  4. إعادة التوجيه إلى Google:

    • أعِد توجيه المتصفّح إلى عنوان URL المقدَّم في redirect_uri.
    • أضِف مَعلمات طلب البحث التالية:
      • code: رمز التفويض الذي أنشأته.
      • state: قيمة الحالة غير المعدَّلة التي تم تلقّيها من Google.

الخطوة 2: معالجة طلبات تبادل الرموز المميّزة

تعالج نقطة نهاية تبادل الرموز المميّزة نوعَين من الطلبات: تبادل الرموز المميّزة مقابل الرموز، وتجديد رموز الدخول المنتهية الصلاحية. للحصول على تفاصيل حول عقود البروتوكول ومتطلبات المَعلمات، يُرجى الاطّلاع على نقطة نهاية تبادل الرموز المميّزة.

أ. تبادل رموز التفويض مقابل الرموز المميّزة

عندما تتلقّى Google رمز التفويض، تطلب نقطة نهاية تبادل الرموز المميّزة (POST) لاسترداد الرموز المميّزة.

  1. التحقّق من صحة الطلب:

    • تحقَّق من client_id وclient_secret.
    • تأكَّد من أنّ رمز التفويض صالح ولم تنتهِ صلاحيته.
    • تأكَّد من أنّ redirect_uri يتطابق مع القيمة المستخدَمة في الخطوة 1.
    • إذا تعذّر التحقّق من الصحة، أعِد رمز HTTP 400 Bad Request مع {"error": "invalid_grant"}.
  2. إصدار الرموز المميّزة:

    • أنشئ refresh_token طويل الأمد وaccess_token قصير الأمد (عادةً ساعة واحدة).
    • أعِد رمز HTTP 200 OK مع استجابة الرمز المميّز بتنسيق JSON القياسي.

ب. تجديد رموز الدخول

عند انتهاء صلاحية رمز الدخول، تطلب Google رمزًا جديدًا باستخدام رمز إعادة التحميل.

  1. التحقّق من صحة الطلب:

    • تحقَّق من client_id وclient_secret وrefresh_token.
    • إذا تعذّر التحقّق من الصحة، أعِد رمز HTTP 400 Bad Request مع {"error": "invalid_grant"}.
  2. إصدار رمز دخول جديد:

    • أنشئ access_token جديدًا قصير الأمد.
    • أعِد رمز HTTP 200 OK مع استجابة الرمز المميّز بتنسيق JSON (بما في ذلك الرمز المميز لإعادة التحميل الجديد اختياريًا).
Handle userinfo requests

The userinfo endpoint is an OAuth 2.0 protected resource that return claims about the linked user. Implementing and hosting the userinfo endpoint is optional, except for the following use cases:

After the access token has been successfully retrieved from your token endpoint, Google sends a request to your userinfo endpoint to retrieve basic profile information about the linked user.

userinfo endpoint request headers
Authorization header The access token of type Bearer.

For example, if your userinfo endpoint is available at https://myservice.example.com/userinfo, a request might look like the following:

GET /userinfo HTTP/1.1
Host: myservice.example.com
Authorization: Bearer ACCESS_TOKEN

For your userinfo endpoint to handle requests, do the following steps:

  1. Extract access token from the Authorization header and return information for the user associated with the access token.
  2. If the access token is invalid, return an HTTP 401 Unauthorized error with using the WWW-Authenticate Response Header. Below is an example of a userinfo error response:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    If a 401 Unauthorized, or any other unsuccessful error response is returned during the linking process, the error will be non-recoverable, the retrieved token will be discarded and the user will have to initiate the linking process again.
  3. If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.

    userinfo endpoint response
    sub A unique ID that identifies the user in your system.
    email Email address of the user.
    given_name Optional: First name of the user.
    family_name Optional: Last name of the user.
    name Optional: Full name of the user.
    picture Optional: Profile picture of the user.

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

يمكنك التحقّق من صحة التنفيذ باستخدام أداة مساحة بروتوكول OAuth 2.0.

في الأداة، اتّبِع الخطوات التالية:

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

يمكنك التحقّق من صحة عملية التنفيذ باستخدام أداة عرض توضيحي لربط حساب Google.

في الأداة، اتّبِع الخطوات التالية:

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