একটি OAuth 2.0 সার্ভার প্রয়োগ করুন

প্রতিটি smart home অ্যাকশন ব্যবহারকারীদের প্রমাণীকরণের জন্য একটি প্রক্রিয়া অন্তর্ভুক্ত করতে হবে।

প্রমাণীকরণ আপনাকে আপনার প্রমাণীকরণ সিস্টেমে ব্যবহারকারীর অ্যাকাউন্টগুলির সাথে আপনার ব্যবহারকারীদের Google অ্যাকাউন্টগুলিকে লিঙ্ক করতে দেয়৷ এটি আপনাকে আপনার ব্যবহারকারীদের শনাক্ত করতে দেয় যখন আপনার পূর্ণতা একটি স্মার্ট হোম অভিপ্রায় পায়। Google স্মার্ট হোম শুধুমাত্র একটি অনুমোদন কোড ফ্লো সহ OAuth সমর্থন করে।

এই পৃষ্ঠাটি বর্ণনা করে যে কীভাবে আপনার OAuth 2.0 সার্ভার সেট আপ করবেন যাতে এটি আপনার smart home অ্যাকশনের সাথে কাজ করে।

OAuth এর সাথে Google অ্যাকাউন্ট লিঙ্ক করা

অনুমোদন কোড প্রবাহে, আপনার দুটি শেষ পয়েন্ট প্রয়োজন:

  • অনুমোদন এন্ডপয়েন্ট, যা আপনার ব্যবহারকারীদের সাইন-ইন UI উপস্থাপন করে যারা ইতিমধ্যে সাইন ইন করেনি। অনুমোদনের শেষ পয়েন্টটি অনুরোধ করা অ্যাক্সেসে ব্যবহারকারীদের সম্মতি রেকর্ড করার জন্য একটি স্বল্প-কালীন অনুমোদন কোডও তৈরি করে।

  • টোকেন এক্সচেঞ্জ এন্ডপয়েন্ট, যা দুই ধরনের এক্সচেঞ্জের জন্য দায়ী:

    1. একটি দীর্ঘস্থায়ী রিফ্রেশ টোকেন এবং একটি স্বল্পকালীন অ্যাক্সেস টোকেনের জন্য একটি অনুমোদন কোড বিনিময় করে৷ এই বিনিময়টি ঘটে যখন ব্যবহারকারী অ্যাকাউন্ট লিঙ্কিং প্রবাহের মধ্য দিয়ে যায়।
    2. একটি স্বল্পকালীন অ্যাক্সেস টোকেনের জন্য দীর্ঘস্থায়ী রিফ্রেশ টোকেন বিনিময় করে। এই বিনিময়টি ঘটে যখন Google এর একটি নতুন অ্যাক্সেস টোকেনের প্রয়োজন হয় কারণ এটির মেয়াদ শেষ হয়ে গেছে৷

ডিজাইন নির্দেশিকা

এই বিভাগে আপনি OAuth লিঙ্কিং ফ্লোগুলির জন্য হোস্ট করা ব্যবহারকারী স্ক্রিনের জন্য ডিজাইনের প্রয়োজনীয়তা এবং সুপারিশগুলি বর্ণনা করে৷ এটি Google-এর অ্যাপ দ্বারা কল করার পরে, আপনার প্ল্যাটফর্মটি Google পৃষ্ঠায় একটি সাইন ইন এবং ব্যবহারকারীকে অ্যাকাউন্ট লিঙ্ক করার সম্মতি স্ক্রীন প্রদর্শন করে। অ্যাকাউন্ট লিঙ্ক করার সম্মতি দেওয়ার পরে ব্যবহারকারীকে Google-এর অ্যাপে ফেরত পাঠানো হয়।

এই চিত্রটি ব্যবহারকারীর জন্য তাদের Google অ্যাকাউন্টকে আপনার প্রমাণীকরণ সিস্টেমের সাথে লিঙ্ক করার পদক্ষেপগুলি দেখায়৷ প্রথম স্ক্রিনশটটি আপনার প্ল্যাটফর্ম থেকে ব্যবহারকারীর সূচনা লিঙ্ক দেখায়। দ্বিতীয় চিত্রটি ব্যবহারকারীর Google-এ সাইন-ইন দেখায়, যখন তৃতীয়টি ব্যবহারকারীর সম্মতি এবং আপনার অ্যাপের সাথে তাদের Google অ্যাকাউন্ট লিঙ্ক করার জন্য নিশ্চিতকরণ দেখায়। চূড়ান্ত স্ক্রিনশট Google অ্যাপে একটি সফলভাবে লিঙ্ক করা ব্যবহারকারীর অ্যাকাউন্ট দেখায়।
চিত্র 1. অ্যাকাউন্ট লিঙ্ককারী ব্যবহারকারী গুগলে সাইন ইন করুন এবং সম্মতি স্ক্রীন।

প্রয়োজনীয়তা

  1. আপনাকে অবশ্যই যোগাযোগ করতে হবে যে ব্যবহারকারীর অ্যাকাউন্টটি Google-এর সাথে লিঙ্ক করা হবে, Google Home বা Google Assistant-এর মতো নির্দিষ্ট Google পণ্যের সাথে নয়
  2. আপনার অবশ্যই একটি Google অনুমোদন বিবৃতি থাকতে হবে যেমন "সাইন ইন করার মাধ্যমে, আপনি আপনার ডিভাইসগুলিকে নিয়ন্ত্রণ করার জন্য Googleকে অনুমোদন করছেন।" Google ��োম ডেভেলপার নীতির Google ডিভাইস নিয়ন্ত্রণ অনুমোদন বিভাগটি দেখুন।
  3. ব্যবহারকারীরা লিঙ্ক না করতে চাইলে আপনাকে অবশ্যই ফিরে যেতে বা বাতিল করার জন্য একটি উপায় প্রদান করতে হবে।
  4. আপনাকে অবশ্যই ওয়েব OAuth লিঙ্কিং পৃষ্ঠা খুলতে হবে এবং নিশ্চিত করতে হবে যে ব্যবহারকারীদের তাদের Google অ্যাকাউন্টে সাইন ইন করার জন্য একটি পরিষ্কার পদ্ধতি আছে, যেমন তাদের ব্যবহারকারীর নাম এবং পাসওয়ার্ডের জন্য ক্ষেত্র। Google সাইন-ইন (GSI) পদ্ধতি ব্যবহার করবেন না যা ব্যবহারকারীদের ওয়েব OAuth লিঙ্কিং পৃষ্ঠায় না নিয়েই লিঙ্ক করতে সক্ষম করে৷ এটি Google নীতির লঙ্ঘন।

সুপারিশ

আমরা আপনাকে নিম্নলিখিতগুলি করার পরামর্শ দিই:

  1. Google এর গোপনীয়তা নীতি প্রদর্শন করুন। সম্মতি স্ক্রিনে Google-এর গোপনীয়তা নীতির একটি লিঙ্ক অন্তর্ভুক্ত করুন।

  2. ডেটা শেয়ার করতে হবে। ব্যবহারকারীকে তাদের Google-এর কোন ডেটা প্রয়োজন এবং কেন তা জানাতে স্পষ্ট এবং সংক্ষিপ্ত ভাষা ব্যবহার করুন।

  3. কল-টু-অ্যাকশন পরিষ্কার করুন। আপনার সম্মতি স্ক্রিনে একটি স্পষ্ট কল-টু-অ্যাকশন বলুন, যেমন "সম্মতি এবং লিঙ্ক"। এর কারণ হল ব্যবহারকারীদের বুঝতে হবে তাদের অ্যাকাউন্ট লিঙ্ক করার জন্য Google-এর সাথে কী ডেটা শেয়ার করতে হবে।

  4. লিঙ্কমুক্ত করার ক্ষমতা। ব্যবহারকারীদের আনলিঙ্ক করার জন্য একটি পদ্ধতি অফার করুন, যেমন আপনার প্ল্যাটফর্মে তাদের অ্যাকাউন্ট সেটিংসের URL। বিকল্পভাবে, আপনি Google অ্যাকাউন্টে একটি লিঙ্ক অন্তর্ভুক্ত করতে পারেন যেখানে ব্যবহারকারীরা তাদের লিঙ্ক করা অ্যাকাউন্ট পরিচালনা করতে পারে।

  5. ব্যবহারকারীর অ্যাকাউন্ট পরিবর্তন করার ক্ষমতা। ব্যবহারকারীদের তাদের অ্যাকাউন্ট(গুলি) পরিবর্তন করার জন্য একটি পদ্ধতির পরামর্শ দিন। এটি বিশেষত উপকারী যদি ব্যবহারকারীদের একাধিক অ্যাকাউন্ট থাকে।

    • যদি কোনো ব্যবহারকারীকে অ্যাকাউন্ট পাল্টানোর জন্য সম্মতি স্ক্রীন বন্ধ করতে হয়, তাহলে Google-এ একটি পুনরুদ্ধারযোগ্য ত্রুটি পাঠান যাতে ব্যবহারকারী OAuth লিঙ্কের মাধ্যমে পছন্দসই অ্যাকাউন্টে সাইন ইন করতে পারেন।
  6. আপনার লোগো অন্তর্ভুক্ত করুন. সম্মতি স্ক্রিনে আপনার কোম্পানির লোগো প্রদর্শন করুন। আপনার লোগো স্থাপন করতে আপনার শৈলী নির্দেশিকা ব্যবহার করুন. আপনি যদি Google এর লোগোও প্রদর্শন করতে চান তবে লোগো এবং ট্রেডমার্ক দেখুন।

