From ef9875c5a6137aef927777eb262d119904e3d6aa Mon Sep 17 00:00:00 2001 From: Ersun Warncke Date: Mon, 21 Jan 2019 13:09:02 -0400 Subject: [PATCH] Merge pull request #1427 from sharelatex/ew-recaptcha add recaptcha validate middleware to reg and add recaptcha v3 GitOrigin-RevId: 35375b7887832b40bc570cf848cab9c62243443b --- .../infrastructure/ExpressLocals.coffee | 1 + services/web/app/views/layout.pug | 5 ++++- .../web/public/src/directives/asyncForm.js | 5 ++++- services/web/public/src/ide.js | 1 + .../ShareProjectModalController.js | 4 ++++ services/web/public/src/main.js | 1 + .../public/src/services/validateCaptchaV3.js | 19 +++++++++++++++++++ 7 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 services/web/public/src/services/validateCaptchaV3.js diff --git a/services/web/app/coffee/infrastructure/ExpressLocals.coffee b/services/web/app/coffee/infrastructure/ExpressLocals.coffee index 71789a11b6..bb320f6039 100644 --- a/services/web/app/coffee/infrastructure/ExpressLocals.coffee +++ b/services/web/app/coffee/infrastructure/ExpressLocals.coffee @@ -373,4 +373,5 @@ module.exports = (app, webRouter, privateApiRouter, publicApiRouter)-> isOverleaf: Settings.overleaf? appName: Settings.appName siteUrl: Settings.siteUrl + recaptchaSiteKeyV3: Settings.recaptcha?.siteKeyV3 next() diff --git a/services/web/app/views/layout.pug b/services/web/app/views/layout.pug index e89d770a69..a5a2c12df5 100644 --- a/services/web/app/views/layout.pug +++ b/services/web/app/views/layout.pug @@ -99,7 +99,10 @@ html( body if(settings.recaptcha) - script(src="https://www.google.com/recaptcha/api.js?render=explicit") + if (settings.recaptcha.siteKeyV3) + script(src="https://www.google.com/recaptcha/api.js?render="+settings.recaptcha.siteKeyV3) + else + script(src="https://www.google.com/recaptcha/api.js?render=explicit") div( id="recaptcha" class="g-recaptcha" diff --git a/services/web/public/src/directives/asyncForm.js b/services/web/public/src/directives/asyncForm.js index c8824459fd..4fcabca628 100644 --- a/services/web/public/src/directives/asyncForm.js +++ b/services/web/public/src/directives/asyncForm.js @@ -14,7 +14,7 @@ * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ define(['base', 'libs/passfield'], function(App) { - App.directive('asyncForm', ($http, validateCaptcha) => ({ + App.directive('asyncForm', ($http, validateCaptcha, validateCaptchaV3) => ({ controller: [ '$scope', function($scope) { @@ -33,6 +33,9 @@ define(['base', 'libs/passfield'], function(App) { if (callback == null) { callback = function(response) {} } + if (attrs.captchaActionName) { + validateCaptchaV3(attrs.captchaActionName) + } if (attrs.captcha != null) { return validateCaptcha(callback) } else { diff --git a/services/web/public/src/ide.js b/services/web/public/src/ide.js index 3522026ed6..456f686fd3 100644 --- a/services/web/public/src/ide.js +++ b/services/web/public/src/ide.js @@ -55,6 +55,7 @@ define([ 'directives/videoPlayState', 'services/queued-http', 'services/validateCaptcha', + 'services/validateCaptchaV3', 'services/wait-for', 'filters/formatDate', 'main/event', diff --git a/services/web/public/src/ide/share/controllers/ShareProjectModalController.js b/services/web/public/src/ide/share/controllers/ShareProjectModalController.js index c129f0eb72..344ff84fff 100644 --- a/services/web/public/src/ide/share/controllers/ShareProjectModalController.js +++ b/services/web/public/src/ide/share/controllers/ShareProjectModalController.js @@ -27,6 +27,7 @@ define(['base'], App => $http, ide, validateCaptcha, + validateCaptchaV3, settings, event_tracking ) { @@ -160,6 +161,9 @@ define(['base'], App => // Skip this existing member return addNextMember() } + // do v3 captcha to collect data only + validateCaptchaV3('invite') + // do v2 captcha return validateCaptcha(function(response) { let inviteId, request $scope.grecaptchaResponse = response diff --git a/services/web/public/src/main.js b/services/web/public/src/main.js index a0302ff3ef..c528d9ebe1 100644 --- a/services/web/public/src/main.js +++ b/services/web/public/src/main.js @@ -54,6 +54,7 @@ define([ 'directives/bookmarkableTabset', 'services/queued-http', 'services/validateCaptcha', + 'services/validateCaptchaV3', 'filters/formatDate', 'components/inputSuggestions', '__MAIN_CLIENTSIDE_INCLUDES__' diff --git a/services/web/public/src/services/validateCaptchaV3.js b/services/web/public/src/services/validateCaptchaV3.js new file mode 100644 index 0000000000..3917ef7726 --- /dev/null +++ b/services/web/public/src/services/validateCaptchaV3.js @@ -0,0 +1,19 @@ +define(['base'], function(App) { + return App.factory('validateCaptchaV3', function() { + const grecaptcha = window.grecaptcha + const ExposedSettings = window.ExposedSettings + return function validateCaptchaV3(actionName, callback = () => {}) { + if (!grecaptcha) { + return + } + if (!ExposedSettings || !ExposedSettings.recaptchaSiteKeyV3) { + return + } + grecaptcha.ready(function() { + grecaptcha + .execute(ExposedSettings.recaptchaSiteKeyV3, { action: actionName }) + .then(callback) + }) + } + }) +})