CaptchaMiddleware.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* eslint-disable
  2. max-len,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let CaptchaMiddleware
  14. const request = require('request')
  15. const logger = require('logger-sharelatex')
  16. const Settings = require('settings-sharelatex')
  17. module.exports = CaptchaMiddleware = {
  18. validateCaptcha(action) {
  19. return function (req, res, next) {
  20. if (
  21. (Settings.recaptcha != null ? Settings.recaptcha.siteKey : undefined) ==
  22. null
  23. ) {
  24. return next()
  25. }
  26. const inviteAndCaptchaDisabled =
  27. action === 'invite' && Settings.recaptcha.disabled.invite
  28. const registerAndCaptchaDisabled =
  29. action === 'register' && Settings.recaptcha.disabled.register
  30. if (inviteAndCaptchaDisabled || registerAndCaptchaDisabled) {
  31. return next()
  32. }
  33. const response = req.body['g-recaptcha-response']
  34. const options = {
  35. form: {
  36. secret: Settings.recaptcha.secretKey,
  37. response
  38. },
  39. json: true
  40. }
  41. return request.post(
  42. 'https://www.google.com/recaptcha/api/siteverify',
  43. options,
  44. function (error, response, body) {
  45. if (error != null) {
  46. return next(error)
  47. }
  48. if (!(body != null ? body.success : undefined)) {
  49. logger.warn(
  50. { statusCode: response.statusCode, body },
  51. 'failed recaptcha siteverify request'
  52. )
  53. return res.status(400).send({
  54. errorReason: 'cannot_verify_user_not_robot',
  55. message: {
  56. text:
  57. '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.'
  58. }
  59. })
  60. } else {
  61. return next()
  62. }
  63. }
  64. )
  65. }
  66. }
  67. }