CaptchaMiddleware.coffee 1.2 KB

12345678910111213141516171819202122232425262728
  1. request = require 'request'
  2. logger = require 'logger-sharelatex'
  3. Settings = require 'settings-sharelatex'
  4. module.exports = CaptchaMiddleware =
  5. validateCaptcha: (action) ->
  6. return (req, res, next) ->
  7. if !Settings.recaptcha?.siteKey?
  8. return next()
  9. inviteAndCaptchaDisabled = action == 'invite' and Settings.recaptcha.disabled.invite
  10. registerAndCaptchaDisabled = action == 'register' and Settings.recaptcha.disabled.register
  11. if inviteAndCaptchaDisabled or registerAndCaptchaDisabled
  12. return next()
  13. response = req.body['g-recaptcha-response']
  14. options =
  15. form:
  16. secret: Settings.recaptcha.secretKey
  17. response: response
  18. json: true
  19. request.post "https://www.google.com/recaptcha/api/siteverify", options, (error, response, body) ->
  20. return next(error) if error?
  21. if !body?.success
  22. logger.warn {statusCode: response.statusCode, body: body}, 'failed recaptcha siteverify request'
  23. return res.status(400).send({errorReason:"cannot_verify_user_not_robot", message:
  24. {text:"Sorry, we could not verify that you are not a robot. Please check that Google reCAPTCHA is not being blocked by an ad blocker or firewall."}
  25. })
  26. else
  27. return next()