From 8b81f1d3193e0d07469edca2d350cb639dfccfc5 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Tue, 8 Aug 2017 21:46:47 +0200 Subject: [PATCH] implemented captcha validation --- captcha.go | 43 +++++++++++++++++++++++++++++++++++++------ main.go | 2 +- mongoConfig.go | 2 +- serverRoutes.go | 19 +++++++++++++++++++ 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/captcha.go b/captcha.go index 3659b25..f63e970 100644 --- a/captcha.go +++ b/captcha.go @@ -4,6 +4,8 @@ import ( "math/rand" "os/exec" "strings" + + "gopkg.in/mgo.v2/bson" ) type Captcha struct { @@ -12,13 +14,17 @@ type Captcha struct { Question string `json:"question"` Date string `json:"date"` } -type CaptchaSolution struct { +type CaptchaSol struct { Id string `json:"id"` Imgs []string `json:"imgs"` ImgsSolution []string `json:"imgssolution"` - Question string `json:"question"` + Question string `json:"question"` //select all X Date string `json:"date"` } +type CaptchaAnswer struct { + CaptchaId string `json:"captchaid"` + Selection []int `json:"selection"` +} type ImgFakePath struct { CaptchaId string `json:"captchaid"` Real string `json:"real"` @@ -38,7 +44,7 @@ func generateRandInt(min int, max int) int { } func generateCaptcha(count int) Captcha { var captcha Captcha - var captchaSol CaptchaSolution + var captchaSol CaptchaSol captcha.Id = generateUUID() captchaSol.Id = captcha.Id @@ -57,11 +63,36 @@ func generateCaptcha(count int) Captcha { captchaSol.Imgs = append(captchaSol.Imgs, dataset[categDataset[nCateg]][nImg]) captchaSol.ImgsSolution = append(captchaSol.ImgsSolution, categDataset[nCateg]) } - captcha.Question = "Select all leopards" - captchaSol.Question = "Select all leopards" + captcha.Question = "leopard" + captchaSol.Question = "leopard" err := captchaCollection.Insert(captcha) check(err) - err = captchaSolutionCollection.Insert(captchaSol) + err = captchaSolCollection.Insert(captchaSol) check(err) return captcha } +func validateCaptcha(captchaAnswer CaptchaAnswer) bool { + var solved bool + solved = true + captchaSol := CaptchaSol{} + err := captchaSolCollection.Find(bson.M{"id": captchaAnswer.CaptchaId}).One(&captchaSol) + check(err) + for k, imgSol := range captchaSol.ImgsSolution { + if imgSol == captchaSol.Question { + if captchaAnswer.Selection[k] == 1 { + //correct + } else { + solved = false + } + } + if imgSol != captchaSol.Question { + if captchaAnswer.Selection[k] == 0 { + //correct + } else { + solved = false + } + } + } + + return solved +} diff --git a/main.go b/main.go index 75a4970..afd9d2f 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ func main() { session, err := getSession() check(err) captchaCollection = getCollection(session, "captchas") - captchaSolutionCollection = getCollection(session, "captchassolutions") + captchaSolCollection = getCollection(session, "captchassolutions") imgFakePathCollection = getCollection(session, "imgfakepath") //start the server diff --git a/mongoConfig.go b/mongoConfig.go index bd02417..d55c4ef 100644 --- a/mongoConfig.go +++ b/mongoConfig.go @@ -17,7 +17,7 @@ type MongoConfig struct { var mongoConfig MongoConfig var captchaCollection *mgo.Collection -var captchaSolutionCollection *mgo.Collection +var captchaSolCollection *mgo.Collection var imgFakePathCollection *mgo.Collection func readMongodbConfig(path string) { diff --git a/serverRoutes.go b/serverRoutes.go index 01053bc..2b58377 100644 --- a/serverRoutes.go +++ b/serverRoutes.go @@ -34,6 +34,12 @@ var routes = Routes{ "/captcha", GetCaptcha, }, + Route{ + "AnswerCaptcha", + "POST", + "/answer", + AnswerCaptcha, + }, } //ROUTES @@ -74,3 +80,16 @@ func GetCaptcha(w http.ResponseWriter, r *http.Request) { check(err) fmt.Fprintln(w, string(jsonResp)) } + +func AnswerCaptcha(w http.ResponseWriter, r *http.Request) { + decoder := json.NewDecoder(r.Body) + var captchaAnswer CaptchaAnswer + err := decoder.Decode(&captchaAnswer) + check(err) + defer r.Body.Close() + + resp := validateCaptcha(captchaAnswer) + jsonResp, err := json.Marshal(resp) + check(err) + fmt.Fprintln(w, string(jsonResp)) +}