Skip to content

Commit 6d61e6d

Browse files
committed
Adjustment to test scripts to account for encryption.
1 parent 6c7c18e commit 6d61e6d

4 files changed

Lines changed: 44 additions & 7 deletions

File tree

docs/guides/api_hmac_keys.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ $user->revokeHmacToken($key);
8787
$user->revokeAllHmacTokens();
8888
```
8989

90+
## HMAC Secret Key Encryption
91+
92+
The HMAC Secret Key is stored encrypted. Before you start using HMAC, you will need to set/override the encryption key
93+
`$hmacEncryptionKey` in **app/Config/AuthToken.php**. This should be set using .env and/or system environment variables.
94+
Instructions on how to do that can be found in the
95+
[Setting Your Encryption Key](https://codeigniter.com/user_guide/libraries/encryption.html#setting-your-encryption-key)
96+
section of the CodeIgniter 4 documentation.
97+
98+
You will also be able to adjust the default Driver `$hmacEncryptionDriver` and the default Digest
99+
`$hmacEncryptionDigest`, these default to `'OpenSSL'` and `'SHA512'` respectively.
100+
101+
90102
## Protecting Routes
91103

92104
The first way to specify which routes are protected is to use the `hmac` controller filter.

docs/references/authentication/hmac.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,14 @@ if ($user->hmacTokenCant('forums.manage')) {
156156
// do something....
157157
}
158158
```
159+
160+
## HMAC Secret Key Encryption
161+
162+
The HMAC Secret Key is stored encrypted. Before you start using HMAC, you will need to set/override the encryption key
163+
`$hmacEncryptionKey` in **app/Config/AuthToken.php**. This should be set using .env and/or system environment variables.
164+
Instructions on how to do that can be found in the
165+
[Setting Your Encryption Key](https://codeigniter.com/user_guide/libraries/encryption.html#setting-your-encryption-key)
166+
section of the CodeIgniter 4 documentation.
167+
168+
You will also be able to adjust the default Driver `$hmacEncryptionDriver` and the default Digest
169+
`$hmacEncryptionDigest`, these default to `'OpenSSL'` and `'SHA512'` respectively.

tests/Authentication/Filters/HmacFilterTest.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Tests\Authentication\Filters;
1515

16+
use CodeIgniter\Encryption\Encryption;
1617
use CodeIgniter\Shield\Entities\AccessToken;
1718
use CodeIgniter\Shield\Entities\User;
1819
use CodeIgniter\Shield\Filters\HmacAuth;
@@ -30,6 +31,14 @@ final class HmacFilterTest extends AbstractFilterTestCase
3031
protected string $alias = 'hmacAuth';
3132
protected string $classname = HmacAuth::class;
3233

34+
protected function setUp(): void
35+
{
36+
parent::setUp();
37+
38+
$authConfig = config('AuthToken');
39+
$authConfig->hmacEncryptionKey = Encryption::createKey();
40+
}
41+
3342
public function testFilterNotAuthorized(): void
3443
{
3544
$result = $this->call('get', 'protected-route');
@@ -47,7 +56,7 @@ public function testFilterSuccess(): void
4756
$user = fake(UserModel::class);
4857
$token = $user->generateHmacToken('foo');
4958

50-
$rawToken = $this->generateRawHeaderToken($token->secret, $token->secret2, '');
59+
$rawToken = $this->generateRawHeaderToken($token->secret, $token->rawSecretKey, '');
5160
$result = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $rawToken])
5261
->get('protected-route');
5362

@@ -68,7 +77,7 @@ public function testFilterInvalidSignature(): void
6877
$user = fake(UserModel::class);
6978
$token = $user->generateHmacToken('foo');
7079

71-
$result = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token->secret, $token->secret2, 'bar')])
80+
$result = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token->secret, $token->rawSecretKey, 'bar')])
7281
->get('protected-route');
7382

7483
$result->assertStatus(401);
@@ -80,7 +89,7 @@ public function testRecordActiveDate(): void
8089
$user = fake(UserModel::class);
8190
$token = $user->generateHmacToken('foo');
8291

83-
$this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token->secret, $token->secret2, '')])
92+
$this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token->secret, $token->rawSecretKey, '')])
8493
->get('protected-route');
8594

8695
// Last Active should be greater than 'updated_at' column
@@ -97,15 +106,15 @@ public function testFiltersProtectsWithScopes(): void
97106
$token2 = $user2->generateHmacToken('foo', ['users-write']);
98107

99108
// User 1 should be able to access the route
100-
$result1 = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token1->secret, $token1->secret2, '')])
109+
$result1 = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token1->secret, $token1->rawSecretKey, '')])
101110
->get('protected-user-route');
102111

103112
$result1->assertStatus(200);
104113
// Last Active should be greater than 'updated_at' column
105114
$this->assertGreaterThan(auth('hmac')->user()->updated_at, auth('hmac')->user()->last_active);
106115

107116
// User 2 should NOT be able to access the route
108-
$result2 = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token2->secret, $token2->secret2, '')])
117+
$result2 = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token2->secret, $token2->rawSecretKey, '')])
109118
->get('protected-user-route');
110119

111120
$result2->assertStatus(401);
@@ -120,7 +129,7 @@ public function testBlocksInactiveUsers(): void
120129
// Activation only required with email activation
121130
setting('Auth.actions', ['register' => null]);
122131

123-
$result = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token->secret, $token->secret2, '')])
132+
$result = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token->secret, $token->rawSecretKey, '')])
124133
->get('protected-route');
125134

126135
$result->assertStatus(200);
@@ -129,7 +138,7 @@ public function testBlocksInactiveUsers(): void
129138
// Now require user activation and try again
130139
setting('Auth.actions', ['register' => '\CodeIgniter\Shield\Authentication\Actions\EmailActivator']);
131140

132-
$result = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token->secret, $token->secret2, '')])
141+
$result = $this->withHeaders(['Authorization' => 'HMAC-SHA256 ' . $this->generateRawHeaderToken($token->secret, $token->rawSecretKey, '')])
133142
->get('protected-route');
134143

135144
$result->assertStatus(403);

tests/Authentication/HasHmacTokensTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Tests\Authentication;
1515

16+
use CodeIgniter\Encryption\Encryption;
1617
use CodeIgniter\Shield\Entities\AccessToken;
1718
use CodeIgniter\Shield\Entities\User;
1819
use CodeIgniter\Shield\Models\UserIdentityModel;
@@ -30,6 +31,9 @@ protected function setUp(): void
3031
{
3132
parent::setUp();
3233

34+
$authConfig = config('AuthToken');
35+
$authConfig->hmacEncryptionKey = Encryption::createKey();
36+
3337
$this->user = fake(UserModel::class);
3438
$this->db->table($this->tables['identities'])->truncate();
3539
}
@@ -43,6 +47,7 @@ public function testGenerateHmacToken(): void
4347

4448
$this->assertIsString($token->secret);
4549
$this->assertIsString($token->secret2);
50+
$this->assertIsString($token->rawSecretKey);
4651

4752
// All scopes are assigned by default via wildcard
4853
$this->assertSame(['*'], $token->scopes);

0 commit comments

Comments
 (0)