@@ -78,9 +78,20 @@ class AuthToken extends BaseConfig
7878 * --------------------------------------------------------------------
7979 * Key to be used when encrypting HMAC Secret Key for storage.
8080 *
81+ * This is an array of keys which will facilitate key rotation. Valid
82+ * keyTitles must include only [a-zA-z0-9_] and should be kept to a
83+ * max of 8 characters.
84+ *
85+ * The valid and old/deprecated keys are identified using $hmacKeyIndex
86+ * and $hmacDeprecatedKeyIndex.
87+ *
88+ * @param array<string, string>|string $hmacEncryptionKey ['keyTitle' => 'keyValue']
89+ *
8190 * @see https://codeigniter.com/user_guide/libraries/encryption.html
8291 */
83- public string $ hmacEncryptionKey = '' ;
92+ public $ hmacEncryptionKey = [
93+ 'k1 ' => '' ,
94+ ];
8495
8596 /**
8697 * --------------------------------------------------------------------
@@ -92,9 +103,16 @@ class AuthToken extends BaseConfig
92103 * OpenSSL
93104 * Sodium
94105 *
106+ * This is an array of drivers values. The keys MUST match and correlate
107+ * to the $hmacEncryptionKey array keys.
108+ *
109+ * @param array<string, string>|string $hmacEncryptionDriver ['keyTitle' => 'driverValue']
110+ *
95111 * @see https://codeigniter.com/user_guide/libraries/encryption.html
96112 */
97- public string $ hmacEncryptionDriver = 'OpenSSL ' ;
113+ public $ hmacEncryptionDriver = [
114+ 'k1 ' => 'OpenSSL ' ,
115+ ];
98116
99117 /**
100118 * --------------------------------------------------------------------
@@ -104,7 +122,79 @@ class AuthToken extends BaseConfig
104122 *
105123 * e.g. 'SHA512' or 'SHA256'. Default value is 'SHA512'.
106124 *
125+ * This is an array of digest values. The keys MUST match and correlate
126+ * to the $hmacEncryptionKey array keys.
127+ *
128+ * @param array<string, string>|string $hmacEncryptionDigest ['keyTitle' => 'digestValue']
129+ *
107130 * @see https://codeigniter.com/user_guide/libraries/encryption.html
108131 */
109- public string $ hmacEncryptionDigest = 'SHA512 ' ;
132+ public $ hmacEncryptionDigest = [
133+ 'k1 ' => 'SHA512 ' ,
134+ ];
135+
136+ /**
137+ * --------------------------------------------------------------------
138+ * HMAC encryption key selector
139+ * --------------------------------------------------------------------
140+ * This identifies which encryption key {$hmacEncryptionKey} is active
141+ * and valid.
142+ */
143+ public string $ hmacKeyIndex = 'k1 ' ;
144+
145+ /**
146+ * --------------------------------------------------------------------
147+ * HMAC encryption key deprecated selector
148+ * --------------------------------------------------------------------
149+ * This identifies which encryption key {$hmacEncryptionKey} is
150+ * recently deprecated. This is required and used when rotating keys.
151+ * Effectively, this is the index selector for the old key.
152+ */
153+ public string $ hmacDeprecatedKeyIndex = '' ;
154+
155+ public function __construct ()
156+ {
157+ parent ::__construct ();
158+
159+ if (is_string ($ this ->hmacEncryptionKey )) {
160+ $ array = json_decode ($ this ->hmacEncryptionKey , true );
161+ if (is_array ($ array )) {
162+ $ this ->hmacEncryptionKey = $ array ;
163+ }
164+ }
165+ if (is_string ($ this ->hmacEncryptionDriver )) {
166+ $ array = json_decode ($ this ->hmacEncryptionDriver , true );
167+ if (is_array ($ array )) {
168+ $ this ->hmacEncryptionDriver = $ array ;
169+ }
170+ }
171+ if (is_string ($ this ->hmacEncryptionDigest )) {
172+ $ array = json_decode ($ this ->hmacEncryptionDigest , true );
173+ if (is_array ($ array )) {
174+ $ this ->hmacEncryptionDigest = $ array ;
175+ }
176+ }
177+ }
178+
179+ /**
180+ * Override parent initEnvValue() to allow for direct setting to array properties values from ENV
181+ *
182+ * In order to set array properties via ENV vars we need to set the property to a string value first.
183+ *
184+ * @param mixed $property
185+ */
186+ protected function initEnvValue (&$ property , string $ name , string $ prefix , string $ shortPrefix ): void
187+ {
188+ switch ($ name ) {
189+ case 'hmacEncryptionKey ' :
190+ case 'hmacEncryptionDriver ' :
191+ case 'hmacEncryptionDigest ' :
192+ // if attempting to set property from ENV, first set to empty string
193+ if ((bool ) $ this ->getEnvValue ($ name , $ prefix , $ shortPrefix )) {
194+ $ property = '' ;
195+ }
196+ }
197+
198+ parent ::initEnvValue ($ property , $ name , $ prefix , $ shortPrefix );
199+ }
110200}
0 commit comments