when initializing slack, stash token in driver

main
Bel LaPointe 2024-04-17 16:07:43 -06:00
parent 5f21098fdc
commit fbd151f9ef
1 changed files with 22 additions and 13 deletions

15
main.go
View File

@ -196,12 +196,13 @@ 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) {
func handlerPostAPIV1EventsSlackInitialize(cfg Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
var challenge struct {
Token string
@ -212,9 +213,17 @@ func handlerPostAPIV1EventsSlackInitialize(w http.ResponseWriter, r *http.Reques
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})
}
}
func _newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {