-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathHmac.php
More file actions
232 lines (191 loc) · 6.71 KB
/
Copy pathHmac.php
File metadata and controls
232 lines (191 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield\Commands;
use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Authentication\HMAC\HmacEncrypter;
use CodeIgniter\Shield\Commands\Exceptions\BadInputException;
use CodeIgniter\Shield\Exceptions\RuntimeException;
use CodeIgniter\Shield\Models\UserIdentityModel;
use Exception;
use ReflectionException;
class Hmac extends BaseCommand
{
/**
* The Command's name
*
* @var string
*/
protected $name = 'shield:hmac';
/**
* the Command's short description
*
* @var string
*/
protected $description = 'Encrypt/Decrypt secretKey for HMAC tokens.';
/**
* the Command's usage
*
* @var string
*/
protected $usage = <<<'EOL'
shield:hmac <action>
shield:hmac reencrypt
shield:hmac encrypt
shield:hmac decrypt
shield:hmac invalidateAll
The reencrypt command should be used when rotating the encryption keys.
The encrypt command should only be run on existing raw secret keys (extremely rare).
The invalidateAll command should only be run if you need to invalidate ALL HMAC Tokens (for everyone).
EOL;
/**
* the Command's Arguments
*
* @var array<string, string>
*/
protected $arguments = [
'action' => <<<'EOL'
reencrypt: Re-encrypts all HMAC Secret Keys on encryption key rotation
encrypt: Encrypt all raw HMAC Secret Keys
decrypt: Decrypt all encrypted HMAC Secret Keys
invalidateAll: Invalidates all HMAC Keys/Tokens (for everyone)
EOL,
];
/**
* HMAC Encrypter Object
*/
private HmacEncrypter $encrypter;
/**
* the Command's Options
*
* @var array<string, string>
*/
protected $options = [];
/**
* Run Encryption Methods
*/
public function run(array $params): int
{
$action = $params[0] ?? null;
$this->encrypter = new HmacEncrypter();
try {
match ($action) {
'encrypt' => $this->encrypt(),
'decrypt' => $this->decrypt(),
'reencrypt' => $this->reEncrypt(),
'invalidateAll' => $this->invalidateAll(),
default => throw new BadInputException('Unrecognized Command'),
};
} catch (Exception $e) {
$this->write($e->getMessage(), 'red');
return EXIT_ERROR;
}
return EXIT_SUCCESS;
}
/**
* Encrypt all Raw HMAC Secret Keys
*
* @throws ReflectionException
*/
public function encrypt(): void
{
$uIdModel = new UserIdentityModel();
$uIdModelSub = new UserIdentityModel(); // For saving.
$encrypter = $this->encrypter;
$that = $this;
$uIdModel->where('type', 'hmac_sha256')->orderBy('id')->chunk(
100,
static function ($identity) use ($uIdModelSub, $encrypter, $that): void {
if ($encrypter->isEncrypted($identity->secret2)) {
$that->write('id: ' . $identity->id . ', already encrypted, skipped.');
return;
}
try {
$identity->secret2 = $encrypter->encrypt($identity->secret2);
$uIdModelSub->save($identity);
$that->write('id: ' . $identity->id . ', encrypted.');
} catch (RuntimeException $e) {
$that->error('id: ' . $identity->id . ', ' . $e->getMessage());
}
},
);
}
/**
* Decrypt all encrypted HMAC Secret Keys
*
* @throws ReflectionException
*/
public function decrypt(): void
{
$uIdModel = new UserIdentityModel();
$uIdModelSub = new UserIdentityModel(); // For saving.
$encrypter = $this->encrypter;
$that = $this;
$uIdModel->where('type', 'hmac_sha256')->orderBy('id')->chunk(
100,
static function ($identity) use ($uIdModelSub, $encrypter, $that): void {
if (! $encrypter->isEncrypted($identity->secret2)) {
$that->write('id: ' . $identity->id . ', not encrypted, skipped.');
return;
}
$identity->secret2 = $encrypter->decrypt($identity->secret2);
$uIdModelSub->save($identity);
$that->write('id: ' . $identity->id . ', decrypted.');
},
);
}
/**
* Re-encrypt all encrypted HMAC Secret Keys from existing/deprecated
* encryption key to new encryption key.
*
* @throws ReflectionException
*/
public function reEncrypt(): void
{
$uIdModel = new UserIdentityModel();
$uIdModelSub = new UserIdentityModel(); // For saving.
$encrypter = $this->encrypter;
$that = $this;
$uIdModel->where('type', 'hmac_sha256')->orderBy('id')->chunk(
100,
static function ($identity) use ($uIdModelSub, $encrypter, $that): void {
if ($encrypter->isEncryptedWithCurrentKey($identity->secret2)) {
$that->write('id: ' . $identity->id . ', already encrypted with current key, skipped.');
return;
}
$identity->secret2 = $encrypter->decrypt($identity->secret2);
$identity->secret2 = $encrypter->encrypt($identity->secret2);
$uIdModelSub->save($identity);
$that->write('id: ' . $identity->id . ', Re-encrypted.');
},
);
}
/**
* Invalidates all HMAC Keys/Tokens for every user.
*/
public function invalidateAll(): void
{
$uIdModel = new UserIdentityModel();
$uIdModelSub = new UserIdentityModel();
$uIdModel->where('type', 'hmac_sha256')->orderBy('id')->chunk(
100,
function ($identity) use ($uIdModelSub): void {
$timeNow = Time::now();
if (null !== $identity->expires && $identity->expires->isBefore($timeNow)) {
$this->write('HMAC Token ID: ' . $identity->id . ', already expired, skipped.');
return;
}
$identity->expires = $timeNow;
$uIdModelSub->save($identity);
$this->write('HMAC Token ID: ' . $identity->id . ', set as expired.');
},
);
}
}