@ -1,6 +1,8 @@
package main
package main
import (
import (
"bufio"
"bytes"
"encoding/json"
"encoding/json"
"html/template"
"html/template"
"io"
"io"
@ -372,20 +374,16 @@ func (app *appContext) GetCaptcha(gc *gin.Context) {
"contactMessage" : app . config . Section ( "ui" ) . Key ( "contact_message" ) . String ( ) ,
"contactMessage" : app . config . Section ( "ui" ) . Key ( "contact_message" ) . String ( ) ,
} )
} )
}
}
var capt * captcha . Data
var capt Captcha
ok = true
if inv . Captchas != nil {
if inv . Captchas != nil {
capt = inv . Captchas [ captchaID ]
capt , ok = inv . Captchas [ captchaID ]
}
}
if capt == nil {
if ! ok {
respondBool ( 400 , false , gc )
respondBool ( 400 , false , gc )
return
return
}
}
if err := capt . WriteImage ( gc . Writer ) ; err != nil {
gc . Data ( 200 , "image/png" , capt . Image )
app . err . Printf ( "Failed to write CAPTCHA image: %v" , err )
respondBool ( 500 , false , gc )
return
}
gc . Status ( 200 )
return
return
}
}
@ -414,10 +412,20 @@ func (app *appContext) GenCaptcha(gc *gin.Context) {
return
return
}
}
if inv . Captchas == nil {
if inv . Captchas == nil {
inv . Captchas = map [ string ] * captcha . Dat a{ }
inv . Captchas = map [ string ] Captch a{ }
}
}
captchaID := genAuthToken ( )
captchaID := genAuthToken ( )
inv . Captchas [ captchaID ] = capt
var buf bytes . Buffer
if err := capt . WriteImage ( bufio . NewWriter ( & buf ) ) ; err != nil {
app . err . Printf ( "Failed to render captcha: %v" , err )
respondBool ( 500 , false , gc )
return
}
inv . Captchas [ captchaID ] = Captcha {
Answer : capt . Text ,
Image : buf . Bytes ( ) ,
Generated : time . Now ( ) ,
}
app . storage . SetInvitesKey ( code , inv )
app . storage . SetInvitesKey ( code , inv )
gc . JSON ( 200 , genCaptchaDTO { captchaID } )
gc . JSON ( 200 , genCaptchaDTO { captchaID } )
return
return
@ -437,7 +445,7 @@ func (app *appContext) verifyCaptcha(code, id, text string) bool {
app . debug . Printf ( "Couldn't find Captcha \"%s\"" , id )
app . debug . Printf ( "Couldn't find Captcha \"%s\"" , id )
return false
return false
}
}
return strings . ToLower ( c . Text ) == strings . ToLower ( text )
return strings . ToLower ( c . Answer ) == strings . ToLower ( text )
}
}
// reCAPTCHA
// reCAPTCHA
@ -504,15 +512,15 @@ func (app *appContext) VerifyCaptcha(gc *gin.Context) {
} )
} )
return
return
}
}
var capt * captcha . Dat a
var capt Captch a
if inv . Captchas != nil {
if inv . Captchas != nil {
capt = inv . Captchas [ captchaID ]
capt , ok = inv . Captchas [ captchaID ]
}
}
if capt == nil {
if ! ok {
respondBool ( 400 , false , gc )
respondBool ( 400 , false , gc )
return
return
}
}
if strings . ToLower ( capt . Text ) != strings . ToLower ( text ) {
if strings . ToLower ( capt . Answer ) != strings . ToLower ( text ) {
respondBool ( 400 , false , gc )
respondBool ( 400 , false , gc )
return
return
}
}