डिजिटल हस्ताक्षर का इस्तेमाल करना

एपीआई पासकोड का इस्तेमाल करके, अपने अनुरोध पर डिजिटल तौर पर हस्ताक्षर करें

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

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

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

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

अहस्ताक्षरित अनुरोधों को सीमित करें

यह पक्का करने के लिए कि आपकी एपीआई पासकोड सिर्फ़ साइन किए गए अनुरोध स्वीकार करे:

  1. Cloud Console में Google Maps Platform कोटा पेज पर जाएं.
  2. प्रोजेक्ट ड्रॉप-डाउन पर क्लिक करें और वही प्रोजेक्ट चुनें जिसका इस्तेमाल आपने अपने ऐप्लिकेशन या साइट के लिए एपीआई पासकोड बनाते समय किया था.
  3. एपीआई के ड्रॉप-डाउन से Maps स्टैटिक एपीआई चुनें.
  4. बिना हस्ताक्षर वाले अनुरोध सेक्शन को बड़ा करें.
  5. कोटा का नाम टेबल में, उस कोटा के बगल में मौजूद 'बदलाव करें' बटन पर क्लिक करें जिसमें आपको बदलाव करना है. उदाहरण के लिए, हर दिन बिना हस्ताक्षर वाले अनुरोध.
  6. कोटा सीमा में बदलाव करें पैनल में कोटे की सीमा अपडेट करें.
  7. सेव करें को चुनें.

शुरू करने से पहले

शुरू करने से पहले, अपने स्टैटिक मैप वाले वेब पेज के यूआरएल का पता लगाएं. इस यूआरएल पर डिजिटल हस्ताक्षर किए जाते हैं.

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

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

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

अपने प्रोजेक्ट के यूआरएल का साइनिंग सीक्रेट पाने के लिए:

  1. Cloud Console में, Google Maps Platform के क्रेडेंशियल पेज पर जाएं.
  2. प्रोजेक्ट ड्रॉप-डाउन चुनें और वही प्रोजेक्ट चुनें जिसका इस्तेमाल आपने Maps Static API के लिए एपीआई पासकोड बनाते समय किया था.
  3. नीचे की ओर स्क्रोल करके, सीक्रेट जनरेटर कार्ड पर जाएं. मौजूदा सीक्रेट फ़ील्ड में आपका मौजूदा यूआरएल साइनिंग सीक्रेट होता है.
  4. इस पेज पर अभी यूआरएल पर हस्ताक्षर करें विजेट की सुविधा भी मौजूद है. यह विजेट आपको, अपने मौजूदा साइनिंग सीक्रेट का इस्तेमाल करके, Maps के स्टैटिक एपीआई अनुरोध पर अपने-आप साइन करने की सुविधा देता है. यूआरएल को ऐक्सेस करने के लिए, नीचे की ओर स्क्रोल करके यूआरएल पर अभी हस्ताक्षर करें कार्ड पर जाएं.

नया यूआरएल साइनिंग सीक्रेट पाने के लिए, सीक्रेट फिर से जनरेट करें को चुनें. नया सीक्रेट जनरेट करने के 24 घंटे बाद, पुराना सीक्रेट खत्म हो जाएगा. 24 घंटे बीत जाने के बाद, पुराने सीक्रेट वाले अनुरोध काम नहीं करेंगे.

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

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

यूआरएल के मान्य वर्णों की खास जानकारी
सेट करेंवर्णयूआरएल का इस्तेमाल
अक्षर और अंक दोनों शामिल हो सकते हैं ए b c d x x x CANNOT TRANSLATE टेक्स्ट स्ट्रिंग, स्कीम का इस्तेमाल (http), पोर्ट (8080) वगैरह.
गैर-आरक्षित - _ . ~ टेक्स्ट स्ट्रिंग
बुकिंग की गई ! * ' ( ) ; : @ & = + $ , / ? % # [ ] कंट्रोल वर्ण और/या टेक्स्ट स्ट्रिंग

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

बिना हस्ताक्षर वाला अनुरोध करने वाला यूआरएल बनाएं. निर्देशों के लिए, डेवलपर का यह दस्तावेज़ देखें:

key पैरामीटर में, एपीआई पासकोड को ज़रूर शामिल करें. उदाहरण के लिए:

https://maps.googleapis.com/maps/api/staticmap?center=Z%C3%BCrich&zoom=12&size=400x400&key=YOUR_API_KEY

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

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

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

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

https://maps.googleapis.com/maps/api/staticmap?center=Z%C3%BCrich&zoom=12&size=400x400&key=YOUR_API_KEY
&signature=BASE64_SIGNATURE
'यूआरएल पर हस्ताक्षर करें' विजेट का इस्तेमाल करना

Google Cloud Console में मौजूद, यूआरएल पर अभी हस्ताक्षर करें विजेट की मदद से, एपीआई पासकोड से डिजिटल हस्ताक्षर जनरेट करने के लिए:

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

यूआरएल पर अभी हस्ताक्षर करें विजेट की तुलना में, सर्वर-साइड से डिजिटल हस्ताक्षर जनरेट करते समय, आपको कुछ और कार्रवाइयां करनी होंगी:

  1. प्रोटोकॉल स्कीम को हटाएं और यूआरएल के होस्ट वाले हिस्सों को सिर्फ़ पाथ और क्वेरी छोड़ दें:

  2. /maps/api/staticmap?center=Z%C3%BCrich&zoom=12&size=400x400&key=YOUR_API_KEY
    
  3. दिखाए गए यूआरएल पर हस्ताक्षर करने के सीक्रेट को यूआरएल के लिए, बदले गए Base64 से एन्कोड किया गया है.

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

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

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

    https://maps.googleapis.com/maps/api/staticmap?center=Z%C3%BCrich&zoom=12&size=400x400&key=YOUR_API_KEY
    &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 क्वेरी पैरामीटर को हटाएं, और नीचे दिए गए निर्देशों का पालन करके फिर से मान्य हस्ताक्षर जनरेट करें:

Google Cloud Console में मौजूद, यूआरएल पर अभी हस्ताक्षर करें विजेट की मदद से, एपीआई पासकोड से डिजिटल हस्ताक्षर जनरेट करने के लिए:

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