כל שילוב של Cloud-to-cloud חייב לכלול מנגנון לאימות משתמשים.
אימות מאפשר לכם לקשר את חשבונות Google של המשתמשים עם חשבונות משתמשים במערכת האימות שלכם. כך תוכלו לזהות את המשתמשים שלכם כשמתקבלת כוונה לבית חכם ב-fulfillment. הפתרון של Google לבית חכם תומך רק ב-OAuth עם תהליך קוד הרשאה.
בדף הזה מוסבר איך להגדיר את שרת OAuth 2.0 כך שיפעל עם השילוב של Cloud-to-cloud.
קישור חשבון Google באמצעות OAuth
In the authorization code flow, you need two endpoints:
The authorization endpoint, which presents the sign-in UI to your users that aren't already signed in. The authorization endpoint also creates a short-lived authorization code to record users' consent to the requested access.
The token exchange endpoint, which is responsible for two types of exchanges:
- Exchanges an authorization code for a long-lived refresh token and a short-lived access token. This exchange happens when the user goes through the account linking flow.
- Exchanges a long-lived refresh token for a short-lived access token. This exchange happens when Google needs a new access token because the one it had expired.
Design guidelines
This section describes the design requirements and recommendations for the user screen that you host for OAuth linking flows. After it's called by Google's app, your platform displays a sign in to Google page and account linking consent screen to the user. The user is directed back to Google's app after giving their consent to link accounts.
Requirements
- You must communicate that the user’s account will be linked to Google, not a specific Google product like Google Home or Google Assistant.
- You must have a Google authorization statement such as "By signing in, you are authorizing Google to control your devices." See the Google Device Control Authorization section of the Google Home Developer Policies.
- You must open the Web OAuth linking page and ensure users have a clear method for signing in to their Google Account, such as fields for their username and password. Don't use the Google Sign-In (GSI) method that enables users to link without being taken to the Web OAuth Linking page. It is a violation of Google policy.
- You must include at least one of the following items in the OAuth linking
page to indicate the integration to which the user is linking:
- Company logo
- Company name
- Integration name
- App icon
Recommendations
We recommend that you do the following:
Display Google's Privacy Policy. Include a link to Google’s Privacy Policy on the consent screen.
Data to be shared. Use clear and concise language to tell the user what data of theirs Google requires and why.
Clear call-to-action. State a clear call-to-action on your consent screen, such as “Agree and link.” This is because users need to understand what data they're required to share with Google to link their accounts.
Ability to cancel. Provide a way for users to go back or cancel, if they choose not to link.
Clear sign-in process. Ensure that users have a clear method for signing in to their Google Account, such as fields for their username and password or Sign in with Google.
Ability to unlink. Offer a mechanism for users to unlink, such as a URL to their account settings on your platform. Alternatively, you can include a link to Google Account where users can manage their linked account.
Ability to change user account. Suggest a method for users to switch their account(s). This is especially beneficial if users tend to have multiple accounts.
- If a user must close the consent screen to switch accounts, send a recoverable error to Google so the user can sign in to the desired account with OAuth linking.
Include your logo. Display your company logo on the consent screen. Use your style guidelines to place your logo. If you wish to also display Google's logo, see Logos and trademarks.
תהליך קוד ההרשאה
הטמעה של שרת OAuth 2.0 של תהליך קוד ההרשאה כוללת שתי נקודות קצה, שהשירות שלכם צריך להפוך לזמינות באמצעות HTTPS. נקודת הקצה הראשונה היא נקודת הקצה של ההרשאה, שאחראית למציאת הסכמה מהמשתמשים לגישה לנתונים או לקבלת הסכמה כזו. נקודת הקצה של ההרשאה מציגה למשתמשים שלא מחוברים כבר ממשק משתמש לכניסה, ומתעדת את ההסכמה לגישה המבוקשת. נקודת הקצה השנייה היא נקודת הקצה של החלפת הטוקנים, שמשמשת לקבלת מחרוזות מוצפנות שנקראות טוקנים, שמאשרות למשתמש גישה לשירות שלכם.
כשנדרשת לאפליקציית Google קריאה לאחד מממשקי ה-API של השירות שלכם, Google משתמשת בנקודות הקצה האלה כדי לקבל מהמשתמשים שלכם הרשאה לקרוא לממשקי ה-API האלה בשמם.
תהליך הרשאה באמצעות קוד OAuth 2.0 שהופעל על ידי Google כולל את השלבים הבאים:
- Google פותחת את נקודת הקצה של ההרשאה בדפדפן של המשתמש. אם התהליך התחיל במכשיר עם קול בלבד לפעולה, Google מעבירה את הביצוע לטלפון.
- המשתמש מתחבר לחשבון שלו, אם הוא לא מחובר כבר, ומעניק ל-Google הרשאה לגשת לנתונים שלו באמצעות ה-API שלכם, אם הוא עדיין לא העניק הרשאה כזו.
- השירות שלכם יוצר קוד הרשאה ומחזיר אותו ל-Google. כדי לעשות זאת, צריך להפנות את הדפדפן של המשתמש בחזרה אל Google עם קוד ההרשאה שמצורף לבקשה.
- Google שולחת את קוד ההרשאה לנקודת הקצה (endpoint) של המרת האסימון, שמאמתת את האותנטיות של הקוד ומחזירה אסימון גישה ואסימון רענון. אסימון הגישה הוא אסימון לטווח קצר שהשירות מקבל כפרטי כניסה לגישה לממשקי API. אסימון הרענון הוא אסימון לטווח ארוך ש-Google יכולה לאחסן ולהשתמש בו כדי לקבל אסימוני גישה חדשים כשהתוקף שלהם פג.
- אחרי שהמשתמש משלים את תהליך קישור החשבון, כל בקשה עוקבת שנשלחת מ-Google מכילה אסימון גישה.
טיפול בבקשות הרשאה
כשצריך לבצע קישור חשבונות באמצעות תהליך קוד ההרשאה של OAuth 2.0, Google שולחת את המשתמש לנקודת הקצה של ההרשאה עם בקשה שכוללת את הפרמטרים הבאים:
| פרמטרים של נקודת קצה להרשאה | |
|---|---|
client_id |
מזהה הלקוח שהקציתם ל-Google. |
redirect_uri |
כתובת ה-URL שאליה שולחים את התשובה לבקשה הזו. |
state |
ערך לניהול חשבונות שמועבר בחזרה ל-Google ללא שינוי ב-URI של ההפניה. |
scope |
אופציונלי: קבוצה של מחרוזות היקף שמופרדות ברווחים ומציינות את הנתונים ש-Google מבקשת הרשאה לגשת אליהם. |
response_type |
סוג הערך שיוחזר בתשובה. בתהליך קוד ההרשאה של OAuth 2.0, סוג התגובה הוא תמיד code.
|
לדוגמה, אם נקודת הקצה להרשאה זמינה בכתובת https://myservice.example.com/auth, בקשה יכולה להיראות כך:
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&scope=REQUESTED_SCOPES&response_type=code
כדי שנקודת הקצה של ההרשאה תטפל בבקשות כניסה, צריך לבצע את השלבים הבאים:
- מוודאים ש-
client_idזהה למזהה הלקוח שהקציתם ל-Google, וש-redirect_uriזהה לכתובת ההפניה האוטומטית ש-Google סיפקה לשירות שלכם. הבדיקות האלה חשובות כדי למנוע מתן גישה לאפליקציות לקוח לא מכוונות או לא מוגדרות. אם אתם תומכים בכמה תהליכי OAuth 2.0, צריך גם לוודא שresponse_typeהואcode. - בודקים אם המשתמש מחובר לשירות שלכם. אם המשתמש לא מחובר, צריך להשלים את תהליך הכניסה או ההרשמה לשירות.
- יוצרים קוד הרשאה ש-Google יכולה להשתמש בו כדי לגשת ל-API שלכם. קוד ההרשאה יכול להיות כל ערך מחרוזת, אבל הוא חייב לייצג באופן ייחודי את המשתמש, את הלקוח שאליו האסימון משויך ואת זמן התפוגה של הקוד, ואי אפשר לנחש אותו. בדרך כלל מנפיקים קודי הרשאה שתוקפם פג אחרי כ-10 דקות.
- מוודאים שכתובת ה-URL שצוינה בפרמטר
redirect_uriהיא מהצורה הבאה:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
- הפניית הדפדפן של המשתמש לכתובת ה-URL שצוינה בפרמטר
redirect_uri. כשמפנים מחדש, צריך לצרף את הפרמטריםcodeו-state, ולכלול את קוד ההרשאה שנוצר ואת ערך המצב המקורי שלא שונה. דוגמה לכתובת ה-URL שמתקבלת:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING
טיפול בבקשות להחלפת טוקנים
נקודת הקצה של שירות החלפת הטוקנים אחראית לשני סוגים של החלפות טוקנים:
- המרת קודי הרשאה לאסימוני גישה ולאסימוני רענון
- המרת אסימוני רענון לאסימוני גישה
בקשות להחלפת אסימונים כוללות את הפרמטרים הבאים:
| פרמטרים של נקודת קצה להחלפת טוקנים | |
|---|---|
client_id |
מחרוזת שמזהה את מקור הבקשה כ-Google. המחרוזת הזו צריכה להיות רשומה במערכת שלכם כמזהה הייחודי של Google. |
client_secret |
מחרוזת סודית שרשמתם ב-Google לשירות שלכם. |
grant_type |
סוג הטוקן שמוחלף. הערך הזה יכול להיות authorization_code או refresh_token. |
code |
כאשר grant_type=authorization_code, הפרמטר הזה הוא הקוד ש-Google קיבלה מנקודת הקצה של הכניסה או של החלפת האסימון. |
redirect_uri |
אם הערך הוא grant_type=authorization_code, הפרמטר הזה הוא כתובת ה-URL שמשמשת בבקשת ההרשאה הראשונית. |
refresh_token |
אם הערך הוא grant_type=refresh_token, הפרמטר הזה הוא אסימון הרענון ש-Google קיבלה מנקודת הקצה (endpoint) של החלפת האסימונים. |
הגדרת האופן שבו Google שולחת את פרטי הכניסה לשרת
בהתאם להטמעה, שרת ההרשאות מצפה לקבל את פרטי הכניסה של הלקוח בגוף הבקשה או בכותרת הבקשה.
כברירת מחדל, Google שולחת את פרטי הכניסה בגוף הבקשה. אם שרת ההרשאות שלכם דורש שפרטי הכניסה של הלקוח יופיעו בכותרת הבקשה, אתם צריכים להגדיר את השילוב של Cloud-to-cloud בהתאם:
ברשימת הפרויקטים, לוחצים על פתיחה לצד הפרויקט שרוצים לעבוד איתו.
בקטע Cloud-to-Cloud, בוחרים באפשרות Develop (פיתוח).
לצד השילוב, לוחצים על פתיחה.
גוללים למטה לקטע Permissions (optional) (הרשאות (אופציונלי)) ומסמנים את תיבת הסימון Have Google transmit Client ID and secret via HTTP basic auth header (העברת מזהה לקוח וסוד באמצעות כותרת אימות בסיסית של HTTP).
לוחצים על שמירה כדי לשמור את השינויים.
המרת קודי הרשאה לאסימוני גישה ולאסימוני רענון
אחרי שהמשתמש נכנס ונקודת הקצה של ההרשאה מחזירה ל-Google קוד הרשאה לטווח קצר, Google שולחת בקשה לנקודת הקצה של החלפת הטוקנים כדי להחליף את קוד ההרשאה בטוקן גישה ובטוקן רענון.
בבקשות האלה, הערך של grant_type הוא authorization_code, והערך של code הוא הערך של קוד ההרשאה שהענקתם בעבר ל-Google. הדוגמה הבאה מציגה בקשה להחלפת קוד הרשאה באסימון גישה ובאסימון רענון:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=REDIRECT_URI
כדי להחליף קודי הרשאה בטוקן גישה ובטוקן רענון, נקודת הקצה להחלפת טוקנים מגיבה לבקשות של POST על ידי ביצוע השלבים הבאים:
- מוודאים שהערך
client_idמזהה את מקור הבקשה כמקור מורשה, ושהערךclient_secretתואם לערך הצפוי. - צריך לוודא שקוד ההרשאה תקין ולא פג התוקף שלו, ושמזהה הלקוח שצוין בבקשה זהה למזהה הלקוח שמשויך לקוד ההרשאה.
- מוודאים שכתובת ה-URL שצוינה בפרמטר
redirect_uriזהה לערך שנעשה בו שימוש בבקשת ההרשאה הראשונית. - אם אי אפשר לאמת את כל הקריטריונים שלמעלה, מחזירים שגיאת בקשה שגויה מסוג HTTP 400 עם
{"error": "invalid_grant"}כתוכן הבקשה. - אחרת, משתמשים במזהה המשתמש מקוד ההרשאה כדי ליצור טוקן רענון וטוקן גישה. האסימונים האלה יכולים להיות כל ערך מחרוזת, אבל הם חייבים לייצג באופן ייחודי את המשתמש ואת הלקוח שאליו האסימון משויך, והם לא יכולים להיות ניתנים לניחוש. לגבי אסימוני גישה, כדאי גם לתעד את זמן התפוגה של האסימון, שהוא בדרך כלל שעה אחרי הנפקת האסימון. תוקף אסימוני הרענון לא פג.
- מחזירים את אובייקט ה-JSON הבא בגוף התגובה של HTTPS:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "refresh_token": "REFRESH_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
Google שומרת את טוקן הגישה ואת טוקן הרענון של המשתמש, ומתעדת את תפוגת טוקן הגישה. כשתוקף אסימון הגישה פג, Google משתמשת באסימון הרענון כדי לקבל אסימון גישה חדש מנקודת הקצה של חילופי האסימונים.
המרת אסימוני רענון לאסימוני גישה
כשפג התוקף של אסימון גישה, Google שולחת בקשה לנקודת הקצה של החלפת האסימונים כדי להחליף אסימון רענון באסימון גישה חדש.
בבקשות האלה, הערך של grant_type הוא refresh_token, והערך של refresh_token הוא הערך של טוקן הרענון שנתתם ל-Google בעבר. דוגמה לבקשה להחלפת טוקן רענון בטוקן גישה:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
כדי להחליף אסימון רענון באסימון גישה, נקודת הקצה להחלפת אסימונים מגיבה לבקשות POST על ידי ביצוע השלבים הבאים:
- מוודאים שהערך
client_idמזהה את מקור הבקשה כ-Google, ושהערךclient_secretתואם לערך הצפוי. - צריך לוודא שאסימון הרענון תקף, ומזהה הלקוח שצוין בבקשה זהה למזהה הלקוח שמשויך לאסימון הרענון.
- אם אי אפשר לאמת את כל הקריטריונים שלמעלה, צריך להחזיר שגיאת בקשה שגויה (HTTP 400) עם
{"error": "invalid_grant"}כתוכן הבקשה. - אחרת, משתמשים במזהה המשתמש מאסימון הרענון כדי ליצור אסימון גישה. האסימונים האלה יכולים להיות כל ערך מחרוזת, אבל הם חייבים לייצג באופן ייחודי את המשתמש ואת הלקוח שאליהם האסימון משויך, ואי אפשר לנחש אותם. במקרה של אסימוני גישה, צריך לתעד גם את זמן התפוגה של האסימון, בדרך כלל שעה אחרי הנפקת האסימון.
- הפונקציה מחזירה את אובייקט ה-JSON הבא בגוף התגובה של HTTPS:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
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.