mirror of
https://github.com/arnaucube/goCaptcha.git
synced 2026-02-06 19:16:44 +01:00
implemented captcha validation
This commit is contained in:
43
captcha.go
43
captcha.go
@@ -4,6 +4,8 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Captcha struct {
|
type Captcha struct {
|
||||||
@@ -12,13 +14,17 @@ type Captcha struct {
|
|||||||
Question string `json:"question"`
|
Question string `json:"question"`
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
}
|
}
|
||||||
type CaptchaSolution struct {
|
type CaptchaSol struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
Imgs []string `json:"imgs"`
|
Imgs []string `json:"imgs"`
|
||||||
ImgsSolution []string `json:"imgssolution"`
|
ImgsSolution []string `json:"imgssolution"`
|
||||||
Question string `json:"question"`
|
Question string `json:"question"` //select all X
|
||||||
Date string `json:"date"`
|
Date string `json:"date"`
|
||||||
}
|
}
|
||||||
|
type CaptchaAnswer struct {
|
||||||
|
CaptchaId string `json:"captchaid"`
|
||||||
|
Selection []int `json:"selection"`
|
||||||
|
}
|
||||||
type ImgFakePath struct {
|
type ImgFakePath struct {
|
||||||
CaptchaId string `json:"captchaid"`
|
CaptchaId string `json:"captchaid"`
|
||||||
Real string `json:"real"`
|
Real string `json:"real"`
|
||||||
@@ -38,7 +44,7 @@ func generateRandInt(min int, max int) int {
|
|||||||
}
|
}
|
||||||
func generateCaptcha(count int) Captcha {
|
func generateCaptcha(count int) Captcha {
|
||||||
var captcha Captcha
|
var captcha Captcha
|
||||||
var captchaSol CaptchaSolution
|
var captchaSol CaptchaSol
|
||||||
|
|
||||||
captcha.Id = generateUUID()
|
captcha.Id = generateUUID()
|
||||||
captchaSol.Id = captcha.Id
|
captchaSol.Id = captcha.Id
|
||||||
@@ -57,11 +63,36 @@ func generateCaptcha(count int) Captcha {
|
|||||||
captchaSol.Imgs = append(captchaSol.Imgs, dataset[categDataset[nCateg]][nImg])
|
captchaSol.Imgs = append(captchaSol.Imgs, dataset[categDataset[nCateg]][nImg])
|
||||||
captchaSol.ImgsSolution = append(captchaSol.ImgsSolution, categDataset[nCateg])
|
captchaSol.ImgsSolution = append(captchaSol.ImgsSolution, categDataset[nCateg])
|
||||||
}
|
}
|
||||||
captcha.Question = "Select all leopards"
|
captcha.Question = "leopard"
|
||||||
captchaSol.Question = "Select all leopards"
|
captchaSol.Question = "leopard"
|
||||||
err := captchaCollection.Insert(captcha)
|
err := captchaCollection.Insert(captcha)
|
||||||
check(err)
|
check(err)
|
||||||
err = captchaSolutionCollection.Insert(captchaSol)
|
err = captchaSolCollection.Insert(captchaSol)
|
||||||
check(err)
|
check(err)
|
||||||
return captcha
|
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
|
||||||
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -20,7 +20,7 @@ func main() {
|
|||||||
session, err := getSession()
|
session, err := getSession()
|
||||||
check(err)
|
check(err)
|
||||||
captchaCollection = getCollection(session, "captchas")
|
captchaCollection = getCollection(session, "captchas")
|
||||||
captchaSolutionCollection = getCollection(session, "captchassolutions")
|
captchaSolCollection = getCollection(session, "captchassolutions")
|
||||||
imgFakePathCollection = getCollection(session, "imgfakepath")
|
imgFakePathCollection = getCollection(session, "imgfakepath")
|
||||||
|
|
||||||
//start the server
|
//start the server
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type MongoConfig struct {
|
|||||||
var mongoConfig MongoConfig
|
var mongoConfig MongoConfig
|
||||||
|
|
||||||
var captchaCollection *mgo.Collection
|
var captchaCollection *mgo.Collection
|
||||||
var captchaSolutionCollection *mgo.Collection
|
var captchaSolCollection *mgo.Collection
|
||||||
var imgFakePathCollection *mgo.Collection
|
var imgFakePathCollection *mgo.Collection
|
||||||
|
|
||||||
func readMongodbConfig(path string) {
|
func readMongodbConfig(path string) {
|
||||||
|
|||||||
@@ -34,6 +34,12 @@ var routes = Routes{
|
|||||||
"/captcha",
|
"/captcha",
|
||||||
GetCaptcha,
|
GetCaptcha,
|
||||||
},
|
},
|
||||||
|
Route{
|
||||||
|
"AnswerCaptcha",
|
||||||
|
"POST",
|
||||||
|
"/answer",
|
||||||
|
AnswerCaptcha,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
//ROUTES
|
//ROUTES
|
||||||
@@ -74,3 +80,16 @@ func GetCaptcha(w http.ResponseWriter, r *http.Request) {
|
|||||||
check(err)
|
check(err)
|
||||||
fmt.Fprintln(w, string(jsonResp))
|
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))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user