88
99namespace OCA \Libresign \Handler ;
1010
11+ use Closure ;
1112use OCA \Libresign \Exception \LibresignException ;
1213
1314class CfsslServerHandler {
14- private string $ csrServerFile ;
15- private string $ configServerFile ;
16- private string $ configServerFileHash ;
17- public function __construct (string $ configPath ) {
18- $ this ->csrServerFile = $ configPath . DIRECTORY_SEPARATOR . 'csr_server.json ' ;
19- $ this ->configServerFile = $ configPath . DIRECTORY_SEPARATOR . 'config_server.json ' ;
20- $ this ->configServerFileHash = $ configPath . DIRECTORY_SEPARATOR . 'hashes.sha256 ' ;
15+ private string $ csrServerFile = '' ;
16+ private string $ configServerFile = '' ;
17+ private string $ configServerFileHash = '' ;
18+ private Closure $ getConfigPath ;
19+
20+ /**
21+ * Create a callback to get config path not at the constructor because
22+ * getting at constructor, every time that the class is instantiated, will
23+ * try to create the config path if not exists.
24+ */
25+ public function configCallback (Closure $ callback ): void {
26+ $ this ->getConfigPath = $ callback ;
27+ $ this ->getConfigPath = function () use ($ callback ) {
28+ if ($ this ->csrServerFile ) {
29+ return ;
30+ }
31+ $ configPath = $ callback ();
32+ $ this ->csrServerFile = $ configPath . DIRECTORY_SEPARATOR . 'csr_server.json ' ;
33+ $ this ->configServerFile = $ configPath . DIRECTORY_SEPARATOR . 'config_server.json ' ;
34+ $ this ->configServerFileHash = $ configPath . DIRECTORY_SEPARATOR . 'hashes.sha256 ' ;
35+ };
2136 }
2237
2338 public function createConfigServer (
@@ -47,6 +62,7 @@ private function putCsrServer(
4762 foreach ($ names as $ id => $ name ) {
4863 $ content ['names ' ][0 ][$ id ] = $ name ['value ' ];
4964 }
65+ ($ this ->getConfigPath )();
5066 $ response = file_put_contents ($ this ->csrServerFile , json_encode ($ content ));
5167 if ($ response === false ) {
5268 throw new LibresignException (
@@ -87,6 +103,7 @@ private function saveNewConfig(string $key, int $expirity): void {
87103
88104 private function saveConfig (array $ config ): void {
89105 $ jsonConfig = json_encode ($ config );
106+ ($ this ->getConfigPath )();
90107 $ response = file_put_contents ($ this ->configServerFile , $ jsonConfig );
91108 if ($ response === false ) {
92109 throw new LibresignException ('Error while writing config server file! ' , 500 );
@@ -96,6 +113,7 @@ private function saveConfig(array $config): void {
96113 }
97114
98115 public function updateExpirity (int $ expirity ): void {
116+ ($ this ->getConfigPath )();
99117 if (file_exists ($ this ->configServerFileHash )) {
100118 $ hashes = file_get_contents ($ this ->configServerFileHash );
101119 preg_match ('/(?<hash>\w*) +config_server.json/ ' , $ hashes , $ matches );
0 commit comments