অনুমোদন কোড প্রবাহ

An OAuth 2.0 server implementation of the authorization code flow consists of two endpoints, which your service makes available by HTTPS. The first endpoint is the authorization endpoint, which is responsible for finding or obtaining consent from users for data access. The authorization endpoint presents a sign-in UI to your users that aren't already signed in and records consent to the requested access. The second endpoint is the token exchange endpoint, which is used to obtain encrypted strings, called tokens, that authorize a user to access your service.

When a Google application needs to call one of your service's APIs, Google uses these endpoints together to get permission from your users to call these APIs on their behalf.

An OAuth 2.0 authorization code flow session initiated by Google has the following flow:

  1. Google opens your authorization endpoint in the user's browser. If the flow started on a voice-only device for an Action, Google transfers the execution to a phone.
  2. The user signs in, if not signed in already, and grants Google permission to access their data with your API, if they haven't already granted permission.
  3. Your service creates an authorization code and returns it to Google. To do so, redirect the user's browser back to Google with the authorization code attached to the request.
  4. Google sends the authorization code to your token exchange endpoint, which verifies the authenticity of the code and returns an access token and a refresh token. The access token is a short-lived token that your service accepts as credentials to access APIs. The refresh token is a long-lived token that Google can store and use to acquire new access tokens when they expire.
  5. After the user has completed the account linking flow, every subsequent request sent from Google contains an access token.

Handle authorization requests

When you need to perform account linking using the OAuth 2.0 authorization code flow, Google sends the user to your authorization endpoint with a request that includes the following parameters:

Authorization endpoint parameters
client_id The Client ID you assigned to Google.
redirect_uri The URL to which you send the response to this request.
state A bookkeeping value that is passed back to Google unchanged in the redirect URI.
scope Optional: A space-delimited set of scope strings that specify the data Google is requesting authorization for.
response_type The type of value to return in the response. For the OAuth 2.0 authorization code flow, the response type is always code.
user_locale The Google Account language setting in RFC5646 format, used to localize your content in the user's preferred language.

For example, if your authorization endpoint is available at https://myservice.example.com/auth, a request might look like the following:

GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&scope=REQUESTED_SCOPES&response_type=code&user_locale=LOCALE

For your authorization endpoint to handle sign-in requests, do the following steps:

  1. Verify that the client_id matches the Client ID you assigned to Google, and that the redirect_uri matches the redirect URL provided by Google for your service. These checks are important to prevent granting access to unintended or misconfigured client apps. If you support multiple OAuth 2.0 flows, also confirm that the response_type is code.
  2. Check if the user is signed in to your service. If the user isn't signed in, complete your service's sign-in or sign-up flow.
  3. Generate an authorization code for Google to use to access your API. The authorization code can be any string value, but it must uniquely represent the user, the client the token is for, and the code's expiration time, and it must not be guessable. You typically issue authorization codes that expire after approximately 10 minutes.
  4. Confirm that the URL specified by the redirect_uri parameter has the following form:
      https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
      https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
      
  5. Redirect the user's browser to the URL specified by the redirect_uri parameter. Include the authorization code you just generated and the original, unmodified state value when you redirect by appending the code and state parameters. The following is an example of the resulting URL:
    https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING

