Last active
August 29, 2015 14:02
-
-
Save jeisenberg/8cf24a92add2a4fd4844 to your computer and use it in GitHub Desktop.
hash.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib | |
import ( | |
"appengine" | |
"appengine/urlfetch" | |
"config" | |
"crypto/hmac" | |
"crypto/sha1" | |
// "encoding/base64" | |
"encoding/hex" | |
"log" | |
"net/url" | |
"strconv" | |
"strings" | |
"time" | |
) | |
const EmailUrl = "/page/api/cons/email_register" | |
const APIVersion = "2" | |
type BSD struct{} | |
// Parameters: | |
// cons_id (optional) | |
// The numeric id of the constituent to add the email to. If not supplied, a new constituent will be created. | |
// email (required) | |
// The email address to register or update. | |
// is_subscribed (optional) | |
// Set the email to subscribed or not (1 or 0) | |
// guid (optional) | |
// The guid to look up the constituent by. | |
// format (optional) | |
// The format of the response, either json or xml. Defaults to xml. | |
func (b *BSD) PostEmailSignUp(email string, c appengine.Context) error { | |
timestamp := strconv.FormatInt(time.Now().Unix(), 10) | |
log.Printf("timstamp%s", timestamp) | |
urlValues := url.Values{} | |
urlValues.Set("api_ts", timestamp) | |
urlValues.Set("api_ver", APIVersion) | |
urlValues.Set("api_id", config.Env["bsd"]["api_id"]) | |
urlValues.Set("email", email) | |
//urlValues.Set("format", "json") | |
apiMac := makeApiMac(urlValues) | |
urlValues.Set("api_mac", apiMac) | |
log.Printf("%s", urlValues) | |
client := urlfetch.Client(c) | |
finalUrl := strings.Join([]string{createUrl(EmailUrl), urlValues.Encode()}, "?") | |
log.Printf("%s", finalUrl) | |
log.Printf("%s", client) | |
resp, err := client.Get(finalUrl) | |
if err != nil { | |
return err | |
} | |
log.Printf("%s", resp) | |
return nil | |
} | |
func createUrl(url string) string { | |
return strings.Join([]string{config.Env["bsd"]["root_url"], url}, "") | |
} | |
func makeApiMac(params url.Values) string { | |
secret := []byte(config.Env["bsd"]["api_secret"]) | |
h := hmac.New(sha1.New, secret) | |
signingString := makeSigningString(params) | |
log.Printf("%s", signingString) | |
h.Write([]byte(signingString)) | |
log.Print("%s", h.Sum(nil)) | |
//encoded := base64.StdEncoding.EncodeToString(h.Sum(nil)) | |
encoded := hex.EncodeToString(h.Sum(nil)) | |
log.Printf("%s", encoded) | |
return encoded | |
} | |
func makeSigningString(params url.Values) string { | |
valuesString := params.Encode() | |
appId := config.Env["bsd"]["api_id"] | |
signingString := strings.Join([]string{appId, params.Get("api_ts"), EmailUrl, valuesString}, "\n") | |
log.Printf("%s", signingString) | |
return signingString | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment