Nikerabbit has uploaded a new change for review. https://gerrit.wikimedia.org/r/219194
Change subject: WIP: Send authorization header to cxserver ...................................................................... WIP: Send authorization header to cxserver Todo: add missing i18n message Depends on commit on cxserver allowing the header Change-Id: If349547fe072b3e7ef5a226afd34373a2199eb95 --- M composer.json M extension.json M modules/tools/ext.cx.tools.mt.js 3 files changed, 39 insertions(+), 8 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation refs/changes/94/219194/1 diff --git a/composer.json b/composer.json index 5d3d30a..72bac9b 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ }, "require": { "php": ">=5.3.0", - "composer/installers": ">=1.0.1" + "composer/installers": ">=1.0.1", + "firebase/php-jwt": "~2.1.0" }, "require-dev": { "jakub-onderka/php-parallel-lint": "0.8.*", diff --git a/extension.json b/extension.json index da3643b..59639f1 100644 --- a/extension.json +++ b/extension.json @@ -38,7 +38,8 @@ "APIModules": { "cxpublish": "ApiContentTranslationPublish", "cxdelete": "ApiContentTranslationDelete", - "cxconfiguration": "ApiContentTranslationConfiguration" + "cxconfiguration": "ApiContentTranslationConfiguration", + "cxtoken": "ApiContentTranslationToken" }, "APIListModules": { "contenttranslation": "ApiQueryContentTranslation", @@ -55,11 +56,12 @@ }, "AutoloadClasses": { "ApiContentTranslationConfiguration": "api/ApiContentTranslationConfiguration.php", - "ApiContentTranslationPublish": "api/ApiContentTranslationPublish.php", "ApiContentTranslationDelete": "api/ApiContentTranslationDelete.php", + "ApiContentTranslationPublish": "api/ApiContentTranslationPublish.php", + "ApiContentTranslationToken": "api/ApiContentTranslationToken.php", "ApiQueryContentTranslation": "api/ApiQueryContentTranslation.php", - "ApiQueryContentTranslationStats": "api/ApiQueryContentTranslationStats.php", "ApiQueryContentTranslationLanguageTrend": "api/ApiQueryContentTranslationLanguageTrend.php", + "ApiQueryContentTranslationStats": "api/ApiQueryContentTranslationStats.php", "ApiQueryPublishedTranslations": "api/ApiQueryPublishedTranslations.php", "ContentTranslationHooks": "ContentTranslation.hooks.php", "ContentTranslation\\Database": "includes/Database.php", @@ -105,7 +107,11 @@ "ContentTranslationUseMagnusTool": true, "ContentTranslationBrowserBlacklist": [ { "msie": [ [ "<", 10 ] ] - } ] + } ], + "ContentTranslationCXServerAuth": { + "algorithm": "HS256", + "key": "" + } }, "ResourceModules": { "ext.cx.contributions": { diff --git a/modules/tools/ext.cx.tools.mt.js b/modules/tools/ext.cx.tools.mt.js index 29e69c1..2e0c33b 100644 --- a/modules/tools/ext.cx.tools.mt.js +++ b/modules/tools/ext.cx.tools.mt.js @@ -11,11 +11,28 @@ ( function ( $, mw ) { 'use strict'; - var cache = {}, // MT requests cache + var + cxserverToken = { issued: undefined, jwt: undefined }, + cache = {}, // MT requests cache providerIdPrefix = 'cx-provider-', disableMT = 'disable-mt', noMT = 'no-mt', sourceMT = 'source-mt'; + + function getCXServerToken() { + if ( cxserverToken.jwt !== undefined ) { + return $.Deferred().resolve( cxserverToken.jwt ); + } + + // TODO: refetch if token is too old + return (new mw.Api()) + .postWithToken( 'edit', { action: 'cxtoken' } ) + .then( function ( response ) { + cxserverToken.jwt = response.jwt; + cxserverToken.issued = Date.now(); + return response.jwt; + } ); + } /** * Get the registry of machine translation providers @@ -80,8 +97,15 @@ $provider: MTControlCard.provider } ); - return $.post( mtURL, sourceHtml ).then( null, function () { - return $.Deferred().reject( 'service-failure', arguments ).promise(); + return getCXServerToken().then( function ( token ) { + return $.ajax( { + type: 'post', + url: mtURL, + data: sourceHtml, + headers: { Authorization: token } + } ).then( null, function () { + return $.Deferred().reject( 'service-failure', arguments ).promise(); + } ); } ); } -- To view, visit https://gerrit.wikimedia.org/r/219194 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If349547fe072b3e7ef5a226afd34373a2199eb95 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ContentTranslation Gerrit-Branch: master Gerrit-Owner: Nikerabbit <niklas.laxst...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits