क्लाइंट आईडी अनुरोध पर हस्ताक्षर

अहम जानकारी: Google Maps Platform का प्रीमियम प्लान, अब साइन अप करने वाले या नए ग्राहकों के लिए उपलब्ध नहीं है.

डिजिटल हस्ताक्षर

डिजिटल हस्ताक्षर कैसे काम करते हैं

डिजिटल हस्ताक्षर, यूआरएल पर हस्ताक्षर करने वाले सीक्रेट या क्रिप्टोग्राफ़िक कुंजी का इस्तेमाल करके जनरेट किए जाते हैं. ये पासकोड, Google Cloud Console पर उपलब्ध होते हैं. यह सीक्रेट एक निजी पासकोड है, जिसे सिर्फ़ आपके और Google के बीच शेयर किया जाता है. यह आपके क्लाइंट आईडी के लिए यूनीक होता है.

हस्ताक्षर करने की प्रोसेस में, यूआरएल और आपके शेयर किए गए सीक्रेट को एक साथ जोड़ा जाता है. इसके लिए, एन्क्रिप्ट (सुरक्षित) करने के एल्गोरिदम का इस्तेमाल किया जाता है. मिलने वाला यूनीक हस्ताक्षर हमारे सर्वर को यह पुष्टि करने की अनुमति देता है कि आपके क्लाइंट आईडी का इस्तेमाल करके, साइट जनरेट करने वाले सभी अनुरोधों को ऐसा करने की अनुमति है या नहीं.

अपने अनुरोधों पर हस्ताक्षर करना

अपने अनुरोधों पर हस्ताक्षर करने के लिए, इन चरणों को पूरा किया जाता है:

पहला चरण: अपने यूआरएल पर हस्ताक्षर करने का सीक्रेट हासिल करना

अपने प्रोजेक्ट के यूआरएल पर हस्ताक्षर करने का सीक्रेट पाने के लिए:

  1. Cloud Console में, क्लाइंट आईडी पेज पर जाएं.
  2. कुंजी फ़ील्ड में, आपके मौजूदा क्लाइंट आईडी के यूआरएल का साइनिंग सीक्रेट होता है.

अगर आपको अपना क्लाइंट आईडी यूआरएल साइनिंग सीक्रेट फिर से जनरेट करना है, तो सहायता टीम से संपर्क करें.

दूसरा चरण: बिना हस्ताक्षर वाला अनुरोध तैयार करना

नीचे दी गई टेबल में जो वर्ण नहीं जोड़े गए हैं वे यूआरएल के हिसाब से कोड में बदले जाने चाहिए:

मान्य यूआरएल वर्णों की खास जानकारी
सेट करेंवर्णयूआरएल का इस्तेमाल
अक्षर और अंक दोनों शामिल हो सकते हैं ए बी सी डी ई एफ़ ग एच आई जे के एल मी एन ओ पी क्यू र स टी यू वी वा x वाय z ए बी सी डी ई एफ़ जी एच आई जे के एल M N O P Q R S T U V X 8 9 6 0 1 2 3 4 5 6 टेक्स्ट स्ट्रिंग, स्कीम का इस्तेमाल (http), पोर्ट (8080) वगैरह.
गैर-आरक्षित - _ . ~ टेक्स्ट स्ट्रिंग
बुकिंग की गई ! * ' ( ) ; : @ & = + $ , / ? % # [ ] कंट्रोल कैरेक्टर और/या टेक्स्ट स्ट्रिंग

यह बात रिज़र्व्ड सेट में मौजूद सभी वर्णों पर भी लागू होती है, अगर उन्हें किसी टेक्स्ट स्ट्रिंग में पास किया जाता है. ज़्यादा जानकारी के लिए, खास वर्ण देखें.

बिना हस्ताक्षर वाला अनुरोध करने वाला यूआरएल बनाएं.

client पैरामीटर में, क्लाइंट आईडी भी शामिल करना न भूलें. उदाहरण के लिए:

https://maps.googleapis.com/maps/api/staticmap?center=Z%C3%BCrich&size=400x400&client=YOUR_CLIENT_ID

हस्ताक्षर किया गया अनुरोध जनरेट करें

समस्या हल करने के लिए, उपलब्ध यूआरएल पर अभी हस्ताक्षर करें विजेट का इस्तेमाल करके, अपने-आप एक डिजिटल हस्ताक्षर जनरेट किया जा सकता है.

डाइनैमिक तौर पर जनरेट होने वाले अनुरोधों के लिए, आपको सर्वर-साइड साइनिंग की सुविधा की ज़रूरत होगी. इसके लिए, कुछ और इंटरमीडिएट चरणों की ज़रूरत होती है

दोनों ही मामलों में, आपको अनुरोध वाला ऐसा यूआरएल मिलना चाहिए जिसके आखिर में signature पैरामीटर जुड़ा हो. उदाहरण के लिए:

https://maps.googleapis.com/maps/api/staticmap?center=Z%C3%BCrich&size=400x400&client=YOUR_CLIENT_ID
&signature=BASE64_SIGNATURE
  1. यूआरएल के प्रोटोकॉल स्कीम और होस्ट वाले हिस्सों को हटा दें. इससे सिर्फ़ पाथ और क्वेरी हट जाएगी:

  2. /maps/api/staticmap?center=Z%C3%BCrich&size=400x400&client=YOUR_CLIENT_ID
    
  3. दिखाए गए यूआरएल के साइनिंग सीक्रेट को यूआरएल के लिए, बदले गए Base64 से एन्कोड किया गया.

    ज़्यादातर क्रिप्टोग्राफ़िक लाइब्रेरी के लिए, कुंजी को रॉ बाइट फ़ॉर्मैट में होना ज़रूरी है. इसलिए, साइन करने से पहले आपको अपने यूआरएल के साइनिंग सीक्रेट को उसके मूल रॉ फ़ॉर्मैट में डिकोड करना होगा.

  4. HMAC-SHA1 का इस्तेमाल करके हटाए गए ऊपर दिए गए अनुरोध पर हस्ताक्षर करें.
  5. ज़्यादातर क्रिप्टोग्राफ़िक लाइब्रेरी, रॉ बाइट फ़ॉर्मैट में सिग्नेचर जनरेट करती हैं. इसलिए, आपको यूआरएल के लिए बदले गए Base64 का इस्तेमाल करके, बने बाइनरी हस्ताक्षर को बदलना होगा. ऐसा इसलिए, ताकि उसे यूआरएल में पास किया जा सके.

  6. signature पैरामीटर में, बिना हस्ताक्षर वाले मूल अनुरोध के यूआरएल के साथ Base64 कोड में बदला गया हस्ताक्षर जोड़ें. उदाहरण के लिए:

    https://maps.googleapis.com/maps/api/staticmap?center=Z%C3%BCrich&size=400x400&client=YOUR_CLIENT_ID
    &signature=BASE64_SIGNATURE

सर्वर साइड कोड का इस्तेमाल करके, यूआरएल साइनिंग की सुविधा लागू करने के तरीके दिखाने वाले सैंपल के लिए, नीचे दिया गया यूआरएल साइनिंग के लिए सैंपल कोड देखें.

यूआरएल पर हस्ताक्षर के लिए सैंपल कोड

नीचे दिए गए सेक्शन में सर्वर-साइड कोड का इस्तेमाल करके, यूआरएल साइनिंग को लागू करने के तरीके बताए गए हैं. उपयोगकर्ताओं को अपना यूआरएल साइनिंग सीक्रेट सार्वजनिक करने से बचने के लिए, यूआरएल हमेशा सर्वर साइड से साइन किए जाने चाहिए.

Python

नीचे दिए गए उदाहरण में, यूआरएल पर हस्ताक्षर करने के लिए स्टैंडर्ड Python लाइब्रेरी का इस्तेमाल किया गया है. (कोड को डाउनलोड करें.)

#!/usr/bin/python
# -*- coding: utf-8 -*-
""" Signs a URL using a URL signing secret """

import hashlib
import hmac
import base64
import urllib.parse as urlparse


def sign_url(input_url=None, secret=None):
    """ Sign a request URL with a URL signing secret.
      Usage:
      from urlsigner import sign_url
      signed_url = sign_url(input_url=my_url, secret=SECRET)
      Args:
      input_url - The URL to sign
      secret    - Your URL signing secret
      Returns:
      The signed request URL
  """

    if not input_url or not secret:
        raise Exception("Both input_url and secret are required")

    url = urlparse.urlparse(input_url)

    # We only need to sign the path+query part of the string
    url_to_sign = url.path + "?" + url.query

    # Decode the private key into its binary format
    # We need to decode the URL-encoded private key
    decoded_key = base64.urlsafe_b64decode(secret)

    # Create a signature using the private key and the URL-encoded
    # string using HMAC SHA1. This signature will be binary.
    signature = hmac.new(decoded_key, str.encode(url_to_sign), hashlib.sha1)

    # Encode the binary signature into base64 for use within a URL
    encoded_signature = base64.urlsafe_b64encode(signature.digest())

    original_url = url.scheme + "://" + url.netloc + url.path + "?" + url.query

    # Return signed URL
    return original_url + "&signature=" + encoded_signature.decode()


