OAuth की मदद से Google खाता लिंक करना (इंप्लिसिट फ़्लो - संग्रहित किया गया)

OAuth 2.0 के इंप्लिसिट फ़्लो के लिए, आपकी सेवा एचटीटीपीएस के ज़रिए ऑथराइज़ेशन एंडपॉइंट उपलब्ध कराती है. यह एंडपॉइंट, पुष्टि करने और डेटा का ऐक्सेस करने के लिए उपयोगकर्ताओं से सहमति लेने का काम करता है. अनुमति देने वाला एंडपॉइंट, उन उपयोगकर्ताओं को साइन इन करने का यूज़र इंटरफ़ेस (यूआई) दिखाता है जिन्होंने पहले से साइन इन नहीं किया है. साथ ही, यह अनुरोध किए गए ऐक्सेस के लिए सहमति रिकॉर्ड करता है.

जब किसी Google ऐप्लिकेशन को आपकी सेवा के किसी ऐसे एपीआई को कॉल करना होता है जिसे अनुमति मिली हुई है, तो Google इस एंडपॉइंट का इस्तेमाल करता है. इससे वह आपके उपयोगकर्ताओं से, उनकी ओर से इन एपीआई को कॉल करने की अनुमति ले पाता है.

Google खाते को किसी दूसरे खाते से लिंक करने की सुविधा: OAuth इंप्लिसिट फ़्लो

इस क्रम के डायग्राम में, उपयोगकर्ता, Google, और आपकी सेवा के एंडपॉइंट के बीच होने वाली इंटरैक्शन की जानकारी दी गई है.

User Google ऐप्लिकेशन / Browser Your Auth Endpoint 1. उपयोगकर्ता खाता लिंक करने की प्रोसेस शुरू करता है 2. रीडायरेक्ट टू ऑथ एंडपॉइंट (GET) client_id, redirect_uri, state, scope 3. Display Sign-in & Consent Screen 4. उपयोगकर्ता पुष्टि करता है और सहमति देता है 5. टोकन के साथ Google पर वापस रीडायरेक्ट करें (GET) access_token, state 6. स्टोर के उपयोगकर्ता टोकन 7. उपयोगकर्ता के संसाधनों को ऐक्सेस करना
पहली इमेज. Google खाता लिंक करने के लिए, OAuth 2.0 इंप्लिसिट फ़्लो में होने वाले इवेंट का क्रम.

भूमिकाएं और ज़िम्मेदारियां

यहां दी गई टेबल में, Google खाते को किसी दूसरे खाते से लिंक करने की सुविधा (जीएएल) के OAuth इंप्लिसिट फ़्लो में शामिल लोगों की भूमिकाओं और ज़िम्मेदारियों के बारे में बताया गया है. ध्यान दें कि GAL में, Google, OAuth क्लाइंट के तौर पर काम करता है. वहीं, आपकी सेवा, पहचान/सेवा देने वाली कंपनी के तौर पर काम करती है.

ऐक्टर / कॉम्पोनेंट जीएएल की भूमिका ज़िम्मेदारियां
Google ऐप्लिकेशन / सर्वर OAuth क्लाइंट यह फ़्लो शुरू करता है. साथ ही, ब्राउज़र रीडायरेक्ट का इस्तेमाल करके ऐक्सेस टोकन पाता है. इसके अलावा, यह आपकी सेवा के एपीआई को ऐक्सेस करने के लिए, इसे सुरक्षित तरीके से सेव करता है.
आपका ऑथराइज़ेशन एंडपॉइंट अनुमति देने वाला सर्वर यह कुकी, आपके उपयोगकर्ताओं की पुष्टि करती है, उनकी सहमति लेती है, और Google को सीधे तौर पर लंबे समय तक इस्तेमाल किए जा सकने वाले ऐक्सेस टोकन जारी करती है.
Google रीडायरेक्ट यूआरआई कॉलबैक एंडपॉइंट यह आपकी अनुमति देने वाली सेवा से उपयोगकर्ता को रीडायरेक्ट करता है. साथ ही, यूआरएल फ़्रैगमेंट में access_token और state वैल्यू शामिल करता है.

Google की ओर से शुरू किए गए OAuth 2.0 इंप्लिसिट फ़्लो सेशन का सामान्य फ़्लो इस तरह होता है:

  1. Google, उपयोगकर्ता के ब्राउज़र में आपका ऑथराइज़ेशन एंडपॉइंट खोलता है. अगर उपयोगकर्ता ने पहले से साइन इन नहीं किया है, तो वह साइन इन करता है. इसके बाद, अगर उसने पहले से अनुमति नहीं दी है, तो वह Google को आपके एपीआई के ज़रिए अपना डेटा ऐक्सेस करने की अनुमति देता है.
  2. आपकी सेवा, एक ऐक्सेस टोकन बनाती है और उसे Google को वापस भेजती है. इसके लिए, उपयोगकर्ता के ब्राउज़र को वापस Google पर रीडायरेक्ट करें. साथ ही, अनुरोध में ऐक्सेस टोकन अटैच करें.
  3. Google, आपकी सेवा के एपीआई को कॉल करता है और हर अनुरोध के साथ ऐक्सेस टोकन अटैच करता है. आपकी सेवा यह पुष्टि करती है कि ऐक्सेस टोकन, Google को एपीआई ऐक्सेस करने की अनुमति देता है. इसके बाद, एपीआई कॉल पूरा किया जाता है.

लागू करने की रेसिपी

इंप्लिसिट फ़्लो लागू करने के लिए, यह तरीका अपनाएं.

पहला चरण: अनुमति के अनुरोधों को मैनेज करना

जब Google खाता लिंक करने की प्रोसेस शुरू करता है, तो वह उपयोगकर्ता को आपके अनुमति वाले एंडपॉइंट पर रीडायरेक्ट करता है. प्रोटोकॉल के कानूनी समझौतों और पैरामीटर की ज़रूरी शर्तों के बारे में ज़्यादा जानने के लिए, ऑथराइज़ेशन एंडपॉइंट देखें.

अनुरोध को मैनेज करने के लिए, ये कार्रवाइयां करें:

  1. अनुरोध की पुष्टि करें:

    • पुष्टि करें कि client_id, Google को असाइन किए गए क्लाइंट आईडी से मेल खाता हो.
    • पुष्टि करें कि redirect_uri, Google के रीडायरेक्ट किए जाने वाले यूआरएल से मेल खाता हो: none https://oauth-redirect.googleusercontent.com/r/YOUR_PARTNER_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PARTNER_ID
    • पुष्टि करें कि response_type token है.
  2. उपयोगकर्ता की पुष्टि करना:

    • यह कुकी यह पता लगाती है कि उपयोगकर्ता ने आपकी सेवा में साइन इन किया है या नहीं.
    • अगर उपयोगकर्ता ने साइन इन नहीं किया है, तो उसे साइन इन या साइन-अप करने के लिए कहें.
  3. ऐक्सेस टोकन जनरेट करें:

    • उपयोगकर्ता और क्लाइंट से जुड़ा एक यूनीक ऐक्सेस टोकन बनाएं. यह ऐसा होना चाहिए जिसका अनुमान न लगाया जा सके.
  4. Google पर वापस रीडायरेक्ट करना:

    • ब्राउज़र को redirect_uri में दिए गए यूआरएल पर रीडायरेक्ट करें.
    • यूआरएल फ़्रैगमेंट (हैश) में ये पैरामीटर जोड़ें:
      • access_token: जनरेट किया गया ऐक्सेस टोकन.
      • token_type: bearer होना चाहिए.
      • state: Google से मिली, बिना बदलाव वाली स्थिति की वैल्यू.
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.