Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Google HMAC auth #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions google_geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package geo

import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -44,6 +47,9 @@ var googleGeocodeURL = "https://maps.googleapis.com/maps/api/geocode/json"

var GoogleAPIKey = ""

var GoogleClientId = ""
var GooglePrivateKey []byte

// Note: In the next major revision (1.0.0), it is planned
// That Geocoders should adhere to the `geo.Geocoder`
// interface and provide versioning of APIs accordingly.
Expand All @@ -56,6 +62,19 @@ func SetGoogleAPIKey(newAPIKey string) {
GoogleAPIKey = newAPIKey
}

func SetGoogleClientId(newClientId string) {
GoogleClientId = newClientId
}

func SetGooglePrivateKey(newBase64PrivateKey string) error {
privateKey, err := base64.URLEncoding.DecodeString(newBase64PrivateKey)
if err != nil {
return err
}
GooglePrivateKey = privateKey
return nil
}

// Issues a request to the google geocoding service and forwards the passed in params string
// as a URL-encoded entity. Returns an array of byes as a result, or an error if one occurs during the process.
// Note: Since this is an arbitrary request, you are responsible for passing in your API key if you want one.
Expand Down Expand Up @@ -130,6 +149,15 @@ func googleGeocodeQueryStr(address string) (string, error) {
if err != nil {
return "", err
}
} else if GoogleClientId != "" && len(GooglePrivateKey) != 0 {
queryStr.WriteString(fmt.Sprintf("&client=%s", GoogleClientId))

signature, signatureErr := calculateSignature(string(queryStr.Bytes()))
if signatureErr != nil {
return "", err
}

queryStr.WriteString(fmt.Sprintf("&signature=%s", signature))
}

return queryStr.String(), err
Expand Down Expand Up @@ -173,7 +201,34 @@ func googleReverseGeocodeQueryStr(p *Point) (string, error) {
if err != nil {
return "", err
}
} else if GoogleClientId != "" && len(GooglePrivateKey) != 0 {
queryStr.WriteString(fmt.Sprintf("&client=%s", GoogleClientId))

signature, signatureErr := calculateSignature(string(queryStr.Bytes()))
if signatureErr != nil {
return "", err
}

queryStr.WriteString(fmt.Sprintf("&signature=%s", signature))
}

return queryStr.String(), err
}

func calculateSignature(queryString string) (signature string, err error) {
url, parseErr := url.Parse(googleGeocodeURL)
if parseErr != nil {
return "", parseErr
}

var pathAndQuery bytes.Buffer
pathAndQuery.WriteString(url.Path)
pathAndQuery.WriteString("?")
pathAndQuery.WriteString("sensor=false&") //is added in the Request method, so we have to supply it in our string to hash
pathAndQuery.WriteString(queryString)

h := hmac.New(sha1.New, GooglePrivateKey)
h.Write(pathAndQuery.Bytes())

return base64.URLEncoding.EncodeToString(h.Sum(nil)), nil
}