if __name__ == "__main__":
    input_url = input("URL to Sign: ")
    secret = input("URL signing secret: ")
    print("Signed URL: " + sign_url(input_url, secret))

Java

नीचे दिए गए उदाहरण में, JDK 1.8 के बाद से उपलब्ध java.util.Base64 क्लास का इस्तेमाल किया गया है. पुराने वर्शन में Apache कॉमंस या इससे मिलते-जुलते वर्शन का इस्तेमाल करने की ज़रूरत पड़ सकती है. (कोड को डाउनलोड करें.)

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;  // JDK 1.8 only - older versions may need to use Apache Commons or similar.
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class UrlSigner {

  // Note: Generally, you should store your private key someplace safe
  // and read them into your code

  private static String keyString = "YOUR_PRIVATE_KEY";
  
  // The URL shown in these examples is a static URL which should already
  // be URL-encoded. In practice, you will likely have code
  // which assembles your URL from user or web service input
  // and plugs those values into its parameters.
  private static String urlString = "YOUR_URL_TO_SIGN";

  // This variable stores the binary key, which is computed from the string (Base64) key
  private static byte[] key;
  
  public static void main(String[] args) throws IOException,
    InvalidKeyException, NoSuchAlgorithmException, URISyntaxException {
    
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    
    String inputUrl, inputKey = null;

    // For testing purposes, allow user input for the URL.
    // If no input is entered, use the static URL defined above.    
    System.out.println("Enter the URL (must be URL-encoded) to sign: ");
    inputUrl = input.readLine();
    if (inputUrl.equals("")) {
      inputUrl = urlString;
    }
    
    // Convert the string to a URL so we can parse it
    URL url = new URL(inputUrl);
 
    // For testing purposes, allow user input for the private key.
    // If no input is entered, use the static key defined above.   
    System.out.println("Enter the Private key to sign the URL: ");
    inputKey = input.readLine();
    if (inputKey.equals("")) {
      inputKey = keyString;
    }
    
    UrlSigner signer = new UrlSigner(inputKey);
    String request = signer.signRequest(url.getPath(),url.getQuery());
    
    System.out.println("Signed URL :" + url.getProtocol() + "://" + url.getHost() + request);
  }
  
  public UrlSigner(String keyString) throws IOException {
    // Convert the key from 'web safe' base 64 to binary
    keyString = keyString.replace('-', '+');
    keyString = keyString.replace('_', '/');
    System.out.println("Key: " + keyString);
    // Base64 is JDK 1.8 only - older versions may need to use Apache Commons or similar.
    this.key = Base64.getDecoder().decode(keyString);
  }

  public String signRequest(String path, String query) throws NoSuchAlgorithmException,
    InvalidKeyException, UnsupportedEncodingException, URISyntaxException {
    
    // Retrieve the proper URL components to sign
    String resource = path + '?' + query;
    
    // Get an HMAC-SHA1 signing key from the raw key bytes
    SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");

    // Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1 key
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(sha1Key);

    // compute the binary signature for the request
    byte[] sigBytes = mac.doFinal(resource.getBytes());

    // base 64 encode the binary signature
    // Base64 is JDK 1.8 only - older versions may need to use Apache Commons or similar.
    String signature = Base64.getEncoder().encodeToString(sigBytes);
    
    // convert the signature to 'web safe' base 64
    signature = signature.replace('+', '-');
    signature = signature.replace('/', '_');
    
    return resource + "&signature=" + signature;
  }
}

Node JS

नीचे दिए गए उदाहरण में, यूआरएल पर हस्ताक्षर करने के लिए नेटिव नोड मॉड्यूल का इस्तेमाल किया गया है. (कोड को डाउनलोड करें.)

'use strict'

const crypto = require('crypto');
const url = require('url');

/**
 * Convert from 'web safe' base64 to true base64.
 *
 * @param  {string} safeEncodedString The code you want to translate
 *                                    from a web safe form.
 * @return {string}
 */
function removeWebSafe(safeEncodedString) {
  return safeEncodedString.replace(/-/g, '+').replace(/_/g, '/');
}

/**
 * Convert from true base64 to 'web safe' base64
 *
 * @param  {string} encodedString The code you want to translate to a
 *                                web safe form.
 * @return {string}
 */
function makeWebSafe(encodedString) {
  return encodedString.replace(/\+/g, '-').replace(/\//g, '_');
}

/**
 * Takes a base64 code and decodes it.
 *
 * @param  {string} code The encoded data.
 * @return {string}
 */
function decodeBase64Hash(code) {
  // "new Buffer(...)" is deprecated. Use Buffer.from if it exists.
  return Buffer.from ? Buffer.from(code, 'base64') : new Buffer(code, 'base64');
}

/**
 * Takes a key and signs the data with it.
 *
 * @param  {string} key  Your unique secret key.
 * @param  {string} data The url to sign.
 * @return {string}
 */
function encodeBase64Hash(key, data) {
  return crypto.createHmac('sha1', key).update(data).digest('base64');
}

/**
 * Sign a URL using a secret key.
 *
 * @param  {string} path   The url you want to sign.
 * @param  {string} secret Your unique secret key.
 * @return {string}
 */
function sign(path, secret) {
  const uri = url.parse(path);
  const safeSecret = decodeBase64Hash(removeWebSafe(secret));
  const hashedSignature = makeWebSafe(encodeBase64Hash(safeSecret, uri.path));
  return url.format(uri) + '&signature=' + hashedSignature;
}

C#

नीचे दिए गए उदाहरण में, यूआरएल के अनुरोध पर हस्ताक्षर करने के लिए, डिफ़ॉल्ट System.Security.Cryptography लाइब्रेरी का इस्तेमाल किया गया है. ध्यान दें कि हमें यूआरएल का इस्तेमाल करने वाले वर्शन को लागू करने के लिए, डिफ़ॉल्ट Base64 एन्कोडिंग को बदलना होगा. (कोड को डाउनलोड करें.)

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace SignUrl {

  public struct GoogleSignedUrl {

    public static string Sign(string url, string keyString) {
      ASCIIEncoding encoding = new ASCIIEncoding();

      // converting key to bytes will throw an exception, need to replace '-' and '_' characters first.
      string usablePrivateKey = keyString.Replace("-", "+").Replace("_", "/");
      byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);

      Uri uri = new Uri(url);
      byte[] encodedPathAndQueryBytes = encoding.GetBytes(uri.LocalPath + uri.Query);

      // compute the hash
      HMACSHA1 algorithm = new HMACSHA1(privateKeyBytes);
      byte[] hash = algorithm.ComputeHash(encodedPathAndQueryBytes);

      // convert the bytes to string and make url-safe by replacing '+' and '/' characters
      string signature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");
            
      // Add the signature to the existing URI.
      return uri.Scheme+"://"+uri.Host+uri.LocalPath + uri.Query +"&signature=" + signature;
    }
  }

  class Program {

    static void Main() {
    
      // Note: Generally, you should store your private key someplace safe
      // and read them into your code

      const string keyString = "YOUR_PRIVATE_KEY";
  
      // The URL shown in these examples is a static URL which should already
      // be URL-encoded. In practice, you will likely have code
      // which assembles your URL from user or web service input
      // and plugs those values into its parameters.
      const  string urlString = "YOUR_URL_TO_SIGN";
      
      string inputUrl = null;
      string inputKey = null;
    
      Console.WriteLine("Enter the URL (must be URL-encoded) to sign: ");
      inputUrl = Console.ReadLine();
      if (inputUrl.Length == 0) {
        inputUrl = urlString;
      }     
    
      Console.WriteLine("Enter the Private key to sign the URL: ");
      inputKey = Console.ReadLine();
      if (inputKey.Length == 0) {
        inputKey = keyString;
      }
      
      Console.WriteLine(GoogleSignedUrl.Sign(inputUrl,inputKey));
    }
  }
}

दूसरी भाषाओं के उदाहरण

url-signing प्रोजेक्ट में, ऐसे उदाहरण उपलब्ध हैं जिनमें ज़्यादा भाषाओं को शामिल किया गया है.

समस्या हल करना

अगर अनुरोध में अमान्य हस्ताक्षर शामिल है, तो एपीआई HTTP 403 (Forbidden) गड़बड़ी दिखाता है. यह गड़बड़ी सबसे ज़्यादा तब होती है, जब इस्तेमाल किया गया साइनिंग सीक्रेट, पास किए गए क्लाइंट आईडी से लिंक न किया गया हो या साइन इन करने से पहले, बिना ASCII वाले इनपुट को यूआरएल कोड में न बदला गया हो.

इस समस्या को हल करने के लिए, अनुरोध का यूआरएल कॉपी करें, signature क्वेरी पैरामीटर हटाएं, और नीचे दिए गए निर्देशों का पालन करके मान्य हस्ताक्षर फिर से जनरेट करें:

नीचे दिए गए यूआरएल पर अभी हस्ताक्षर करें विजेट का इस्तेमाल करके, अपने क्लाइंट आईडी से डिजिटल हस्ताक्षर जनरेट करने के लिए:

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