Skip to content

Commit 20bc9f9

Browse files
committed
Updated documentation to reflect encryption key updates.
1 parent a68eb63 commit 20bc9f9

4 files changed

Lines changed: 73 additions & 5 deletions

File tree

UPGRADING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ If you are using the HMAC authentication you need to update the encryption setti
5656
You will need to update and set the encryption key `$hmacEncryptionKey`. This should be set using **.env** and/or system
5757
environment variables. Instructions on how to do that can be found in the
5858
[Setting Your Encryption Key](https://codeigniter.com/user_guide/libraries/encryption.html#setting-your-encryption-key)
59-
section of the CodeIgniter 4 documentation.
59+
section of the CodeIgniter 4 documentation and in [HMAC SHA256 Token Authenticator](./docs/references/authentication/hmac.md#hmac-secret-key-encryption).
6060

6161
You also may wish to adjust the default Driver `$hmacEncryptionDriver` and the default Digest `$hmacEncryptionDigest`,
6262
these currently default to `'OpenSSL'` and `'SHA512'` respectively.

docs/guides/api_hmac_keys.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,13 @@ section of the CodeIgniter 4 documentation.
9999
You will also be able to adjust the default Driver `$hmacEncryptionDriver` and the default Digest
100100
`$hmacEncryptionDigest`, these default to `'OpenSSL'` and `'SHA512'` respectively.
101101

102-
Depending on the set length of the Secret Key and the type of encryption used, it is possible for the encrypted value to
103-
exceed the database column character limit of 255 characters. If this happens, creation of a new HMAC identity will
104-
throw a `RuntimeException`.
102+
See [HMAC SHA256 Token Authenticator](../references/authentication/hmac.md#hmac-secret-key-encryption) for additional
103+
details on setting these values.
104+
105+
### Encryption Key Rotation
106+
107+
See [HMAC SHA256 Token Authenticator](../references/authentication/hmac.md#hmac-secret-key-encryption) for information on
108+
how to set, rotate encryption keys and re-encrypt existing HMAC `'secretKey'` values.
105109

106110
## Protecting Routes
107111

docs/references/authentication/hmac.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,65 @@ Instructions on how to do that can be found in the
166166
section of the CodeIgniter 4 documentation.
167167

168168
You will also be able to adjust the default Driver `$hmacEncryptionDriver` and the default Digest
169-
`$hmacEncryptionDigest`, these default to `'OpenSSL'` and `'SHA512'` respectively.
169+
`$hmacEncryptionDigest`, these default to `'OpenSSL'` and `'SHA512'` respectively. All three properties (`$hmacEncryptionKey`,
170+
`$hmacEncryptionDriver`, and `$hmacEncryptionDigest`) are set in array format (see below).
171+
172+
```php
173+
public $hmacEncryptionKey = [
174+
'k1' => 'hex2bin:923dfab5ddca0c7784c2c388a848a704f5e048736c1a852c862959da62ade8c7',
175+
];
176+
177+
public $hmacEncryptionDriver = [
178+
'k1' => 'OpenSSL',
179+
];
180+
181+
public $hmacEncryptionDigest = [
182+
'k1' => 'SHA512',
183+
];
184+
185+
public string $hmacKeyIndex = 'k1';
186+
```
187+
188+
When it is time to update your encryption keys you will need to add an additional key to the above arrays. Then adjust
189+
the `$hmacKeyIndex` to point at the new key and adjust `$hmacDeprecatedKeyIndex` to point at the old key. After the new
190+
encryption key is in place, run `php spark shield:hmac reencrypt` to re-encrypt all existing keys with the new encryption
191+
key.
192+
193+
```php
194+
public $hmacEncryptionKey = [
195+
'k1' => 'hex2bin:923dfab5ddca0c7784c2c388a848a704f5e048736c1a852c862959da62ade8c7',
196+
'k2' => 'hex2bin:451df599363b19be1434605fff8556a0bbfc50bede1bb33793dcde4d97fce4b0',
197+
];
198+
199+
public $hmacEncryptionDriver = [
200+
'k1' => 'OpenSSL',
201+
'k2' => 'OpenSSL',
202+
];
203+
204+
public $hmacEncryptionDigest = [
205+
'k1' => 'SHA512',
206+
'k2' => 'SHA512',
207+
];
208+
209+
public string $hmacKeyIndex = 'k2';
210+
public string $hmacDeprecatedKeyIndex = 'k1';
211+
```
212+
213+
```shell
214+
php spark shield:hmac reencrypt
215+
```
216+
217+
You can (and should) set these values using environment variable and/or the `.env` file. To do this you will need to set
218+
the values as JSON strings:
219+
220+
```dotenv
221+
authtoken.hmacEncryptionKey = '{"k1":"hex2bin:923dfab5ddca0c7784c2c388a848a704f5e048736c1a852c862959da62ade8c7","k2":"hex2bin:451df599363b19be1434605fff8556a0bbfc50bede1bb33793dcde4d97fce4b0"}'
222+
authtoken.hmacEncryptionDriver = '{"k1":"OpenSSL","k2":"OpenSSL"}'
223+
authtoken.hmacEncryptionDigest = '{"k1":"SHA512","k2":"SHA512"}'
224+
authtoken.hmacKeyIndex = k2
225+
authtoken.hmacDeprecatedKeyIndex = k1
226+
```
227+
228+
Depending on the set length of the Secret Key and the type of encryption used, it is possible for the encrypted value to
229+
exceed the database column character limit of 255 characters. If this happens, creation of a new HMAC identity will
230+
throw a `RuntimeException`.

src/Config/AuthToken.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ class AuthToken extends BaseConfig
152152
*/
153153
public string $hmacDeprecatedKeyIndex = '';
154154

155+
/**
156+
* AuthToken Config Constructor
157+
*/
155158
public function __construct()
156159
{
157160
parent::__construct();

0 commit comments

Comments
 (0)