ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
KeyRotationObjective.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\FileDelivery\Setup;
22 
24 use ILIAS\Setup;
25 
30 {
31  public const KEY_ROTATION = './src/FileDelivery/artifacts/key_rotation.php';
32  public const KEY_LENGTH = 32;
33  private const NUMBER_OF_KEYS = 5;
34 
35  public function getArtifactPath(): string
36  {
37  return self::KEY_ROTATION;
38  }
39 
40  public function build(): Setup\Artifact
41  {
42  $current_keys = null;
43  if (is_readable(self::KEY_ROTATION)) {
45  $current_keys = require self::KEY_ROTATION;
46  }
47 
48  $new_keys = [];
49 
50  if (is_array($current_keys)) {
51  // drop the first key
52  $current_keys = array_slice($current_keys, 1);
53  $new_keys = $current_keys;
54  }
55  // $push a new key to the array at first position
56  while (count($new_keys) < self::NUMBER_OF_KEYS) {
57  $new_keys[] = $this->generateRandomString(self::KEY_LENGTH);
58  }
59  // keep only the first 5 keys
60  $new_keys = array_slice($new_keys, 0, self::NUMBER_OF_KEYS);
61 
62  return new Setup\Artifact\ArrayArtifact($new_keys);
63  }
64 
65  private function generateRandomString(int $length): string
66  {
67  $return = '';
68  for ($i = 0; $i < $length; $i++) {
69  $return .= chr(random_int(33, 125));
70  }
71  return $return;
72  }
73 }
build()
Build the artifact based.
This is an objective to build some artifact.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An artifact is some file that is build on demand per installation and is not shipped with the ILIAS s...
Definition: Artifact.php:27