Created
January 22, 2020 01:42
-
-
Save yogin/b93b0749d7c7174bb1b843b01d935f2d to your computer and use it in GitHub Desktop.
Hubspot API signatures in 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 main | |
| import ( | |
| "crypto/sha256" | |
| "encoding/hex" | |
| "fmt" | |
| ) | |
| // Request ... | |
| type Request struct { | |
| Method string | |
| URL string | |
| Secret string | |
| Body string | |
| ExpectedV1 string | |
| ExpectedV2 string | |
| } | |
| func main() { | |
| r := &Request{ | |
| Method: "GET", | |
| URL: "https://www.example.com/webhook_uri", | |
| Secret: "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy", | |
| Body: "[{\"eventId\":1,\"subscriptionId\":12345,\"portalId\":62515,\"occurredAt\":1564113600000,\"subscriptionType\":\"contact.creation\",\"attemptNumber\":0,\"objectId\":123,\"changeSource\":\"CRM\",\"changeFlag\":\"NEW\",\"appId\":54321}]", | |
| ExpectedV1: "232db2615f3d666fe21a8ec971ac7b5402d33b9a925784df3ca654d05f4817de", | |
| ExpectedV2: "eee2dddcc73c94d699f5e395f4b9d454a069a6855fbfa152e91e88823087200e", | |
| } | |
| r.sigv1() | |
| r.sigv2() | |
| } | |
| // https://developers.hubspot.com/docs/faq/v1-request-validation | |
| func (r *Request) sigv1() { | |
| fmt.Println("Signature v1") | |
| s := r.Secret + r.Body | |
| r1 := sha256.Sum256([]byte(s)) | |
| r2 := hex.EncodeToString(r1[:]) | |
| fmt.Printf("Expected: %s\n", r.ExpectedV1) | |
| fmt.Printf(" Got: %s\n", r2) | |
| } | |
| // https://developers.hubspot.com/docs/faq/v2-request-validation | |
| func (r *Request) sigv2() { | |
| fmt.Println("Signature v2") | |
| s := r.Secret + r.Method + r.URL | |
| r1 := sha256.Sum256([]byte(s)) | |
| r2 := hex.EncodeToString(r1[:]) | |
| fmt.Printf("Expected: %s\n", r.ExpectedV2) | |
| fmt.Printf(" Got: %s\n", r2) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ go run main.go Signature v1 Expected: 232db2615f3d666fe21a8ec971ac7b5402d33b9a925784df3ca654d05f4817de Got: 232db2615f3d666fe21a8ec971ac7b5402d33b9a925784df3ca654d05f4817de Signature v2 Expected: eee2dddcc73c94d699f5e395f4b9d454a069a6855fbfa152e91e88823087200e Got: eee2dddcc73c94d699f5e395f4b9d454a069a6855fbfa152e91e88823087200e