Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add token_auth to overlay requests where necessary #17851

Merged
merged 15 commits into from Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion core/View.php
Expand Up @@ -11,6 +11,7 @@
use Exception;
use Piwik\AssetManager\UIAssetCacheBuster;
use Piwik\Container\StaticContainer;
use Piwik\Session\SessionAuth;
use Piwik\View\ViewInterface;
use Piwik\View\SecurityPolicy;
use Twig\Environment;
Expand Down Expand Up @@ -465,7 +466,19 @@ public static function singleReport($title, $reportHtml)
private function shouldPropagateTokenAuthInAjaxRequests()
{
$generalConfig = Config::getInstance()->General;
return Common::getRequestVar('module', false) == 'Widgetize' || $generalConfig['enable_framed_pages'] == '1';
return Common::getRequestVar('module', false) == 'Widgetize' ||
$generalConfig['enable_framed_pages'] == '1' ||
$this->validTokenAuthInUrl();
}

/**
* @return bool
* @throws Exception
*/
private function validTokenAuthInUrl()
{
$tokenAuth = Common::getRequestVar('token_auth', '', 'string', $_GET);
return ($tokenAuth && $tokenAuth === Piwik::getCurrentUserTokenAuth());
}

/**
Expand Down
1 change: 1 addition & 0 deletions plugins/Annotations/javascripts/annotations.js
Expand Up @@ -112,6 +112,7 @@

var ajaxRequest = new ajaxHelper();
ajaxRequest.addParams(ajaxParams, 'get');
ajaxRequest.withTokenInUrl();
ajaxRequest.setFormat('html');
ajaxRequest.setCallback(callback);
ajaxRequest.send();
Expand Down
2 changes: 1 addition & 1 deletion plugins/CoreHome/angularjs/common/services/piwik-api.js
Expand Up @@ -338,7 +338,7 @@ var hasBlockedContent = false;
}

return {
withTokenInUrl: withTokenInUrl,
withTokenInUrl: withTokenInUrl, // technically should probably be called withTokenInPost
bulkFetch: bulkFetch,
post: post,
fetch: fetch,
Expand Down
Expand Up @@ -114,7 +114,10 @@
}

if (piwik.shouldPropagateTokenAuth && broadcast.getValueFromUrl('token_auth')) {
url += '&force_api_session=1&token_auth=' + broadcast.getValueFromUrl('token_auth');
if (!piwik.broadcast.isWidgetizeRequestWithoutSession()) {
url += '&force_api_session=1';
}
url += '&token_auth=' + encodeURIComponent(broadcast.getValueFromUrl('token_auth'));
}

url += '&random=' + parseInt(Math.random() * 10000);
Expand Down
1 change: 0 additions & 1 deletion plugins/CoreHome/javascripts/broadcast.js
Expand Up @@ -176,7 +176,6 @@ var broadcast = {
}
}
},

isWidgetizedDashboard: function() {
return broadcast.getValueFromUrl('module') == 'Widgetize' && broadcast.getValueFromUrl('moduleToWidgetize') == 'Dashboard';
},
Expand Down
1 change: 1 addition & 0 deletions plugins/CoreHome/javascripts/dataTable_rowactions.js
Expand Up @@ -474,6 +474,7 @@ DataTable_RowActions_RowEvolution.prototype.showRowEvolution = function (apiMeth

var ajaxRequest = new ajaxHelper();
ajaxRequest.addParams(requestParams, 'get');
ajaxRequest.withTokenInUrl();
ajaxRequest.setCallback(callback);
ajaxRequest.setFormat('html');
ajaxRequest.send();
Expand Down
1 change: 1 addition & 0 deletions plugins/Live/javascripts/SegmentedVisitorLog.js
Expand Up @@ -135,6 +135,7 @@ var SegmentedVisitorLog = function() {

var ajaxRequest = new ajaxHelper();
ajaxRequest.addParams(requestParams, 'get');
ajaxRequest.withTokenInUrl();
ajaxRequest.setCallback(callback);
ajaxRequest.setFormat('html');
ajaxRequest.send();
Expand Down
5 changes: 4 additions & 1 deletion plugins/Live/javascripts/visitorProfile.js
Expand Up @@ -156,7 +156,10 @@
$element.on('mousedown', '.visitor-profile-export', function (e) {
var url = $(this).attr('href');
if (url.indexOf('&token_auth=') == -1) {
$(this).attr('href', url + '&force_api_session=1&token_auth=' + piwik.token_auth);
if (!piwik.broadcast.isWidgetizeRequestWithoutSession()) {
url += '&force_api_session=1';
}
$(this).attr('href', url + '&token_auth=' + piwik.token_auth);
}
});

Expand Down
5 changes: 4 additions & 1 deletion plugins/Overlay/javascripts/Overlay_Helper.js
Expand Up @@ -29,7 +29,10 @@ var Overlay_Helper = {

var token_auth = piwik.broadcast.getValueFromUrl("token_auth");
if (token_auth.length && piwik.shouldPropagateTokenAuth) {
url += '&force_api_session=1&token_auth=' + encodeURIComponent(token_auth);
if (!piwik.broadcast.isWidgetizeRequestWithoutSession()) {
url += '&force_api_session=1';
}
url += '&token_auth=' + encodeURIComponent(token_auth);
}

if (link) {
Expand Down
1 change: 1 addition & 0 deletions plugins/Overlay/javascripts/Piwik_Overlay.js
Expand Up @@ -50,6 +50,7 @@ var Piwik_Overlay = (function () {
globalAjaxQueue.abort();
var ajaxRequest = new ajaxHelper();
ajaxRequest.addParams(params, 'get');
ajaxRequest.withTokenInUrl(); // needed because it is calling a controller and not the API
ajaxRequest.setCallback(
function (response) {
hideLoading();
Expand Down
5 changes: 4 additions & 1 deletion plugins/Overlay/templates/index.twig
Expand Up @@ -73,7 +73,10 @@

var iframeSrc = 'index.php?module=Overlay&action=startOverlaySession&idSite={{ idSite }}&period={{ period }}&date={{ rawDate }}&segment={{ segment }}';
if (piwik.shouldPropagateTokenAuth) {
iframeSrc += '&force_api_session=1&token_auth=' + piwik.token_auth;
if (!piwik.broadcast.isWidgetizeRequestWithoutSession()) {
iframeSrc += '&force_api_session=1';
}
iframeSrc += '&token_auth=' + piwik.token_auth;
}

Piwik_Overlay.init(iframeSrc, '{{ idSite }}', '{{ period }}', '{{ rawDate }}', '{{ segment }}');
Expand Down
5 changes: 4 additions & 1 deletion plugins/Overlay/templates/index_noframe.twig
Expand Up @@ -8,7 +8,10 @@
<script type="text/javascript">
var newLocation = 'index.php?module=Overlay&action=startOverlaySession&idSite={{ idSite }}&period={{ period }}&date={{ date }}&segment={{ segment }}';
if (piwik.shouldPropagateTokenAuth) {
newLocation += '&force_api_session=1&token_auth=' + piwik.token_auth;
if (!piwik.broadcast.isWidgetizeRequestWithoutSession()) {
newLocation += '&force_api_session=1';
}
newLocation += '&token_auth=' + piwik.token_auth;
}

var locationParts = window.location.href.split('#');
Expand Down