generated from okp4/template-go
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package captcha | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func VerificationMiddleware(secret string) func(http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Get the Captcha response token from default request body field 'g-recaptcha-response'. | ||
bodyBytes, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
http.Error(w, fmt.Sprintf("Unauthorized: %s", err.Error()), http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
// Unmarshal body into struct. | ||
var body siteVerifyRequest | ||
if err := json.Unmarshal(bodyBytes, &body); err != nil { | ||
http.Error(w, fmt.Sprintf("Unauthorized: %s", err.Error()), http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
// Restore request body to read more than once. | ||
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) | ||
|
||
// Check and verify the Captcha response token. | ||
if err := checkRecaptcha(secret, body.RecaptchaResponse); err != nil { | ||
http.Error(w, fmt.Sprintf("Unauthorized: %s", err.Error()), http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package captcha | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
const siteVerifyURL = "https://www.google.com/recaptcha/api/siteverify" | ||
|
||
type siteVerifyResponse struct { | ||
Success bool `json:"success"` | ||
Score float64 `json:"score"` | ||
Action string `json:"action"` | ||
ChallengeTS time.Time `json:"challenge_ts"` | ||
Hostname string `json:"hostname"` | ||
ErrorCodes []string `json:"error-codes"` | ||
} | ||
|
||
type siteVerifyRequest struct { | ||
RecaptchaResponse string `json:"g-recaptcha-response"` | ||
} | ||
|
||
func checkRecaptcha(secret, response string) error { | ||
req, err := http.NewRequest(http.MethodPost, siteVerifyURL, nil) | ||
if err != nil { | ||
log.Error().Msgf("Error while creating Captcha verification request: %s", err.Error()) | ||
return fmt.Errorf("error while creating Captcha verification request: %s", err.Error()) | ||
} | ||
|
||
q := req.URL.Query() | ||
q.Add("secret", secret) | ||
q.Add("response", response) | ||
req.URL.RawQuery = q.Encode() | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
log.Error().Msgf("Error while requesting Captcha verification: %s", err.Error()) | ||
return fmt.Errorf("error while requesting Captcha verification: %s", err.Error()) | ||
} | ||
defer resp.Body.Close() | ||
|
||
var body siteVerifyResponse | ||
if err = json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
log.Error().Msgf("Error while decoding Captcha verification response: %s", err.Error()) | ||
return fmt.Errorf("error while decoding Captcha verification response: %s", err.Error()) | ||
} | ||
|
||
// If success false, Captcha verification KO. | ||
if !body.Success { | ||
log.Debug().Msg("Captcha verification failed") | ||
return errors.New("captcha verification failed") | ||
} | ||
|
||
// If score is too low, verification KO. | ||
if body.Score > 0.5 { | ||
log.Debug().Msg("Captcha verification failed: score is too low") | ||
return errors.New("captcha verification failed: score is too low") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters