Debian Codegraph @

slackevents/parsers.go

raw symbols SCIP
1 package slackevents
2
3 import (
4 "crypto/subtle"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "reflect"
9
10 "github.com/slack-go/slack"
11 )
12
13 // eventsMap checks both slack.EventsMapping and
14 // and slackevents.EventsAPIInnerEventMapping. If the event
15 // exists, returns the unmarshalled struct instance of
16 // target for the matching event type.
17 // TODO: Consider moving all events into its own package?
18 func eventsMap(t string) (interface{}, bool) {
19 // Must parse EventsAPI FIRST as both RTM and EventsAPI
20 // have a type: "Message" event.
21 // TODO: Handle these cases more explicitly.
22 v, exists := EventsAPIInnerEventMapping[EventsAPIType(t)]
23 if exists {
24 return v, exists
25 }
26 v, exists = slack.EventMapping[t]
27 if exists {
28 return v, exists
29 }
30 return v, exists
31 }
32
33 func parseOuterEvent(rawE json.RawMessage) (EventsAPIEvent, error) {
34 e := &EventsAPIEvent{}
35 err := json.Unmarshal(rawE, e)
36 if err != nil {
37 return EventsAPIEvent{
38 "",
39 "",
40 "unmarshalling_error",
41 "",
42 "",
43 &slack.UnmarshallingErrorEvent{ErrorObj: err},
44 EventsAPIInnerEvent{},
45 }, err
46 }
47 if e.Type == CallbackEvent {
48 cbEvent := &EventsAPICallbackEvent{}
49 err = json.Unmarshal(rawE, cbEvent)
50 if err != nil {
51 return EventsAPIEvent{
52 "",
53 "",
54 "unmarshalling_error",
55 "",
56 "",
57 &slack.UnmarshallingErrorEvent{ErrorObj: err},
58 EventsAPIInnerEvent{},
59 }, err
60 }
61 return EventsAPIEvent{
62 e.Token,
63 e.TeamID,
64 e.Type,
65 e.APIAppID,
66 e.EnterpriseID,
67 cbEvent,
68 EventsAPIInnerEvent{},
69 }, nil
70 }
71 urlVE := &EventsAPIURLVerificationEvent{}
72 err = json.Unmarshal(rawE, urlVE)
73 if err != nil {
74 return EventsAPIEvent{
75 "",
76 "",
77 "unmarshalling_error",
78 "",
79 "",
80 &slack.UnmarshallingErrorEvent{ErrorObj: err},
81 EventsAPIInnerEvent{},
82 }, err
83 }
84 return EventsAPIEvent{
85 e.Token,
86 e.TeamID,
87 e.Type,
88 e.APIAppID,
89 e.EnterpriseID,
90 urlVE,
91 EventsAPIInnerEvent{},
92 }, nil
93 }
94
95 func parseInnerEvent(e *EventsAPICallbackEvent) (EventsAPIEvent, error) {
96 iE := &slack.Event{}
97 rawInnerJSON := e.InnerEvent
98 err := json.Unmarshal(*rawInnerJSON, iE)
99 if err != nil {
100 return EventsAPIEvent{
101 e.Token,
102 e.TeamID,
103 "unmarshalling_error",
104 e.APIAppID,
105 "",
106 &slack.UnmarshallingErrorEvent{ErrorObj: err},
107 EventsAPIInnerEvent{},
108 }, err
109 }
110 v, exists := eventsMap(iE.Type)
111 if !exists {
112 return EventsAPIEvent{
113 e.Token,
114 e.TeamID,
115 iE.Type,
116 e.APIAppID,
117 "",
118 nil,
119 EventsAPIInnerEvent{},
120 }, fmt.Errorf("Inner Event does not exist! %s", iE.Type)
121 }
122 t := reflect.TypeOf(v)
123 recvEvent := reflect.New(t).Interface()
124 err = json.Unmarshal(*rawInnerJSON, recvEvent)
125 if err != nil {
126 return EventsAPIEvent{
127 e.Token,
128 e.TeamID,
129 "unmarshalling_error",
130 e.APIAppID,
131 "",
132 &slack.UnmarshallingErrorEvent{ErrorObj: err},
133 EventsAPIInnerEvent{},
134 }, err
135 }
136 return EventsAPIEvent{
137 e.Token,
138 e.TeamID,
139 e.Type,
140 e.APIAppID,
141 "",
142 e,
143 EventsAPIInnerEvent{iE.Type, recvEvent},
144 }, nil
145 }
146
147 type Config struct {
148 VerificationToken string
149 TokenVerified bool
150 }
151
152 type Option func(cfg *Config)
153
154 type verifier interface {
155 Verify(token string) bool
156 }
157
158 func OptionVerifyToken(v verifier) Option {
159 return func(cfg *Config) {
160 cfg.TokenVerified = v.Verify(cfg.VerificationToken)
161 }
162 }
163
164 // OptionNoVerifyToken skips the check of the Slack verification token
165 func OptionNoVerifyToken() Option {
166 return func(cfg *Config) {
167 cfg.TokenVerified = true
168 }
169 }
170
171 type TokenComparator struct {
172 VerificationToken string
173 }
174
175 func (c TokenComparator) Verify(t string) bool {
176 return subtle.ConstantTimeCompare([]byte(c.VerificationToken), []byte(t)) == 1
177 }
178
179 // ParseEvent parses the outer and inner events (if applicable) of an events
180 // api event returning a EventsAPIEvent type. If the event is a url_verification event,
181 // the inner event is empty.
182 func ParseEvent(rawEvent json.RawMessage, opts ...Option) (EventsAPIEvent, error) {
183 e, err := parseOuterEvent(rawEvent)
184 if err != nil {
185 return EventsAPIEvent{}, err
186 }
187
188 cfg := &Config{}
189 cfg.VerificationToken = e.Token
190 for _, opt := range opts {
191 opt(cfg)
192 }
193
194 if !cfg.TokenVerified {
195 return EventsAPIEvent{}, errors.New("Invalid verification token")
196 }
197
198 if e.Type == CallbackEvent {
199 cbEvent := e.Data.(*EventsAPICallbackEvent)
200 innerEvent, err := parseInnerEvent(cbEvent)
201 if err != nil {
202 err := fmt.Errorf("EventsAPI Error parsing inner event: %s, %s", innerEvent.Type, err)
203 return EventsAPIEvent{
204 "",
205 "",
206 "unmarshalling_error",
207 "",
208 "",
209 &slack.UnmarshallingErrorEvent{ErrorObj: err},
210 EventsAPIInnerEvent{},
211 }, err
212 }
213 return innerEvent, nil
214 }
215 urlVerificationEvent := &EventsAPIURLVerificationEvent{}
216 err = json.Unmarshal(rawEvent, urlVerificationEvent)
217 if err != nil {
218 return EventsAPIEvent{
219 "",
220 "",
221 "unmarshalling_error",
222 "",
223 "",
224 &slack.UnmarshallingErrorEvent{ErrorObj: err},
225 EventsAPIInnerEvent{},
226 }, err
227 }
228 return EventsAPIEvent{
229 e.Token,
230 e.TeamID,
231 e.Type,
232 e.APIAppID,
233 e.EnterpriseID,
234 urlVerificationEvent,
235 EventsAPIInnerEvent{},
236 }, nil
237 }
238
239 func ParseActionEvent(payloadString string, opts ...Option) (MessageAction, error) {
240 byteString := []byte(payloadString)
241 action := MessageAction{}
242 err := json.Unmarshal(byteString, &action)
243 if err != nil {
244 return MessageAction{}, errors.New("MessageAction unmarshalling failed")
245 }
246
247 cfg := &Config{}
248 cfg.VerificationToken = action.Token
249 for _, opt := range opts {
250 opt(cfg)
251 }
252
253 if !cfg.TokenVerified {
254 return MessageAction{}, errors.New("invalid verification token")
255 } else {
256 return action, nil
257 }
258 }