2022-06-24 23:09:46 +04:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-28 23:30:38 +04:00
|
|
|
"image/jpeg"
|
2023-01-08 17:00:19 +04:00
|
|
|
"justcaptcha/internal/server"
|
2022-10-20 22:58:14 +04:00
|
|
|
"justcaptcha/pkg/captcha"
|
|
|
|
"justcaptcha/pkg/captcha/inmemdb"
|
2023-01-08 17:00:19 +04:00
|
|
|
"justcaptcha/pkg/dwcaptcha"
|
2022-06-24 23:09:46 +04:00
|
|
|
"net/http"
|
2022-10-20 22:58:14 +04:00
|
|
|
"time"
|
2022-06-24 23:09:46 +04:00
|
|
|
)
|
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
const errMsgWrongAnswer = "An answer provided was wrong"
|
|
|
|
const errMsgImageNotFound = "cannot get an image for a non-existing CAPTCHA"
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
type CaptchaHandlers struct {
|
|
|
|
expiry time.Duration
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
func New(expiry time.Duration) *CaptchaHandlers {
|
|
|
|
inmemdb.SetExpiry(expiry)
|
|
|
|
return &CaptchaHandlers{expiry: expiry}
|
|
|
|
}
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
func (h *CaptchaHandlers) New(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dc := dwcaptcha.NewDwellingCaptcha(h.expiry)
|
|
|
|
_, id := inmemdb.New(r.RemoteAddr, dc)
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2022-06-24 23:09:46 +04:00
|
|
|
fmt.Fprint(w, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
|
2022-10-20 22:58:14 +04:00
|
|
|
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
|
2022-06-27 01:20:36 +04:00
|
|
|
captchaStyle := r.URL.Query().Get("style")
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
captchaImage := inmemdb.Image(captchaID, captchaStyle)
|
2022-06-24 23:09:46 +04:00
|
|
|
if captchaImage == nil {
|
2022-10-21 00:01:19 +04:00
|
|
|
http.Error(w, errMsgImageNotFound, http.StatusNotFound)
|
2022-06-24 23:09:46 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Disposition", "inline; filename=\""+string(captchaID)+"\"")
|
|
|
|
|
2022-08-28 23:30:38 +04:00
|
|
|
jpeg.Encode(w, *captchaImage, &jpeg.Options{Quality: 20})
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
|
2022-10-20 22:58:14 +04:00
|
|
|
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
r.ParseForm()
|
|
|
|
answer := captcha.Answer(r.FormValue("answer"))
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
if ok := inmemdb.Solve(captchaID, answer); !ok {
|
2022-10-20 23:16:30 +04:00
|
|
|
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
|
2022-10-20 22:58:14 +04:00
|
|
|
return
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
2022-10-20 22:58:14 +04:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) {
|
2022-10-20 22:58:14 +04:00
|
|
|
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
|
2022-08-28 23:32:16 +04:00
|
|
|
isJustRemove := r.URL.Query().Has("remove")
|
|
|
|
|
|
|
|
if isJustRemove {
|
2022-10-20 22:58:14 +04:00
|
|
|
inmemdb.Remove(captchaID)
|
2022-10-20 23:16:30 +04:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2022-08-28 23:32:16 +04:00
|
|
|
return
|
|
|
|
}
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
if solved := inmemdb.IsSolved(captchaID); !solved {
|
2022-10-20 23:16:30 +04:00
|
|
|
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
|
2022-10-21 00:01:19 +04:00
|
|
|
return
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
2022-10-20 23:16:30 +04:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|