ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
SecretKeyRotation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
42{
46 private array $older_keys;
47
48 public function __construct(
49 private SecretKey $current_key,
51 ) {
52 $this->older_keys = $older_keys;
53 }
54
66 public static function fromArtefact(string $path): self
67 {
68 $stored = is_file($path) ? @include $path : [];
69
70 $keys = array_map(
71 static fn(string $key): SecretKey => new SecretKey($key),
72 array_values(array_filter(is_array($stored) ? $stored : [], 'is_string'))
73 );
74
75 $current_key = array_shift($keys) ?? self::generatedKey();
76
77 return new self($current_key, ...$keys);
78 }
79
80 private static function generatedKey(): SecretKey
81 {
82 return new SecretKey(bin2hex(random_bytes(32)));
83 }
84
85 public function getAllKeys(): array
86 {
87 return array_merge([$this->current_key], $this->older_keys);
88 }
89
90 public function getOlderKeys(): array
91 {
92 return $this->older_keys;
93 }
94
95 public function getCurrentKey(): SecretKey
96 {
97 return $this->current_key;
98 }
99}
Key rotation can provide an extra layer of mitigation against an attacker discovering a secret key.
__construct(private SecretKey $current_key, SecretKey ... $older_keys)
static fromArtefact(string $path)
Load a rotation from a static PHP artefact written by the setup, newest key first.
Signatures are secured by the secret_key.
Definition: SecretKey.php:40