diff --git a/Gruntfile.js b/Gruntfile.js index dd5bc86bb..c1a388bb8 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -50,7 +50,7 @@ module.exports = function (grunt) { "jQuery": true, "$": true, "_": true, - "oc_requesttoken": true + "_nc_auth_requestToken": true } }, all: ['js/app/**/*.js'] @@ -229,6 +229,7 @@ module.exports = function (grunt) { 'js/vendor/ui-sortable/sortable.js', 'js/vendor/jquery-3.7.1.min.js', 'js/vendor/jquery-ui.min.js', + 'js/lib/auth.js', 'js/lib/promise.js', 'js/lib/crypto_wrap.js', 'js/lib/otpauth.umd.js', @@ -274,6 +275,7 @@ module.exports = function (grunt) { 'js/vendor/papa-parse/papaparse.min.js', 'js/vendor/jquery-3.7.1.min.js', 'js/vendor/jquery-ui.min.js', + 'js/lib/auth.js', 'js/lib/promise.js', 'js/lib/crypto_wrap.js', 'js/lib/otpauth.umd.js', diff --git a/js/app/app.js b/js/app/app.js index 80a827072..1e7648ce9 100644 --- a/js/app/app.js +++ b/js/app/app.js @@ -80,8 +80,7 @@ }); }).config([ '$httpProvider', function($httpProvider) { - /** global: oc_requesttoken */ - $httpProvider.defaults.headers.common.requesttoken = oc_requesttoken; + $httpProvider.defaults.headers.common.requesttoken = GetRequestToken(); }]).config(function(localStorageServiceProvider) { localStorageServiceProvider.setNotify(true, true); }).config(function($translateProvider) { diff --git a/js/app/app_public.js b/js/app/app_public.js index 2552c32a2..4c8fac3dd 100644 --- a/js/app/app_public.js +++ b/js/app/app_public.js @@ -37,8 +37,7 @@ 'pascalprecht.translate' ]).config(['$httpProvider', function ($httpProvider) { - /** global: oc_requesttoken */ - $httpProvider.defaults.headers.common.requesttoken = oc_requesttoken; + $httpProvider.defaults.headers.common.requesttoken = GetRequestToken(); }]).config(function ($translateProvider) { $translateProvider.useUrlLoader(OC.generateUrl('/apps/' + APP_ID + '/api/v2/language')); $translateProvider.preferredLanguage('en'); diff --git a/js/lib/auth.js b/js/lib/auth.js new file mode 100644 index 000000000..e62af7052 --- /dev/null +++ b/js/lib/auth.js @@ -0,0 +1,14 @@ +/** + * Token injection logic required for ajax requests with Nextcloud 34+ since they removed their upstream jquery + * as well as the former oc_requesttoken (with Nextcloud 35+). See https://github.com/nextcloud/passman/issues/869 + */ +function GetRequestToken() { + if (typeof OC !== 'undefined' && OC.requestToken) { + return OC.requestToken; + } + if (typeof _nc_auth_requestToken === 'string' && _nc_auth_requestToken) { + return _nc_auth_requestToken; + } + const head = document.head || document.getElementsByTagName('head')[0]; + return (head && head.dataset.requesttoken) ? head.dataset.requesttoken : ''; +} diff --git a/js/settings-admin.js b/js/settings-admin.js index 584d655bb..bec23631f 100644 --- a/js/settings-admin.js +++ b/js/settings-admin.js @@ -23,24 +23,12 @@ $(document).ready(function () { const urlPrefix = 'apps/passman'; - // Token injection logic required for ajax requests with Nextcloud 34+ since they removed their upstream jquery - function getRequestToken() { - if (typeof OC !== 'undefined' && OC.requestToken) { - return OC.requestToken; - } - if (typeof oc_requesttoken !== 'undefined' && oc_requesttoken) { - return oc_requesttoken; - } - var head = document.head || document.getElementsByTagName('head')[0]; - return (head && head.dataset.requesttoken) ? head.dataset.requesttoken : ''; - } - function getAjaxMethod(options) { return (options.type || options.method || 'GET').toUpperCase(); } $.ajaxPrefilter(function (options) { - var token = getRequestToken(); + var token = GetRequestToken(); if (!token) { return; } diff --git a/templates/admin.php b/templates/admin.php index e7ea5923f..da8d8dd8a 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -8,6 +8,7 @@ /*build-js-start*/ Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/jquery-3.7.1.min', 'core'); +Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/auth', 'core'); /*build-js-end*/ Util::addScript(MyAppTemplateConfig::APP_ID, 'settings-admin', 'core'); diff --git a/templates/bookmarklet.php b/templates/bookmarklet.php index 0dfd4f6a4..cbd81805c 100644 --- a/templates/bookmarklet.php +++ b/templates/bookmarklet.php @@ -35,6 +35,7 @@ Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/download', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/ui-sortable/sortable', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/papa-parse/papaparse.min', 'core'); +Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/auth', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/promise', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/crypto_wrap', 'core'); diff --git a/templates/main.php b/templates/main.php index 917209e05..38583f1ec 100644 --- a/templates/main.php +++ b/templates/main.php @@ -35,6 +35,7 @@ Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/download', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/ui-sortable/sortable', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/papa-parse/papaparse.min', 'core'); +Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/auth', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/promise', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/crypto_wrap', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/otpauth.umd', 'core'); diff --git a/templates/public_share.php b/templates/public_share.php index 7f77561d2..809828eac 100644 --- a/templates/public_share.php +++ b/templates/public_share.php @@ -33,6 +33,7 @@ Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/llqrcode/llqrcode', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/forge.0.6.9.min', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'vendor/download', 'core'); +Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/auth', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/promise', 'core'); Util::addScript(MyAppTemplateConfig::APP_ID, 'lib/crypto_wrap', 'core');