סקירה כללית
כניסה באמצעות חשבון Google מבוססת-OAuth עם קישור יעיל מוסיפה כניסה באמצעות חשבון Google על גבי קישור OAuth. הקישור הזה מאפשר למשתמשי Google ליהנות מחוויית קישור חלקה, וגם ליצור חשבון חדש בשירות שלכם באמצעות חשבון Google שלהם.
כדי לבצע קישור חשבונות באמצעות OAuth ו'כניסה באמצעות חשבון Google', פועלים לפי השלבים הכלליים הבאים:
- קודם כל, צריך לבקש מהמשתמש לתת הסכמה לגישה לפרופיל ב-Google.
- משתמשים במידע בפרופיל כדי לבדוק אם חשבון המשתמש קיים.
- למשתמשים קיימים, מקשרים את החשבונות.
- אם לא מצאתם התאמה למשתמש Google במערכת האימות שלכם, צריך לאמת את אסימון הזהות שהתקבל מ-Google. לאחר מכן תוכלו ליצור משתמש על סמך פרטי הפרופיל שכלולים באסימון המזהה.
איור 1. קישור חשבון בטלפון של משתמש באמצעות קישור פשוט
קישור יעיל: תהליך OAuth + כניסה באמצעות חשבון Google
תרשים הרצף הבא מציג את האינטראקציות בין המשתמש, Google ונקודת הקצה של חילופי הטוקנים שלכם לקישור פשוט.
תפקידים ותחומי אחריות
בטבלה הבאה מוגדרים התפקידים ותחומי האחריות של הגורמים בתהליך המקוצר של קישור החשבונות.
| המשתמש / הרכיב | תפקיד ב-GAL | תחומי אחריות |
|---|---|---|
| אפליקציית Google / שרת | לקוח OAuth | מקבל את הסכמת המשתמש לכניסה באמצעות חשבון Google, מעביר הצהרות זהות (JWT) לשרת שלכם ושומר בצורה מאובטחת את הטוקנים שנוצרו. |
| נקודת הקצה של חילופי הטוקנים | ספק זהויות / שרת הרשאות | מאמת את הצהרות הזהות, בודק אם יש חשבונות קיימים, מטפל בכוונות לקישור חשבונות (check, get, create) ומנפיק טוקנים על סמך הכוונות המבוקשות. |
| Your Service API | שרת משאבים | מאפשר גישה לנתוני משתמש כשמוצג אסימון גישה תקין. |
הדרישות לקישור פשוט
- מטמיעים את תהליך הקישור הבסיסי באמצעות OAuth. השירות שלכם צריך לתמוך בנקודות קצה של הרשאה והחלפת אסימונים שתואמות ל-OAuth 2.0.
- נקודת הקצה של החלפת האסימונים חייבת לתמוך בהצהרות של JSON Web Token (JWT) וליישם את הכוונות
check,createו-get.
לוגיקה של החלטות לקישור יעיל
הלוגיקה הבאה קובעת איך מתבצעות קריאות לכוונות במהלך תהליך הקישור הפשוט:
- האם למשתמש יש חשבון במערכת האימות שלכם? (המשתמש מחליט על ידי בחירה באפשרות 'כן' או 'לא')
- כן : המשתמש משתמש בכתובת האימייל שמשויכת לחשבון Google שלו כדי להיכנס לפלטפורמה שלכם. (המשתמש מחליט על ידי בחירה באפשרות 'כן' או 'לא')
- כן : האם למשתמש יש חשבון תואם במערכת האימות שלכם? (מתקשרים אל
check intentכדי לאשר)- YES : מתקשרים אל
get intentוהחשבון מקושר אם הכוונה מוחזרת בהצלחה. - לא : ליצור חשבון חדש? (המשתמש מחליט על ידי בחירה באפשרות 'כן' או 'לא')
- כן : מתבצעת קריאה אל
create intentוהחשבון מקושר אם כוונת היצירה מוחזרת בהצלחה. - לא : תהליך הקישור באמצעות OAuth מופעל, המשתמש מועבר לדפדפן שלו ומוצגת לו האפשרות לקשר עם כתובת אימייל אחרת.
- כן : מתבצעת קריאה אל
- YES : מתקשרים אל
- לא : מופעל תהליך הקישור באמצעות OAuth, המשתמש מועבר לדפדפן שלו ומוצגת לו האפשרות לקשר עם כתובת אימייל אחרת.
- כן : האם למשתמש יש חשבון תואם במערכת האימות שלכם? (מתקשרים אל
- לא : האם למשתמש יש חשבון תואם במערכת האימות שלך? (מתקשרים אל
check intentכדי לאשר)- YES : מתקשרים אל
get intentוהחשבון מקושר אם הכוונה מוחזרת בהצלחה. - לא : מתבצעת קריאה אל
create intentוהחשבון מקושר אם הכוונה ליצור חוזרת בהצלחה.
- YES : מתקשרים אל
- כן : המשתמש משתמש בכתובת האימייל שמשויכת לחשבון Google שלו כדי להיכנס לפלטפורמה שלכם. (המשתמש מחליט על ידי בחירה באפשרות 'כן' או 'לא')
מתכון להטמעה
נקודת הקצה של חילופי הטוקנים צריכה להטמיע את ה-Intents check, get ו-create כדי לתמוך בקישור פשוט.
כדי לטפל בכוונות השונות:
Check for an existing user account (check intent)
Google calls your token exchange endpoint to verify if the Google user exists in your system. For parameter details, see Streamlined Linking Intents.
Implementation Recipe
To handle the check intent, perform the following actions:
Validate the request:
- Verify
client_id,client_secret, andgrant_type(must beurn:ietf:params:oauth:grant-type:jwt-bearer). - Validate the
assertion(JWT) using the criteria in JWT Validation.
- Verify
Lookup user:
- Check if the Google Account ID (
sub) or email address in the JWT matches a user in your database.
- Check if the Google Account ID (
Respond:
- If found: Return HTTP
200 OKwith{"account_found": "true"}. - If not found: Return HTTP
404 Not Foundwith{"account_found": "false"}.
- If found: Return HTTP
Handle automatic linking (get intent)
If the account exists, Google calls your endpoint with intent=get to retrieve tokens. For parameter details, see Streamlined Linking Intents.
Implementation Recipe
To handle the get intent, perform the following actions:
Validate the request:
- Verify
client_id,client_secret, andgrant_type. - Validate the
assertion(JWT).
- Verify
Lookup user:
- Verify the user exists using the
suboremailclaim.
- Verify the user exists using the
Respond:
- If successful: Generate and return
access_token,refresh_token, andexpires_inin a JSON response (HTTP200 OK). - If linking fails: Return HTTP
401 Unauthorizedwith{"error": "linking_error"}and an optionallogin_hintto fall back to standard OAuth linking.
- If successful: Generate and return
Handle account creation using Sign in with Google (create intent)
If no account exists, Google calls your endpoint with intent=create to create a new user. For parameter details, see Streamlined Linking Intents.
Implementation Recipe
To handle the create intent, perform the following actions:
Validate the request:
- Verify
client_id,client_secret, andgrant_type. - Validate the
assertion(JWT).
- Verify
Verify user does not exist:
- Check if the
suboremailis already in your database. - If the user does exist: Return HTTP
401 Unauthorizedwith{"error": "linking_error", "login_hint": "USER_EMAIL"}to force fallback to OAuth linking.
- Check if the
Create account:
- Use the
sub,email,name, andpictureclaims from the JWT to create a new user record.
- Use the
Respond:
- Generate and return tokens in a JSON response (HTTP
200 OK).
- Generate and return tokens in a JSON response (HTTP
איך מקבלים את מזהה הלקוח ב-Google API
תצטרכו לספק את מזהה הלקוח של Google API במהלך תהליך ההרשמה לקישור החשבון. כדי לקבל את מזהה הלקוח של ה-API באמצעות הפרויקט שיצרתם במהלך השלבים של קישור OAuth. כדי לעשות זאת, מבצעים את השלבים הבאים:
- עוברים אל דף הלקוחות.
יוצרים פרויקט ב-Google APIs או בוחרים פרויקט קיים.
אם בפרויקט שלכם אין מזהה לקוח מסוג אפליקציית אינטרנט, לוחצים על יצירת לקוח כדי ליצור אחד. חשוב להוסיף את הדומיין של האתר לתיבה מקורות מורשים של JavaScript. כשמבצעים בדיקות מקומיות או פיתוח, צריך להוסיף את
http://localhostואתhttp://localhost:<port_number>לשדה מקורות JavaScript מורשים.
אימות ההטמעה
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.