From fbd151f9ef8d1a783add9f833fa7f0f4798a7e6b Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:07:43 -0600 Subject: [PATCH] when initializing slack, stash token in driver --- main.go | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 4db2c4d..b5ef585 100644 --- a/main.go +++ b/main.go @@ -196,24 +196,33 @@ func basicAuth(cfg Config, w http.ResponseWriter, r *http.Request) bool { func newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc { if cfg.InitializeSlack { - return handlerPostAPIV1EventsSlackInitialize + return handlerPostAPIV1EventsSlackInitialize(cfg) } return _newHandlerPostAPIV1EventsSlack(cfg) } -func handlerPostAPIV1EventsSlackInitialize(w http.ResponseWriter, r *http.Request) { - b, _ := io.ReadAll(r.Body) - var challenge struct { - Token string - Challenge string - Type string +func handlerPostAPIV1EventsSlackInitialize(cfg Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + var challenge struct { + Token string + Challenge string + Type string + } + if err := json.Unmarshal(b, &challenge); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + if _, err := cfg.driver.ExecContext(r.Context(), ` + CREATE TABLE IF NOT EXISTS initialization (label TEXT, token TEXT, updated TIMESTAMP); + INSERT INTO initialization (token, updated) VALUES ('slack_events_webhook_token', $1, $2); + `, challenge.Token, time.Now().UTC()); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + log.Println("stashed new slack initialization token") + encodeResponse(w, r, map[string]any{"challenge": challenge.Challenge}) } - if err := json.Unmarshal(b, &challenge); err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - - encodeResponse(w, r, map[string]any{"challenge": challenge.Challenge}) } func _newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc {