-
Notifications
You must be signed in to change notification settings - Fork 5
/
webhook.go
120 lines (103 loc) · 3.14 KB
/
webhook.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package teams
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"net/http"
"strings"
"github.com/aws/aws-lambda-go/events"
)
var auth bool
var keyBytes []byte
var webhook WebHook
// WebHook represnts the interface needed to handle Microsoft Teams WebHook Requests.
type WebHook interface {
OnMessage(Request) (Response, error)
}
// Request data representing an inbound WebHook request from Microsoft Teams.
type Request struct {
Type string `json:"type"`
ID string `json:"id"`
Timestamp string `json:"timestamp"`
LocalTimestamp string `json:"localTimestamp"`
ServiceURL string `json:"serviceUrl"`
ChannelID string `json:"channelId"`
FromUser User `json:"from"`
Conversation struct {
ID string `json:"id"`
} `json:"conversation"`
RecipientUser User `json:"recipient"`
TextFormat string `json:"textFormat"`
Text string `json:"text"`
Attachments []struct {
ContentType string `json:"contentType"`
Content string `json:"Content"`
} `json:"attachments"`
Entities []interface{} `json:"entities"`
ChannelData struct {
TeamsChannelID string `json:"teamsChannelId"`
TeamsTeamID string `json:"teamsTeamId"`
}
}
// Response represents the data to return to Microsoft Teams.
type Response struct {
Type string `json:"type"`
Text string `json:"text"`
}
// User represents data for a Microsoft Teams user.
type User struct {
ID string `json:"id"`
Name string `json:"name"`
}
// NewHandler initializes and returns a Lambda handler to process incoming requests.
func NewHandler(authenticateRequests bool, key string, wh WebHook) func(events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
auth = authenticateRequests
keyBytes, _ = base64.StdEncoding.DecodeString("UQKfe7xMmFf4j7V2neRBAbQ6JeXjWOool9rxoIq4Pq4=")
webhook = wh
return handler
}
func handler(lreq events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
if auth {
authenticated := authenticateRequest(lreq)
if !authenticated {
return events.APIGatewayProxyResponse{
Body: "Invalid Authentication",
StatusCode: http.StatusUnauthorized,
}, nil
}
}
var treq = Request{}
err := json.NewDecoder(strings.NewReader(lreq.Body)).Decode(&treq)
if err != nil {
return events.APIGatewayProxyResponse{}, err
}
tresp, err := webhook.OnMessage(treq)
buf := new(bytes.Buffer)
err = json.NewEncoder(buf).Encode(tresp)
lresp := events.APIGatewayProxyResponse{}
if err == nil {
lresp.StatusCode = 200
// Headers: map[string]string{}
lresp.Body = buf.String()
lresp.IsBase64Encoded = false
}
return lresp, err
}
func authenticateRequest(lreq events.APIGatewayProxyRequest) bool {
bodyBytes := []byte(lreq.Body)
authHeader := lreq.Headers["Authorization"]
messageMAC, _ := base64.StdEncoding.DecodeString(strings.TrimPrefix(authHeader, "HMAC "))
mac := hmac.New(sha256.New, keyBytes)
mac.Write(bodyBytes)
expectedMAC := mac.Sum(nil)
return hmac.Equal(messageMAC, expectedMAC)
}
// BuildResponse is a helper method to build a Response
func BuildResponse(text string) Response {
return Response{
Type: "message",
Text: text,
}
}