*/ declare(strict_types=1); namespace oat\tao\scripts\tools; use common_report_Report as Report; use Exception; use InvalidArgumentException; use oat\oatbox\extension\script\ScriptAction; use oat\tao\model\security\xsrf\TokenService; class SetupClientCsrfTokenPoolStore extends ScriptAction { private const OPTION_STORE = 'store'; protected function provideOptions(): array { return [ self::OPTION_STORE => [ 'prefix' => 's', 'longPrefix' => self::OPTION_STORE, 'required' => true, 'description' => "One of {$this->createFormattedStoreOptionValues()}", ], ]; } protected function provideDescription(): string { return 'Set a client-side CSRF token pool store preference.'; } protected function run(): Report { try { /** @var TokenService $tokenService */ $tokenService = $this->getServiceLocator()->get(TokenService::class); $storeOption = $this->getStoreOption(); $tokenService->setOptions( array_replace( $tokenService->getOptions(), [ TokenService::OPTION_CLIENT_STORE => $storeOption, ] ) ); $this->getServiceManager()->register(TokenService::SERVICE_ID, $tokenService); } catch (Exception $exception) { return Report::createFailure($exception->getMessage()); } return Report::createSuccess( sprintf( 'Set "%s"\'s "%s" option to "%s"', TokenService::class, TokenService::OPTION_CLIENT_STORE, $storeOption ) ); } private function getStoreOption(): string { $value = $this->getOption(self::OPTION_STORE); if (!in_array($value, TokenService::CLIENT_STORE_OPTION_VALUES, true)) { throw new InvalidArgumentException( sprintf( '"%s" must be one of %s, "%s" given.', self::OPTION_STORE, $this->createFormattedStoreOptionValues(), $value ) ); } return $value; } /** * @return string */ private function createFormattedStoreOptionValues(): string { return sprintf('"%s"', implode('", "', TokenService::CLIENT_STORE_OPTION_VALUES)); } }