POST /api/v1/events/slack asserts .token==$SLACK_TOKEN

main
Bel LaPointe 2024-04-11 17:11:43 -06:00
parent 2372fa8bb9
commit 6a6524692a
2 changed files with 16 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import (
type Config struct {
Port int
InitializeSlack bool
SlackToken string
}
func newConfig() (Config, error) {

15
main.go
View File

@ -84,6 +84,7 @@ func handlerPostAPIV1EventsSlackInitialize(w http.ResponseWriter, r *http.Reques
}
if err := json.Unmarshal(b, &challenge); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
json.NewEncoder(w).Encode(map[string]any{"challenge": challenge.Challenge})
@ -91,6 +92,20 @@ func handlerPostAPIV1EventsSlackInitialize(w http.ResponseWriter, r *http.Reques
func _newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
r.Body = io.NopCloser(bytes.NewReader(b))
var token struct {
Token string
}
if err := json.Unmarshal(b, &token); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
} else if token.Token != cfg.SlackToken {
http.Error(w, "invalid .token", http.StatusForbidden)
return
}
http.Error(w, "not impl", http.StatusNotImplemented)
}
}