-
Notifications
You must be signed in to change notification settings - Fork 1
/
superpop.go
77 lines (65 loc) · 2.19 KB
/
superpop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package TwitterIOT
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)
// ComputeHmac256 returns the CRC check token string
func ComputeHmac256(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
// TwitterResponse represents the json response required for twitter CRC
type TwitterResponse struct {
ResponseToken string `json:"response_token"`
}
// GetCRCResponse returns the Json object required for Twitter CRC check
// Parameters
// message (string) - message from twitter
// secret (string) - Twitter consumer secret
//
// Example JSON response (from twitter docs):
// {
// "response_token": "sha256=x0mYd8hz2goCTfcNAaMqENy2BFgJJfJOb4PdvTffpwg="
// }
//
func GetCRCResponse(message string, secret string, w http.ResponseWriter) error {
s := ComputeHmac256(message, secret)
tr := &TwitterResponse{
ResponseToken: "sha256=" + s,
}
// encode the response as json and send to web output stream, in this case: ResponseWriter
encoder := json.NewEncoder(w)
if err := encoder.Encode(tr); err != nil {
return err
}
return nil
}
// testCRCResponse is a quick test function for the CRC check required for twitter.
//
// URL: /app/test/crt?message=a9sd87f98s6a7f
func testCRCResponse(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
if r.Method == http.MethodGet {
// this should be the consumer secret that is provided to the twitter account
secret := "89s76d9fasdasdf"
// messsage is pulled from the URL query but twitter will provide it as a JSON object that
// needs to be decoded
message := r.URL.Query().Get("message")
// if there is an error decoding we should log it
err := GetCRCResponse(message, secret, w)
if err != nil {
log.Errorf(ctx, "there was an error encoding the response %s", err)
}
} else {
// Twitter will make a call to the webhook endpoint in the form of a POST for actual data
// but will call the same endpoint using GET to do the CRC check
log.Debugf(ctx, "Method is not GET it is %s", r.Method)
}
}