حسابها با استفاده از جریان کد مجوز استاندارد صنعتی 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:
- Go to the Google API Console.
- Click Create project.
- Enter a name or accept the generated suggestion.
- Confirm or edit any remaining fields.
- Click Create.
To view your project ID:
- Go to the Google API Console.
- Find your project in the table on the landing page. The project ID appears in the ID column.
Configure your OAuth Consent Screen
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.
- Open the OAuth consent screen page of the Google APIs console.
- If prompted, select the project you just created.
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
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
نمودار توالی زیر، جزئیات تعاملات بین کاربر، گوگل و نقاط پایانی سرویس شما را نشان میدهد.
نقشها و مسئولیتها
جدول زیر نقشها و مسئولیتهای بازیگران در جریان OAuth اتصال حساب گوگل (GAL) را تعریف میکند. توجه داشته باشید که در GAL، گوگل به عنوان کلاینت OAuth عمل میکند، در حالی که سرویس شما به عنوان ارائهدهنده هویت/سرویس عمل میکند.
| بازیگر / جزء | نقش GAL | مسئولیتها |
|---|---|---|
| برنامه/سرور گوگل | کلاینت OAuth | جریان را آغاز میکند، کد مجوز را دریافت میکند، آن را با توکنها مبادله میکند و آنها را به طور ایمن ذخیره میکند تا به APIهای سرویس شما دسترسی داشته باشد. |
| نقطه پایانی مجوز شما | سرور احراز هویت | کاربران شما را احراز هویت میکند و رضایت آنها را برای اشتراکگذاری دسترسی به دادههایشان با گوگل دریافت میکند. |
| نقطه پایانی تبادل توکن شما | Authorization Server | کدهای مجوز و توکنهای بهروزرسانی را اعتبارسنجی میکند و توکنهای دسترسی را به سرور گوگل صادر میکند. |
| آدرس اینترنتی ریدایرکت گوگل | نقطه پایانی پاسخ به تماس | تغییر مسیر کاربر را از سرویس احراز هویت شما به همراه code و مقادیر state دریافت میکند. |
یک جلسه جریان کد مجوز OAuth 2.0 که توسط گوگل آغاز شده است، جریان زیر را دارد:
- گوگل نقطه پایانی احراز هویت شما را در مرورگر کاربر باز میکند. اگر جریان برای یک اقدام در دستگاهی که فقط با صدا کار میکند شروع شده باشد، گوگل اجرا را به تلفن منتقل میکند.
- کاربر، اگر قبلاً وارد سیستم نشده باشد، وارد سیستم میشود و اگر قبلاً اجازه نداده باشد، به گوگل اجازه میدهد تا به دادههایش با API شما دسترسی پیدا کند.
- سرویس شما یک کد مجوز ایجاد میکند و آن را به گوگل برمیگرداند. برای انجام این کار، مرورگر کاربر را به همراه کد مجوز پیوست شده به درخواست، به گوگل هدایت کنید.
- گوگل کد مجوز را به نقطه پایانی تبادل توکن شما ارسال میکند، که صحت کد را تأیید میکند و یک توکن دسترسی و یک توکن بهروزرسانی را برمیگرداند. توکن دسترسی یک توکن کوتاهمدت است که سرویس شما آن را به عنوان اعتبارنامه برای دسترسی به APIها میپذیرد. توکن بهروزرسانی یک توکن بلندمدت است که گوگل میتواند آن را ذخیره کند و از آن برای به دست آوردن توکنهای دسترسی جدید پس از انقضا استفاده کند.
- پس از اینکه کاربر فرآیند اتصال حساب کاربری را تکمیل کرد، هر درخواست بعدی که از گوگل ارسال میشود حاوی یک توکن دسترسی است.
دستور العمل اجرا
برای پیادهسازی جریان کد مجوز، این مراحل را دنبال کنید.
مرحله ۱: رسیدگی به درخواستهای مجوز
وقتی گوگل اتصال حساب را آغاز میکند، کاربر را به نقطه پایانی مجوز شما هدایت میکند. برای جزئیات قراردادهای پروتکل و الزامات پارامتر، به نقطه پایانی مجوز مراجعه کنید.
برای مدیریت درخواست، اقدامات زیر را انجام دهید:
اعتبارسنجی درخواست :
- تأیید کنید که
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_typecodeاست.
- تأیید کنید که
احراز هویت کاربر :
- بررسی کنید که آیا کاربر به سرویس شما وارد شده است یا خیر.
- اگر کاربر وارد سیستم نشده است، از او بخواهید مراحل ورود یا ثبتنام شما را تکمیل کند.
تولید کد مجوز :
- یک کد مجوز منحصر به فرد و غیرقابل حدس مرتبط با کاربر و کلاینت ایجاد کنید.
- کد را طوری تنظیم کنید که تقریباً 10 دقیقه دیگر منقضی شود.
ریدایرکت مجدد به گوگل :
- مرورگر را به URL ارائه شده در
redirect_uriهدایت کنید. - پارامترهای پرس و جوی زیر را اضافه کنید:
-
code: کد مجوزی که ایجاد کردهاید. -
state: مقدار وضعیت اصلاح نشدهای که از گوگل دریافت شده است.
-
- مرورگر را به URL ارائه شده در
مرحله ۲: رسیدگی به درخواستهای تبادل توکن
نقطه پایانی تبادل توکن شما دو نوع درخواست را پردازش میکند: تبادل کدها با توکنها، و بهروزرسانی توکنهای دسترسی منقضی شده. برای جزئیات قراردادهای پروتکل و الزامات پارامتر، به نقطه پایانی تبادل توکن مراجعه کنید.
الف. تبادل کدهای مجوز با توکنها
وقتی گوگل کد مجوز را دریافت میکند، نقطه پایانی تبادل توکن شما (POST) را برای بازیابی توکنها فراخوانی میکند.
اعتبارسنجی درخواست :
-
client_idوclient_secretرا تأیید کنید. - بررسی کنید که کد مجوز معتبر است و منقضی نشده است.
- تأیید کنید که
redirect_uriبا مقدار استفاده شده در مرحله ۱ مطابقت دارد. - اگر اعتبارسنجی با شکست مواجه شد، یک
400 Bad Requestبا{"error": "invalid_grant"}برگردانید.
-
توکنهای صدور :
- یک
refresh_tokenبا طول عمر بالا و یکaccess_tokenبا طول عمر کوتاه (معمولاً ۱ ساعت) تولید کنید. - یک HTTP
200 OKبا پاسخ استاندارد توکن JSON برگردانید.
- یک
ب. بهروزرسانی توکنهای دسترسی
وقتی توکن دسترسی منقضی میشود، گوگل با استفاده از توکن رفرش، درخواست جدیدی میدهد.
اعتبارسنجی درخواست :
- شناسه
client_id،client_secretوrefresh_tokenرا تأیید کنید. - اگر اعتبارسنجی با شکست مواجه شد، یک
400 Bad Requestبا{"error": "invalid_grant"}برگردانید.
- شناسه
صدور توکن دسترسی جدید :
- Generate a new short-lived
access_token. - یک
200 OKبه همراه پاسخ توکن JSON (به صورت اختیاری شامل یک توکن بهروزرسانی جدید) برگردانید.
- Generate a new short-lived
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:
- Linked Account Sign-In with Google One Tap.
- Frictionless subscription on AndroidTV.
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:
- Extract access token from the Authorization header and return information for the user associated with the access token.
- If the access token is invalid, return an HTTP 401 Unauthorized error with using the
WWW-AuthenticateResponse Header. Below is an example of a userinfo error response: 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.HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="The Access Token expired"
If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:
If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.{ "sub": "USER_UUID", "email": "EMAIL_ADDRESS", "given_name": "FIRST_NAME", "family_name": "LAST_NAME", "name": "FULL_NAME", "picture": "PROFILE_PICTURE", }userinfo endpoint response subA unique ID that identifies the user in your system. emailEmail address of the user. given_nameOptional: First name of the user. family_nameOptional: Last name of the user. nameOptional: Full name of the user. pictureOptional: 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:
- Click Configuration to open the OAuth 2.0 Configuration window.
- In the OAuth flow field, select Client-side.
- In the OAuth Endpoints field, select Custom.
- Specify your OAuth 2.0 endpoint and the client ID you assigned to Google in the corresponding fields.
- 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.
- 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:
- Click the Sign in with Google button.
- Choose the account you'd like to link.
- Enter the service ID.
- Optionally enter one or more scopes that you will request access for.
- Click Start Demo.
- When prompted, confirm that you may consent and deny the linking request.
- Confirm that you are redirected to your platform.