MCPcopy Create free account
hub / github.com/daodst/chat / validateRecaptcha

Function validateRecaptcha

clientapi/routing/register.go:334–397  ·  view source on GitHub ↗

validateRecaptcha returns an error response if the captcha response is invalid

(
	cfg *config.ClientAPI,
	response string,
	clientip string,
)

Source from the content-addressed store, hash-verified

332
333// validateRecaptcha returns an error response if the captcha response is invalid
334func validateRecaptcha(
335 cfg *config.ClientAPI,
336 response string,
337 clientip string,
338) *util.JSONResponse {
339 if !cfg.RecaptchaEnabled {
340 return &util.JSONResponse{
341 Code: http.StatusConflict,
342 JSON: jsonerror.Unknown("Captcha registration is disabled"),
343 }
344 }
345
346 if response == "" {
347 return &util.JSONResponse{
348 Code: http.StatusBadRequest,
349 JSON: jsonerror.BadJSON("Captcha response is required"),
350 }
351 }
352
353 // Make a POST request to Google's API to check the captcha response
354 resp, err := http.PostForm(cfg.RecaptchaSiteVerifyAPI,
355 url.Values{
356 "secret": {cfg.RecaptchaPrivateKey},
357 "response": {response},
358 "remoteip": {clientip},
359 },
360 )
361
362 if err != nil {
363 return &util.JSONResponse{
364 Code: http.StatusInternalServerError,
365 JSON: jsonerror.BadJSON("Error in requesting validation of captcha response"),
366 }
367 }
368
369 // Close the request once we're finishing reading from it
370 defer resp.Body.Close() // nolint: errcheck
371
372 // Grab the body of the response from the captcha server
373 var r recaptchaResponse
374 body, err := io.ReadAll(resp.Body)
375 if err != nil {
376 return &util.JSONResponse{
377 Code: http.StatusGatewayTimeout,
378 JSON: jsonerror.Unknown("Error in contacting captcha server" + err.Error()),
379 }
380 }
381 err = json.Unmarshal(body, &r)
382 if err != nil {
383 return &util.JSONResponse{
384 Code: http.StatusInternalServerError,
385 JSON: jsonerror.BadJSON("Error in unmarshaling captcha server's response: " + err.Error()),
386 }
387 }
388
389 // Check that we received a "success"
390 if !r.Success {
391 return &util.JSONResponse{

Callers 2

handleRegistrationFlowFunction · 0.85
AuthFallbackFunction · 0.85

Calls 2

CloseMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected