diff --git a/config/defaultSettings.json b/config/defaultSettings.json index 4313f565..971bf2b0 100644 --- a/config/defaultSettings.json +++ b/config/defaultSettings.json @@ -26,7 +26,7 @@ "saveReqTimeout": 10, "saveRetryMax": 3, "saveRetrySleep": 5, - "requireRunToken": false, + "runTokenHash": false, "refreshControl": 0 }, "storage": { diff --git a/inc/Client.php b/inc/Client.php index fd963e9f..c40d3aeb 100644 --- a/inc/Client.php +++ b/inc/Client.php @@ -144,15 +144,10 @@ public static function getNameValidationRegex() { public static function validateRunToken( TestSwarmContext $context, $runToken ) { $conf = $context->getConf(); - if ( !$conf->client->requireRunToken ) { + if ( !$conf->client->runTokenHash ) { return true; } - $cacheFile = $conf->storage->cacheDir . '/run_token_hash.cache'; - if ( !is_readable( $cacheFile ) ) { - throw new SwarmException( 'Configuration requires a runToken but none has been configured.' ); - } - $runTokenHash = trim( file_get_contents( $cacheFile ) ); - if ( $runToken !== null && $runTokenHash === sha1( $runToken ) ) { + if ( $runToken !== null && $conf->client->runTokenHash === sha1( $runToken ) ) { return true; } throw new SwarmException( 'This TestSwarm requires a run token. Either none was entered or it is invalid.' ); diff --git a/inc/actions/GetrunAction.php b/inc/actions/GetrunAction.php index 38d16e68..b89b251d 100644 --- a/inc/actions/GetrunAction.php +++ b/inc/actions/GetrunAction.php @@ -25,7 +25,7 @@ public function doAction() { } $runToken = $request->getVal( "run_token" ); - if ( $conf->client->requireRunToken && !$runToken ) { + if ( $conf->client->runTokenHash && !$runToken ) { $this->setError( "missing-parameters", "This TestSwarm does not allow unauthorized clients to join the swarm." ); return; } diff --git a/inc/actions/PingAction.php b/inc/actions/PingAction.php index edf76235..6e08d98e 100644 --- a/inc/actions/PingAction.php +++ b/inc/actions/PingAction.php @@ -25,7 +25,7 @@ public function doAction() { } $runToken = $request->getVal( 'run_token' ); - if ( $conf->client->requireRunToken && !$runToken ) { + if ( $conf->client->runTokenHash && !$runToken ) { $this->setError( 'missing-parameters', 'This TestSwarm does not allow unauthorized clients to join the swarm.' ); return; } diff --git a/inc/actions/SaverunAction.php b/inc/actions/SaverunAction.php index 102481e5..11c5bf35 100644 --- a/inc/actions/SaverunAction.php +++ b/inc/actions/SaverunAction.php @@ -34,7 +34,7 @@ public function doAction() { } $runToken = $request->getVal( 'run_token' ); - if ( $conf->client->requireRunToken && !$runToken ) { + if ( $conf->client->runTokenHash && !$runToken ) { $this->setError( 'missing-parameters', 'This TestSwarm does not allow unauthorized clients to join the swarm.' ); return; } diff --git a/inc/pages/HomePage.php b/inc/pages/HomePage.php index 66a60ec6..867bca71 100644 --- a/inc/pages/HomePage.php +++ b/inc/pages/HomePage.php @@ -41,7 +41,7 @@ protected function initContent() { . ''; $html .= '
'; - if ( !$conf->client->requireRunToken ) { + if ( !$conf->client->runTokenHash ) { if ( $browserInfo->isInSwarmUaIndex() ) { $auth = $context->getAuth(); $suggestedClientName = $auth ? $auth->project->id : ''; diff --git a/inc/pages/RunPage.php b/inc/pages/RunPage.php index 17510cbc..197ba47e 100644 --- a/inc/pages/RunPage.php +++ b/inc/pages/RunPage.php @@ -17,7 +17,7 @@ protected function initContent() { $runToken = null; - if ( $conf->client->requireRunToken ) { + if ( $conf->client->runTokenHash ) { $runToken = $request->getVal( "run_token" ); if ( !$runToken ) { return '
This swarm has restricted access to join the swarm.
'; diff --git a/scripts/generateRunToken.php b/scripts/generateRunToken.php new file mode 100644 index 00000000..5a7d0c62 --- /dev/null +++ b/scripts/generateRunToken.php @@ -0,0 +1,29 @@ +setDescription( + 'Generates a new run token for clients and its hash for the configuration file.' + ); + } + + protected function execute() { + $runToken = sha1( mt_rand() ); + $runTokenHash = sha1( $runToken ); + $this->out( "New run token: $runToken" ); + $this->out( "New token hash: $runTokenHash" ); + } +} + +$script = GenerateRunTokenScript::newFromContext( $swarmContext ); +$script->run(); diff --git a/scripts/refreshRunToken.php b/scripts/refreshRunToken.php deleted file mode 100644 index 6d495e74..00000000 --- a/scripts/refreshRunToken.php +++ /dev/null @@ -1,47 +0,0 @@ -setDescription( - 'Sets a (new) run token. Overwrites an existing token if there is one.' - . ' Running this script does not change any settings. For the token' - . ' requirement to be enforced, make sure you have `client.requireRunToken = true`' - . ' set in your configuration file.' - ); - } - - protected function execute() { - $this->timeWarningForScriptWill( 'invalidate any existing token' ); - - $cacheDir = $this->getContext()->getConf()->storage->cacheDir; - $cacheFile = $cacheDir . '/run_token_hash.cache'; - if ( file_exists( $cacheFile ) ) { - $deleted = unlink( $cacheFile ); - if ( !$deleted ) { - $this->error( "Deletion of cache file failed:\n$cacheFile" ); - } - } - $runToken = sha1( mt_rand() ); - $runTokenHash = sha1( $runToken ); - $saved = file_put_contents( $cacheFile, $runTokenHash ); - if ( $saved === false ) { - $this->error( "Saving of cache file failed:\n$cacheFile" ); - } - $this->out( - "Run token has been generated and stored in place.\nNew run token: $runToken" - ); - } -} - -$script = RefreshRunTokenScript::newFromContext( $swarmContext ); -$script->run();