Handle token exchange requests

Your service's token exchange endpoint is responsible for two kinds of token exchanges:

  • Exchange authorization codes for access tokens and refresh tokens
  • Exchange refresh tokens for access tokens

Token exchange requests include the following parameters:

Token exchange endpoint parameters
client_id A string that identifies the request origin as Google. This string must be registered within your system as Google's unique identifier.
client_secret A secret string that you registered with Google for your service.
grant_type The type of token being exchanged. It's either authorization_code or refresh_token.
code When grant_type=authorization_code, this parameter is the code Google received from either your sign-in or token exchange endpoint.
redirect_uri When grant_type=authorization_code, this parameter is the URL used in the initial authorization request.
refresh_token When grant_type=refresh_token, this parameter is the refresh token Google received from your token exchange endpoint.

Exchange authorization codes for access tokens and refresh tokens

After the user signs in and your authorization endpoint returns a short-lived authorization code to Google, Google sends a request to your token exchange endpoint to exchange the authorization code for an access token and a refresh token.

For these requests, the value of grant_type is authorization_code, and the value of code is the value of the authorization code you previously granted to Google. The following is an example of a request to exchange an authorization code for an access token and a refresh token:

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

To exchange authorization codes for an access token and a refresh token, your token exchange endpoint responds to POST requests by executing the following steps:

  1. Verify that the client_id identifies the request origin as an authorized origin, and that the client_secret matches the expected value.
  2. Verify that the authorization code is valid and not expired, and that the client ID specified in the request matches the client ID associated with the authorization code.
  3. Confirm that the URL specified by the redirect_uri parameter is identical to the value used in the initial authorization request.
  4. If you can't verify all of the above criteria, return an HTTP 400 Bad Request error with {"error": "invalid_grant"} as the body.
  5. Otherwise, use the user ID from the authorization code to generate a refresh token and an access token. These tokens can be any string value, but they must uniquely represent the user and the client the token is for, and they must not be guessable. For access tokens, also record the expiration time of the token, which is typically an hour after you issue the token. Refresh tokens don't expire.
  6. Return the following JSON object in the body of the HTTPS response:
    {
    "token_type": "Bearer",
    "access_token": "ACCESS_TOKEN",
    "refresh_token": "REFRESH_TOKEN",
    "expires_in": SECONDS_TO_EXPIRATION
    }
    

Google stores the access token and the refresh token for the user and records the expiration of the access token. When the access token expires, Google uses the refresh token to get a new access token from your token exchange endpoint.

Exchange refresh tokens for access tokens

When an access token expires, Google sends a request to your token exchange endpoint to exchange a refresh token for a new access token.

For these requests, the value of grant_type is refresh_token, and the value of refresh_token is the value of the refresh token you previously granted to Google. The following is an example of a request to exchange a refresh token for an access token:

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

To exchange a refresh token for an access token, your token exchange endpoint responds to POST requests by executing the following steps:

  1. Verify that the client_id identifies the request origin as Google, and that the client_secret matches the expected value.
  2. Verify that the refresh token is valid, and that the client ID specified in the request matches the client ID associated with the refresh token.
  3. If you can't verify all of the above criteria, return an HTTP 400 Bad Request error with {"error": "invalid_grant"} as the body.
  4. Otherwise, use the user ID from the refresh token to generate an access token. These tokens can be any string value, but they must uniquely represent the user and the client the token is for, and they must not be guessable. For access tokens, also record the expiration time of the token, typically an hour after you issue the token.
  5. Return the following JSON object in the body of the HTTPS response:
    {
    "token_type": "Bearer",
    "access_token": "ACCESS_TOKEN",
    "expires_in": SECONDS_TO_EXPIRATION
    }

ব্যবহারকারীর তথ্যের অনুরোধগুলি পরিচালনা করুন

