mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/ConfirmEdit
synced 2024-12-04 20:48:31 +00:00
fbf6c1c7ea
This is a rudimentary implementation of Cloudflare's Turnstile captcha alternative. Since it is fairly similar to ReCaptchaNoCaptcha, there is some duplicate code used here. I opted not to use the migration path that CF has for reCAPTCHA v2 and build this as a separate implementation. After T324925, it should be easy to refactor this code. Users should set $wgTurnstileSiteKey and $wgTurnstileSecretKey as appropriate, based on details from the Cloudflare dashboard. Bug: T319068 Change-Id: I0d5a74655619975f0bf61b5b672159afe5f4fb00
73 lines
2.3 KiB
JavaScript
73 lines
2.3 KiB
JavaScript
mw.loader.using( 'ext.visualEditor.targetLoader' ).then( function () {
|
|
mw.libs.ve.targetLoader.addPlugin( function () {
|
|
|
|
ve.init.mw.TurnstileSaveErrorHandler = function () {};
|
|
|
|
OO.inheritClass( ve.init.mw.TurnstileSaveErrorHandler, ve.init.mw.SaveErrorHandler );
|
|
|
|
ve.init.mw.TurnstileSaveErrorHandler.static.name = 'confirmEditTurnstile';
|
|
|
|
ve.init.mw.TurnstileSaveErrorHandler.static.getReadyPromise = function () {
|
|
const onLoadFn = 'onTurnstileLoadCallback' + Date.now();
|
|
let deferred, config, scriptURL, params;
|
|
|
|
if ( !this.readyPromise ) {
|
|
deferred = $.Deferred();
|
|
config = mw.config.get( 'wgConfirmEditConfig' );
|
|
scriptURL = new mw.Uri( config.turnstileScriptURL );
|
|
params = { onload: onLoadFn, render: 'explicit' };
|
|
scriptURL.query = $.extend( scriptURL.query, params );
|
|
|
|
this.readyPromise = deferred.promise();
|
|
window[ onLoadFn ] = deferred.resolve;
|
|
mw.loader.load( scriptURL.toString() );
|
|
}
|
|
|
|
return this.readyPromise;
|
|
};
|
|
|
|
ve.init.mw.TurnstileSaveErrorHandler.static.matchFunction = function ( data ) {
|
|
const captchaData = ve.getProp( data, 'visualeditoredit', 'edit', 'captcha' );
|
|
|
|
return !!( captchaData && captchaData.type === 'turnstile' );
|
|
};
|
|
|
|
ve.init.mw.TurnstileSaveErrorHandler.static.process = function ( data, target ) {
|
|
const self = this,
|
|
config = mw.config.get( 'wgConfirmEditConfig' ),
|
|
siteKey = config.turnstileSiteKey,
|
|
$container = $( '<div>' );
|
|
|
|
// Register extra fields
|
|
target.saveFields.wpCaptchaWord = function () {
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
return $( '#cf-turnstile-response' ).val();
|
|
};
|
|
|
|
this.getReadyPromise()
|
|
.then( function () {
|
|
if ( self.widgetId ) {
|
|
window.turnstile.reset( self.widgetId );
|
|
} else {
|
|
target.saveDialog.showMessage( 'api-save-error', $container, { wrap: false } );
|
|
self.widgetId = window.turnstile.render( $container[ 0 ], {
|
|
sitekey: siteKey,
|
|
callback: function () {
|
|
target.saveDialog.executeAction( 'save' );
|
|
},
|
|
'expired-callback': function () {},
|
|
'error-callback': function () {}
|
|
} );
|
|
|
|
target.saveDialog.updateSize();
|
|
}
|
|
|
|
target.emit( 'saveErrorCaptcha' );
|
|
} );
|
|
};
|
|
|
|
ve.init.mw.saveErrorHandlerFactory.register( ve.init.mw.TurnstileSaveErrorHandler );
|
|
|
|
} );
|
|
} );
|