github.com/korniux/gophish@v0.9.0/controllers/api/webhook.go (about) 1 package api 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "strconv" 7 8 log "github.com/gophish/gophish/logger" 9 "github.com/gophish/gophish/models" 10 "github.com/gophish/gophish/webhook" 11 "github.com/gorilla/mux" 12 ) 13 14 // Webhooks returns a list of webhooks, both active and disabled 15 func (as *Server) Webhooks(w http.ResponseWriter, r *http.Request) { 16 switch { 17 case r.Method == "GET": 18 whs, err := models.GetWebhooks() 19 if err != nil { 20 log.Error(err) 21 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) 22 return 23 } 24 JSONResponse(w, whs, http.StatusOK) 25 26 case r.Method == "POST": 27 wh := models.Webhook{} 28 err := json.NewDecoder(r.Body).Decode(&wh) 29 if err != nil { 30 JSONResponse(w, models.Response{Success: false, Message: "Invalid JSON structure"}, http.StatusBadRequest) 31 return 32 } 33 err = models.PostWebhook(&wh) 34 if err != nil { 35 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 36 return 37 } 38 JSONResponse(w, wh, http.StatusCreated) 39 } 40 } 41 42 // Webhook returns details of a single webhook specified by "id" parameter 43 func (as *Server) Webhook(w http.ResponseWriter, r *http.Request) { 44 vars := mux.Vars(r) 45 id, _ := strconv.ParseInt(vars["id"], 0, 64) 46 wh, err := models.GetWebhook(id) 47 if err != nil { 48 JSONResponse(w, models.Response{Success: false, Message: "Webhook not found"}, http.StatusNotFound) 49 return 50 } 51 switch { 52 case r.Method == "GET": 53 JSONResponse(w, wh, http.StatusOK) 54 55 case r.Method == "DELETE": 56 err = models.DeleteWebhook(id) 57 if err != nil { 58 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) 59 return 60 } 61 log.Infof("Deleted webhook with id: %d", id) 62 JSONResponse(w, models.Response{Success: true, Message: "Webhook deleted Successfully!"}, http.StatusOK) 63 64 case r.Method == "PUT": 65 wh2 := models.Webhook{} 66 err = json.NewDecoder(r.Body).Decode(&wh2) 67 wh2.Id = id 68 err = models.PutWebhook(&wh2) 69 if err != nil { 70 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 71 return 72 } 73 JSONResponse(w, wh2, http.StatusOK) 74 } 75 } 76 77 // ValidateWebhook makes an HTTP request to a specified remote url to ensure that it's valid. 78 func (as *Server) ValidateWebhook(w http.ResponseWriter, r *http.Request) { 79 type validationEvent struct { 80 Success bool `json:"success"` 81 } 82 switch { 83 case r.Method == "POST": 84 vars := mux.Vars(r) 85 id, _ := strconv.ParseInt(vars["id"], 0, 64) 86 wh, err := models.GetWebhook(id) 87 if err != nil { 88 log.Error(err) 89 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) 90 return 91 } 92 payload := validationEvent{Success: true} 93 err = webhook.Send(webhook.EndPoint{URL: wh.URL, Secret: wh.Secret}, payload) 94 if err != nil { 95 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 96 return 97 } 98 JSONResponse(w, wh, http.StatusOK) 99 } 100 }