پیوند حساب 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 در دسترس قرار می‌دهد. نقطه پایانی اول، نقطه پایانی مجوز است که مسئول یافتن یا اخذ رضایت از کاربران برای دسترسی به داده‌ها است. نقطه پایانی مجوز، یک رابط کاربری ورود به سیستم را به کاربرانی که هنوز وارد سیستم نشده‌اند ارائه می‌دهد و رضایت آنها را برای دسترسی درخواستی ثبت می‌کند. نقطه پایانی دوم، نقطه پایانی تبادل توکن است که برای دریافت رشته‌های رمزگذاری شده، به نام توکن‌ها، که به کاربر اجازه دسترسی به سرویس شما را می‌دهند، استفاده می‌شود.

وقتی یک برنامه گوگل نیاز به فراخوانی یکی از APIهای سرویس شما دارد، گوگل از این نقاط پایانی به صورت مشترک استفاده می‌کند تا از کاربران شما اجازه فراخوانی این APIها را از طرف آنها دریافت کند.

لینک کردن حساب گوگل: جریان کد مجوز OAuth

نمودار توالی زیر، جزئیات تعاملات بین کاربر، گوگل و نقاط پایانی سرویس شما را نشان می‌دهد.

کاربر اپلیکیشن گوگل / مرورگر سرور گوگل مجوز شما نقطه پایانی توکن شما نقطه پایانی ۱. کاربر پیوند را آغاز می‌کند ۲. ریدایرکت به Auth Endpoint (GET) client_id، redirect_uri، وضعیت، دامنه ۳. نمایش صفحه ورود و رضایت‌نامه 4. User Authenticates & Grants Consent ۵. ریدایرکت مجدد به گوگل (GET) کد، ایالت ۶. مدیریت تغییر مسیر و کد عبور/وضعیت ۷. صرافی توکن (POST) grant_type=کد_مجوز، کد ۸. توکن‌های برگشتی (۲۰۰ تایی) توکن_دسترسی، توکن_رفرش ۹. ذخیره توکن‌های کاربر ۱۰. دسترسی به منابع کاربر
Figure 1. The sequence of events in the OAuth 2.0 Authorization Code flow for Google Account Linking.

نقش‌ها و مسئولیت‌ها

جدول زیر نقش‌ها و مسئولیت‌های بازیگران در جریان OAuth اتصال حساب گوگل (GAL) را تعریف می‌کند. توجه داشته باشید که در GAL، گوگل به عنوان کلاینت OAuth عمل می‌کند، در حالی که سرویس شما به عنوان ارائه‌دهنده هویت/سرویس عمل می‌کند.

بازیگر / جزء نقش GAL مسئولیت‌ها
برنامه/سرور گوگل کلاینت OAuth جریان را آغاز می‌کند، کد مجوز را دریافت می‌کند، آن را با توکن‌ها مبادله می‌کند و آنها را به طور ایمن ذخیره می‌کند تا به APIهای سرویس شما دسترسی داشته باشد.
نقطه پایانی مجوز شما سرور احراز هویت کاربران شما را احراز هویت می‌کند و رضایت آنها را برای اشتراک‌گذاری دسترسی به داده‌هایشان با گوگل دریافت می‌کند.
نقطه پایانی تبادل توکن شما Authorization Server کدهای مجوز و توکن‌های به‌روزرسانی را اعتبارسنجی می‌کند و توکن‌های دسترسی را به سرور گوگل صادر می‌کند.
آدرس اینترنتی ریدایرکت گوگل نقطه پایانی پاسخ به تماس تغییر مسیر کاربر را از سرویس احراز هویت شما به همراه code و مقادیر state دریافت می‌کند.

یک جلسه جریان کد مجوز OAuth 2.0 که توسط گوگل آغاز شده است، جریان زیر را دارد:

  1. گوگل نقطه پایانی احراز هویت شما را در مرورگر کاربر باز می‌کند. اگر جریان برای یک اقدام در دستگاهی که فقط با صدا کار می‌کند شروع شده باشد، گوگل اجرا را به تلفن منتقل می‌کند.
  2. کاربر، اگر قبلاً وارد سیستم نشده باشد، وارد سیستم می‌شود و اگر قبلاً اجازه نداده باشد، به گوگل اجازه می‌دهد تا به داده‌هایش با API شما دسترسی پیدا کند.
  3. سرویس شما یک کد مجوز ایجاد می‌کند و آن را به گوگل برمی‌گرداند. برای انجام این کار، مرورگر کاربر را به همراه کد مجوز پیوست شده به درخواست، به گوگل هدایت کنید.
  4. گوگل کد مجوز را به نقطه پایانی تبادل توکن شما ارسال می‌کند، که صحت کد را تأیید می‌کند و یک توکن دسترسی و یک توکن به‌روزرسانی را برمی‌گرداند. توکن دسترسی یک توکن کوتاه‌مدت است که سرویس شما آن را به عنوان اعتبارنامه برای دسترسی به APIها می‌پذیرد. توکن به‌روزرسانی یک توکن بلندمدت است که گوگل می‌تواند آن را ذخیره کند و از آن برای به دست آوردن توکن‌های دسترسی جدید پس از انقضا استفاده کند.
  5. پس از اینکه کاربر فرآیند اتصال حساب کاربری را تکمیل کرد، هر درخواست بعدی که از گوگل ارسال می‌شود حاوی یک توکن دسترسی است.

دستور العمل اجرا

برای پیاده‌سازی جریان کد مجوز، این مراحل را دنبال کنید.

مرحله ۱: رسیدگی به درخواست‌های مجوز

وقتی گوگل اتصال حساب را آغاز می‌کند، کاربر را به نقطه پایانی مجوز شما هدایت می‌کند. برای جزئیات قراردادهای پروتکل و الزامات پارامتر، به نقطه پایانی مجوز مراجعه کنید.

برای مدیریت درخواست، اقدامات زیر را انجام دهید:

  1. اعتبارسنجی درخواست :

    • تأیید کنید که client_id با Client ID اختصاص داده شده به گوگل مطابقت دارد.
    • تأیید کنید که redirect_uri با آدرس اینترنتی ریدایرکت مورد انتظار گوگل مطابقت دارد: 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. ریدایرکت مجدد به گوگل :

    • مرورگر را به URL ارائه شده در redirect_uri هدایت کنید.
    • پارامترهای پرس و جوی زیر را اضافه کنید:
      • code : کد مجوزی که ایجاد کرده‌اید.
      • state : مقدار وضعیت اصلاح نشده‌ای که از گوگل دریافت شده است.

مرحله ۲: رسیدگی به درخواست‌های تبادل توکن

نقطه پایانی تبادل توکن شما دو نوع درخواست را پردازش می‌کند: تبادل کدها با توکن‌ها، و به‌روزرسانی توکن‌های دسترسی منقضی شده. برای جزئیات قراردادهای پروتکل و الزامات پارامتر، به نقطه پایانی تبادل توکن مراجعه کنید.

الف. تبادل کدهای مجوز با توکن‌ها

وقتی گوگل کد مجوز را دریافت می‌کند، نقطه پایانی تبادل توکن شما (POST) را برای بازیابی توکن‌ها فراخوانی می‌کند.

  1. اعتبارسنجی درخواست :

    • client_id و client_secret را تأیید کنید.
    • بررسی کنید که کد مجوز معتبر است و منقضی نشده است.
    • تأیید کنید که redirect_uri با مقدار استفاده شده در مرحله ۱ مطابقت دارد.
    • اگر اعتبارسنجی با شکست مواجه شد، یک 400 Bad Request با {"error": "invalid_grant"} برگردانید.
  2. توکن‌های صدور :

    • یک refresh_token با طول عمر بالا و یک access_token با طول عمر کوتاه (معمولاً ۱ ساعت) تولید کنید.
    • یک HTTP 200 OK با پاسخ استاندارد توکن JSON برگردانید.

ب. به‌روزرسانی توکن‌های دسترسی

وقتی توکن دسترسی منقضی می‌شود، گوگل با استفاده از توکن رفرش، درخواست جدیدی می‌دهد.

  1. اعتبارسنجی درخواست :

    • شناسه client_id ، client_secret و refresh_token را تأیید کنید.
    • اگر اعتبارسنجی با شکست مواجه شد، یک 400 Bad Request با {"error": "invalid_grant"} برگردانید.
  2. صدور توکن دسترسی جدید :

    • Generate a new short-lived access_token .
    • یک 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.

اعتبارسنجی پیاده‌سازی شما

You can validate your implementation by using the OAuth 2.0 Playground tool.

In the tool, do the following steps:

  1. Click Configuration to open the OAuth 2.0 Configuration window.
  2. In the OAuth flow field, select Client-side.
  3. In the OAuth Endpoints field, select Custom.
  4. Specify your OAuth 2.0 endpoint and the client ID you assigned to Google in the corresponding fields.
  5. In the Step 1 section, don't select any Google scopes. Instead, leave this field blank or type a scope valid for your server (or an arbitrary string if you don't use OAuth scopes). When you're done, click Authorize APIs.
  6. In the Step 2 and Step 3 sections, go through the OAuth 2.0 flow and verify that each step works as intended.

You can validate your implementation by using the Google Account Linking Demo tool.

In the tool, do the following steps:

  1. Click the Sign in with Google button.
  2. Choose the account you'd like to link.
  3. Enter the service ID.
  4. Optionally enter one or more scopes that you will request access for.
  5. Click Start Demo.
  6. When prompted, confirm that you may consent and deny the linking request.
  7. Confirm that you are redirected to your platform.