ইউজারইনফো এন্ডপয়েন্ট হল একটি OAuth 2.0 সুরক্ষিত রিসোর্স যা লিঙ্ক করা ব্যবহারকারীর বিষয়ে দাবি ফেরত দেয়। ইউজার ইনফো এন্ডপয়েন্ট বাস্তবায়ন এবং হোস্ট করা ঐচ্ছিক, নিম্নলিখিত ব্যবহারের ক্ষেত্রে ছাড়া:

আপনার টোকেন এন্ডপয়েন্ট থেকে অ্যাক্সেস টোকেন সফলভাবে পুনরুদ্ধার করার পরে, Google লিঙ্ক করা ব্যবহারকারীর সম্পর্কে প্রাথমিক প্রোফাইল তথ্য পুনরুদ্ধার করার জন্য আপনার ব্যবহারকারীর তথ্য এন্ডপয়েন্টে একটি অনুরোধ পাঠায়।

userinfo এন্ডপয়েন্ট রিকোয়েস্ট হেডার
Authorization header টাইপ বিয়ারারের অ্যাক্সেস টোকেন।

উদাহরণস্বরূপ, যদি আপনার ব্যবহারকারীর তথ্যের এন্ডপয়েন্ট https://myservice.example.com/userinfo এ উপলব্ধ থাকে, তাহলে একটি অনুরোধ নিম্নলিখিতটির মতো দেখতে পারে:

GET /userinfo HTTP/1.1
Host: myservice.example.com
Authorization: Bearer ACCESS_TOKEN

অনুরোধগুলি পরিচালনা করার জন্য আপনার ব্যবহারকারীর তথ্যের শেষ পয়েন্টের জন্য, নিম্নলিখিত পদক্ষেপগুলি করুন:

  1. অনুমোদন শিরোনাম থেকে অ্যাক্সেস টোকেন বের করুন এবং অ্যাক্সেস টোকেনের সাথে যুক্ত ব্যবহারকারীর জন্য তথ্য ফেরত দিন।
  2. অ্যাক্সেস টোকেনটি অবৈধ হলে, WWW-Authenticate প্রতিক্রিয়া শিরোনাম ব্যবহার করে একটি HTTP 401 অননুমোদিত ত্রুটি ফেরত দিন। নীচে একটি ব্যবহারকারীর তথ্য ত্রুটি প্রতিক্রিয়ার একটি উদাহরণ রয়েছে:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    লিঙ্কিং প্রক্রিয়া চলাকালীন যদি একটি 401 অননুমোদিত, বা অন্য কোনও অসফল ত্রুটি প্রতিক্রিয়া ফেরত দেওয়া হয়, তবে ত্রুটিটি পুনরুদ্ধারযোগ্য নয়, পুনরুদ্ধার করা টোকেনটি বাতিল করা হবে এবং ব্যবহারকারীর কাছে থাকবে আবার লিঙ্কিং প্রক্রিয়া শুরু করতে।
  3. অ্যাক্সেস টোকেন বৈধ হলে, HTTPS প্রতিক্রিয়ার মূল অংশে নিম্নলিখিত JSON অবজেক্টের সাথে HTTP 200 প্রতিক্রিয়া প্রত্যাবর্তন করুন:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    
    যদি আপনার ব্যবহারকারীর তথ্য এন্ডপয়েন্ট একটি HTTP 200 সাফল্যের প্রতিক্রিয়া প্রদান করে, তবে পুনরুদ্ধার করা টোকেন এবং দাবিগুলি ব্যবহারকারীর Google এর বিরুদ্ধে নিবন্ধিত হয় অ্যাকাউন্ট

    ব্যবহারকারীর তথ্য শেষ পয়েন্ট প্রতিক্রিয়া
    sub একটি অনন্য আইডি যা আপনার সিস্টেমে ব্যবহারকারীকে শনাক্ত করে।
    email ব্যবহারকারীর ইমেল ঠিকানা।
    given_name ঐচ্ছিক: ব্যবহারকারীর প্রথম নাম।
    family_name ঐচ্ছিক: ব্যবহারকারীর শেষ নাম।
    name ঐচ্ছিক: ব্যবহারকারীর পুরো নাম।
    picture ঐচ্ছিক: ব্যবহারকারীর প্রোফাইল ছবি।