when initializing slack, stash token in driver
parent
5f21098fdc
commit
fbd151f9ef
35
main.go
35
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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue