github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/cmd/geph-binder/registration.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 "github.com/dchest/captcha" 8 ) 9 10 func handleRegister(w http.ResponseWriter, r *http.Request) { 11 if r.Method == "OPTIONS" { 12 return 13 } 14 var req struct { 15 Username string 16 Password string 17 CaptchaID string 18 CaptchaSoln string 19 } 20 err := json.NewDecoder(r.Body).Decode(&req) 21 if err != nil { 22 w.WriteHeader(http.StatusBadRequest) 23 return 24 } 25 // check captcha 26 if !captcha.VerifyString(req.CaptchaID, req.CaptchaSoln) { 27 w.WriteHeader(http.StatusBadRequest) 28 return 29 } 30 // register 31 err = createUser(req.Username, req.Password) 32 if err != nil { 33 w.WriteHeader(http.StatusForbidden) 34 return 35 } 36 // ok! 37 return 38 } 39 40 func handleCaptcha(w http.ResponseWriter, r *http.Request) { 41 w.Header().Add("content-type", "image/png") 42 w.Header().Add("cache-control", "no-cache") 43 id := captcha.NewLen(8) 44 w.Header().Add("x-captcha-id", id) 45 w.Header().Add("Access-Control-Expose-Headers", "x-captcha-id") 46 captcha.WriteImage(w, id, 200, 100) 47 }