diff --git a/datamodels/2.x/itop-hub-connector/TokenValidation.php b/datamodels/2.x/itop-hub-connector/TokenValidation.php new file mode 100644 index 000000000..6a30079cf --- /dev/null +++ b/datamodels/2.x/itop-hub-connector/TokenValidation.php @@ -0,0 +1,19 @@ +add('
'); - $oPage->add(''); - $oPage->add_ready_script('$("#hub_launch_form").submit();'); - break; + // Hidden IFRAME at the end of the setup + require_once (APPROOT.'/application/ajaxwebpage.class.inc.php'); + $sParamToken = utils::ReadParam('setup_token'); + $oTokenValidation = new TokenValidation(); + $bIsTokenValid = $oTokenValidation->isSetupTokenValid($sParamToken); + if (UserRights::IsAdministrator() || $bIsTokenValid) { + $oPage = new NiceWebPage(''); + $aDataToPost = MakeDataToPost($sTargetRoute); + $oPage->add(''); + $oPage->add(''); + $oPage->add_ready_script('$("#hub_launch_form").submit();'); + } else { + IssueLog::Error('TokenValidation failed on inform_after_setup page'); + throw new Exception("Not allowed"); + } + break; + default: // All other cases, special "Hub like" web page if ($sTargetRoute == 'view_dashboard') { diff --git a/setup/wizardsteps.class.inc.php b/setup/wizardsteps.class.inc.php index f5633c63b..68f997960 100644 --- a/setup/wizardsteps.class.inc.php +++ b/setup/wizardsteps.class.inc.php @@ -2616,6 +2616,11 @@ class WizStepDone extends WizardStep $oProductionEnv->InitDataModel($oConfig, true); $sIframeUrl = $oConfig->GetModuleSetting('itop-hub-connector', 'setup_url', ''); + $sSetupTokenFile = APPROOT.'data/.setup'; + $sSetupToken = bin2hex(random_bytes(12)); + file_put_contents($sSetupTokenFile, $sSetupToken); + $sIframeUrl.= "&setup_token=$sSetupToken"; + if ($sIframeUrl != '') { $oPage->add(''); diff --git a/tests/php-unit-tests/unitary-tests/datamodels/2.x/itop-hub-connector/TokenValidationTest.php b/tests/php-unit-tests/unitary-tests/datamodels/2.x/itop-hub-connector/TokenValidationTest.php new file mode 100644 index 000000000..8ae6da01e --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/datamodels/2.x/itop-hub-connector/TokenValidationTest.php @@ -0,0 +1,43 @@ +RequireOnceItopFile('datamodels/2.x/itop-hub-connector/TokenValidation.php'); + } + + public function testLaunch() + { + $oTokenValidation = new TokenValidation(); + + $sSetupToken = bin2hex(random_bytes(12)); + $this->assertFalse($oTokenValidation->isSetupTokenValid('lol')); + $this->assertFalse($oTokenValidation->isSetupTokenValid('')); + $this->assertFalse($oTokenValidation->isSetupTokenValid($sSetupToken)); + $this->createSetupTokenFile($sSetupToken); + $this->assertFalse($oTokenValidation->isSetupTokenValid('lol')); + $this->createSetupTokenFile($sSetupToken); + $this->assertFalse($oTokenValidation->isSetupTokenValid('')); + $this->createSetupTokenFile($sSetupToken); + $this->assertTrue($oTokenValidation->isSetupTokenValid($sSetupToken)); + } +}