Skip to content

Commit d02ccf4

Browse files
committed
Added HmacEncrypter class
1 parent 9ec7b68 commit d02ccf4

3 files changed

Lines changed: 89 additions & 29 deletions

File tree

src/Authentication/Authenticators/HmacSha256.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
use CodeIgniter\I18n\Time;
1818
use CodeIgniter\Shield\Authentication\AuthenticationException;
1919
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
20+
use CodeIgniter\Shield\Authentication\HMAC\HmacEncrypter;
2021
use CodeIgniter\Shield\Config\Auth;
21-
use CodeIgniter\Shield\Config\AuthToken;
2222
use CodeIgniter\Shield\Entities\User;
2323
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
2424
use CodeIgniter\Shield\Models\TokenLoginModel;
2525
use CodeIgniter\Shield\Models\UserIdentityModel;
2626
use CodeIgniter\Shield\Models\UserModel;
2727
use CodeIgniter\Shield\Result;
28-
use Config\Encryption;
29-
use Config\Services;
3028

3129
class HmacSha256 implements AuthenticatorInterface
3230
{
@@ -162,17 +160,8 @@ public function check(array $credentials): Result
162160
]);
163161
}
164162

165-
/** @var AuthToken $authConfig */
166-
$authConfig = config('AuthToken');
167-
$config = new Encryption();
168-
169-
$config->key = $authConfig->hmacEncryptionKey;
170-
$config->driver = $authConfig->hmacEncryptionDriver;
171-
$config->digest = $authConfig->hmacEncryptionDigest;
172-
173-
// decrypt secret key so signature can be validated
174-
$encryptor = Services::encrypter($config);
175-
$secretKey = $encryptor->decrypt(hex2bin($token->secret2));
163+
$encrypter = new HmacEncrypter();
164+
$secretKey = $encrypter->decrypt($token->secret2);
176165

177166
// Check signature...
178167
$hash = hash_hmac('sha256', $credentials['body'], $secretKey);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeIgniter\Shield\Authentication\HMAC;
6+
7+
use CodeIgniter\Encryption\EncrypterInterface;
8+
use CodeIgniter\Shield\Config\AuthToken;
9+
use Config\Encryption;
10+
use Config\Services;
11+
use Exception;
12+
13+
/**
14+
* HMAC Encrypter class
15+
*
16+
* This class handles the setup and configuration of the HMAC Encryption
17+
*/
18+
class HmacEncrypter
19+
{
20+
/**
21+
* Codeigniter Encrypter
22+
*/
23+
private EncrypterInterface $encrypter;
24+
25+
/**
26+
* Auth Token config
27+
*/
28+
private AuthToken $authConfig;
29+
30+
/**
31+
* Constructor
32+
* Setup encryption configuration
33+
*/
34+
public function __construct()
35+
{
36+
$this->authConfig = config('AuthToken');
37+
$config = new Encryption();
38+
39+
$config->key = $this->authConfig->hmacEncryptionKey;
40+
$config->driver = $this->authConfig->hmacEncryptionDriver;
41+
$config->digest = $this->authConfig->hmacEncryptionDigest;
42+
43+
// decrypt secret key so signature can be validated
44+
$this->encrypter = Services::encrypter($config);
45+
}
46+
47+
/**
48+
* Decrypt
49+
*
50+
* @param string $hexString Encrypted string in Hex format
51+
*
52+
* @return string Raw decrypted string
53+
*/
54+
public function decrypt(string $hexString): string
55+
{
56+
return $this->encrypter->decrypt(hex2bin($hexString));
57+
}
58+
59+
/**
60+
* Encrypt
61+
*
62+
* @param string $rawString Raw string to encrypt
63+
*
64+
* @return string Encrypted string in hex format
65+
*/
66+
public function encrypt(string $rawString): string
67+
{
68+
return bin2hex($this->encrypter->encrypt($rawString));
69+
}
70+
71+
/**
72+
* Generate Key
73+
*
74+
* @return string Secret Key in hexed format
75+
*
76+
* @throws Exception
77+
*/
78+
public function generateSecretKey(): string
79+
{
80+
return bin2hex(random_bytes($this->authConfig->hmacSecretKeyByteSize));
81+
}
82+
}

src/Models/UserIdentityModel.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
use CodeIgniter\Shield\Authentication\Authenticators\AccessTokens;
1818
use CodeIgniter\Shield\Authentication\Authenticators\HmacSha256;
1919
use CodeIgniter\Shield\Authentication\Authenticators\Session;
20+
use CodeIgniter\Shield\Authentication\HMAC\HmacEncrypter;
2021
use CodeIgniter\Shield\Authentication\Passwords;
21-
use CodeIgniter\Shield\Config\AuthToken;
2222
use CodeIgniter\Shield\Entities\AccessToken;
2323
use CodeIgniter\Shield\Entities\User;
2424
use CodeIgniter\Shield\Entities\UserIdentity;
2525
use CodeIgniter\Shield\Exceptions\LogicException;
2626
use CodeIgniter\Shield\Exceptions\ValidationException;
27-
use Config\Encryption;
28-
use Config\Services;
2927
use Exception;
3028
use Faker\Generator;
3129
use ReflectionException;
@@ -254,18 +252,9 @@ public function generateHmacToken(User $user, string $name, array $scopes = ['*'
254252
{
255253
$this->checkUserId($user);
256254

257-
/** @var AuthToken $authConfig */
258-
$authConfig = config('AuthToken');
259-
$config = new Encryption();
260-
261-
$config->key = $authConfig->hmacEncryptionKey;
262-
$config->driver = $authConfig->hmacEncryptionDriver;
263-
$config->digest = $authConfig->hmacEncryptionDigest;
264-
265-
// Generate and encrypt secret key
266-
$encrypter = Services::encrypter($config);
267-
$rawSecretKey = bin2hex(random_bytes(config('AuthToken')->hmacSecretKeyByteSize));
268-
$secretKey = bin2hex($encrypter->encrypt($rawSecretKey));
255+
$encrypter = new HmacEncrypter();
256+
$rawSecretKey = $encrypter->generateSecretKey();
257+
$secretKey = $encrypter->encrypt($rawSecretKey);
269258

270259
$return = $this->insert([
271260
'type' => HmacSha256::ID_TYPE_HMAC_TOKEN,

0 commit comments

Comments
 (0)