summaryrefslogtreecommitdiff
path: root/api/guestbook.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/guestbook.go')
-rw-r--r--api/guestbook.go69
1 files changed, 8 insertions, 61 deletions
diff --git a/api/guestbook.go b/api/guestbook.go
index 7b84f45..ee3c79a 100644
--- a/api/guestbook.go
+++ b/api/guestbook.go
@@ -1,8 +1,6 @@
package api
import (
- "encoding/json"
- "fmt"
"log"
"net/http"
"strings"
@@ -43,16 +41,11 @@ func SignGuestbookContinuation(context *RequestContext, req *http.Request, resp
return func(success Continuation, failure Continuation) ContinuationChain {
name := req.FormValue("name")
message := req.FormValue("message")
- hCaptchaResponse := req.FormValue("h-captcha-response")
formErrors := FormError{
Errors: []string{},
}
- if hCaptchaResponse == "" {
- formErrors.Errors = append(formErrors.Errors, "hCaptcha is required")
- }
-
entry := &database.GuestbookEntry{
ID: utils.RandomId(),
Name: name,
@@ -60,22 +53,19 @@ func SignGuestbookContinuation(context *RequestContext, req *http.Request, resp
}
formErrors.Errors = append(formErrors.Errors, validateGuestbookEntry(entry)...)
- err := verifyHCaptcha(context.Args.HcaptchaSecret, hCaptchaResponse)
- if err != nil {
- log.Println(err)
-
- formErrors.Errors = append(formErrors.Errors, "hCaptcha verification failed")
+ if len(formErrors.Errors) == 0 {
+ _, err := database.SaveGuestbookEntry(context.DBConn, entry)
+ if err != nil {
+ log.Println(err)
+ formErrors.Errors = append(formErrors.Errors, "failed to save entry")
+ }
}
+
if len(formErrors.Errors) > 0 {
(*context.TemplateData)["FormError"] = formErrors
(*context.TemplateData)["EntryForm"] = entry
- return failure(context, req, resp)
- }
+ resp.WriteHeader(http.StatusBadRequest)
- _, err = database.SaveGuestbookEntry(context.DBConn, entry)
- if err != nil {
- log.Println(err)
- resp.WriteHeader(http.StatusInternalServerError)
return failure(context, req, resp)
}
@@ -96,46 +86,3 @@ func ListGuestbookContinuation(context *RequestContext, req *http.Request, resp
return success(context, req, resp)
}
}
-
-func HcaptchaArgsContinuation(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain {
- return func(success Continuation, failure Continuation) ContinuationChain {
- (*context.TemplateData)["HcaptchaArgs"] = HcaptchaArgs{
- SiteKey: context.Args.HcaptchaSiteKey,
- }
- log.Println(context.Args.HcaptchaSiteKey)
- return success(context, req, resp)
- }
-}
-
-func verifyHCaptcha(secret, response string) error {
- verifyURL := "https://hcaptcha.com/siteverify"
- body := strings.NewReader("secret=" + secret + "&response=" + response)
-
- req, err := http.NewRequest("POST", verifyURL, body)
- if err != nil {
- return err
- }
-
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
-
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- return err
- }
-
- jsonResponse := struct {
- Success bool `json:"success"`
- }{}
- err = json.NewDecoder(resp.Body).Decode(&jsonResponse)
- if err != nil {
- return err
- }
-
- if !jsonResponse.Success {
- return fmt.Errorf("hcaptcha verification failed")
- }
-
- defer resp.Body.Close()
- return nil
-}