סקירה כללית
כניסה באמצעות חשבון 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ו-get, ואופציונלית את הכוונהcreate.
לוגיקת ההחלטה לקישור פשוט
הלוגיקה הבאה קובעת איך מתבצעת קריאה לכוונות במהלך תהליך הקישור הפשוט:
- האם למשתמש יש חשבון במערכת האימות שלכם? (המשתמש מחליט אם לבחור באפשרות YES או NO)
- YES : האם המשתמש משתמש בכתובת האימייל שמשויכת לחשבון Google שלו כדי להיכנס לפלטפורמה שלך? (המשתמש מחליט אם לבחור באפשרות 'כן' או 'לא')
- כן : האם למשתמש יש חשבון תואם במערכת האימות שלכם? (מתבצעת שיחה עם
checkintent כדי לאשר)- כן : מתבצעת קריאה ל
getintent והחשבון מקושר אם הפונקציה get intent מחזירה ערך בהצלחה. - לא : ליצור חשבון חדש? (המשתמש מחליט אם ליצור חשבון על ידי בחירה באפשרות 'כן' או 'לא'; רלוונטי רק אם השירות תומך ביצירת חשבון)
- כן : מתבצעת קריאה אל
createintent והחשבון מקושר אם הפעולה create intent מסתיימת בהצלחה. - לא : מופעל תהליך הקישור באמצעות OAuth, המשתמש מועבר לדפדפן שלו ומוצגת לו האפשרות לקשר חשבון עם כתובת אימייל אחרת.
- כן : מתבצעת קריאה אל
- כן : מתבצעת קריאה ל
- לא : מופעל תהליך הקישור באמצעות OAuth, המשתמש מועבר לדפדפן ומוצגת לו האפשרות לקשר כתובת אימייל אחרת.
- כן : האם למשתמש יש חשבון תואם במערכת האימות שלכם? (מתבצעת שיחה עם
- לא : האם למשתמש יש חשבון תואם במערכת האימות שלך? (מתבצעת שיחה עם
checkintent כדי לאשר)- כן : מתבצעת קריאה אל
getintent והחשבון מקושר אם הפונקציה get intent מחזירה ערך בהצלחה. - לא : אם השירות תומך ביצירת חשבון, מתבצעת קריאה אל
createהכוונה והחשבון מקושר אם הכוונה ליצירת חשבון מוחזרת בהצלחה. אם יצירת חשבון לא נתמכת, נקודת הקצה צריכה להחזיר שגיאת קישור HTTP 401 כדי להפעיל את תהליך הקישור החלופי של OAuth.
- כן : מתבצעת קריאה אל
- YES : האם המשתמש משתמש בכתובת האימייל שמשויכת לחשבון Google שלו כדי להיכנס לפלטפורמה שלך? (המשתמש מחליט אם לבחור באפשרות 'כן' או 'לא')
מתכון להטמעה
נקודת הקצה של החלפת הטוקנים צריכה להטמיע את כוונות 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 required 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 required 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 your service supports account creation and 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 optional 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.