2022-06-24 23:09:46 +04:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-28 23:30:38 +04:00
|
|
|
"image/jpeg"
|
2022-10-20 22:58:14 +04:00
|
|
|
"justcaptcha/internal/dwcaptcha"
|
|
|
|
"justcaptcha/pkg/captcha"
|
|
|
|
"justcaptcha/pkg/captcha/inmemdb"
|
2022-06-24 23:09:46 +04:00
|
|
|
"justcaptcha/pkg/server"
|
|
|
|
"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 errMsgNotFound = "CAPTCHA with such ID doesn't exist"
|
|
|
|
const errMsgWrongAnswer = "An answer provided was wrong"
|
|
|
|
const errMsgFailedToGenImage = "failed to generate an image for a CAPTCHA"
|
|
|
|
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-20 22:58:14 +04:00
|
|
|
captchaImage, err := inmemdb.Image(captchaID, captchaStyle)
|
2022-06-24 23:09:46 +04:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2022-10-20 22:58:14 +04:00
|
|
|
fmt.Fprint(w, errMsgImageNotFound)
|
2022-06-24 23:09:46 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if captchaImage == nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2022-10-20 22:58:14 +04:00
|
|
|
fmt.Fprint(w, errMsgFailedToGenImage)
|
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-20 22:58:14 +04:00
|
|
|
ok, err := inmemdb.Solve(captchaID, answer)
|
2022-06-24 23:09:46 +04:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2022-10-20 22:58:14 +04:00
|
|
|
fmt.Fprint(w, errMsgNotFound)
|
2022-06-24 23:09:46 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2022-10-20 22:58:14 +04:00
|
|
|
fmt.Fprint(w, errMsgWrongAnswer)
|
|
|
|
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")
|
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
2022-08-28 23:32:16 +04:00
|
|
|
if isJustRemove {
|
2022-10-20 22:58:14 +04:00
|
|
|
inmemdb.Remove(captchaID)
|
2022-08-28 23:32:16 +04:00
|
|
|
return
|
|
|
|
}
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
solved, err := inmemdb.IsSolved(captchaID)
|
2022-06-24 23:09:46 +04:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2022-10-20 22:58:14 +04:00
|
|
|
fmt.Fprint(w, errMsgNotFound)
|
2022-06-24 23:09:46 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !solved {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2022-10-20 22:58:14 +04:00
|
|
|
fmt.Fprint(w, errMsgWrongAnswer)
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
}
|