|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CodeIgniter\Shield\Commands; |
| 6 | + |
| 7 | +use CodeIgniter\Shield\Authentication\HMAC\HmacEncrypter; |
| 8 | +use CodeIgniter\Shield\Commands\Exceptions\BadInputException; |
| 9 | +use CodeIgniter\Shield\Models\UserIdentityModel; |
| 10 | +use ReflectionException; |
| 11 | + |
| 12 | +class Hmac extends BaseCommand |
| 13 | +{ |
| 14 | + /** |
| 15 | + * The Command's name |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $name = 'shield:hmac'; |
| 20 | + |
| 21 | + /** |
| 22 | + * the Command's short description |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $description = 'Encrypt/Decrypt secretKey for HMAC tokens. The encryption should only be run on existing raw secret keys (extremely rare).'; |
| 27 | + |
| 28 | + /** |
| 29 | + * the Command's usage |
| 30 | + * |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $usage = <<<'EOL' |
| 34 | + shield:hmac <action> |
| 35 | + shield:hmac encrypt |
| 36 | + shield:hmac decrypt |
| 37 | + EOL; |
| 38 | + |
| 39 | + /** |
| 40 | + * the Command's Arguments |
| 41 | + * |
| 42 | + * @var array |
| 43 | + */ |
| 44 | + protected $arguments = [ |
| 45 | + 'action' => <<<'EOL' |
| 46 | + encrypt: Encrypt all raw HMAC Secret Keys |
| 47 | + decrypt: Decrypt all encrypted HMAC Secret Keys |
| 48 | + EOL, |
| 49 | + ]; |
| 50 | + |
| 51 | + /** |
| 52 | + * HMAC Encrypter Object |
| 53 | + */ |
| 54 | + private HmacEncrypter $encrypter; |
| 55 | + |
| 56 | + /** |
| 57 | + * the Command's Options |
| 58 | + * |
| 59 | + * @var array |
| 60 | + */ |
| 61 | + protected $options = []; |
| 62 | + |
| 63 | + /** |
| 64 | + * Run Encryption Methods |
| 65 | + */ |
| 66 | + public function run(array $params): int |
| 67 | + { |
| 68 | + $action = $params[0] ?? null; |
| 69 | + |
| 70 | + $this->encrypter = new HmacEncrypter(); |
| 71 | + |
| 72 | + try { |
| 73 | + switch ($action) { |
| 74 | + case 'encrypt': |
| 75 | + $this->encrypt(); |
| 76 | + break; |
| 77 | + |
| 78 | + case 'decrypt': |
| 79 | + $this->decrypt(); |
| 80 | + break; |
| 81 | + |
| 82 | + default: |
| 83 | + throw new BadInputException('Unrecognized Command'); |
| 84 | + } |
| 85 | + } catch (BadInputException|ReflectionException $e) { |
| 86 | + $this->write($e->getMessage(), 'red'); |
| 87 | + |
| 88 | + return EXIT_ERROR; |
| 89 | + } |
| 90 | + |
| 91 | + return EXIT_SUCCESS; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Encrypt all Raw HMAC Secret Keys |
| 96 | + * |
| 97 | + * @throws ReflectionException |
| 98 | + */ |
| 99 | + public function encrypt(): void |
| 100 | + { |
| 101 | + $uIdModel = new UserIdentityModel(); |
| 102 | + $encrypter = $this->encrypter; |
| 103 | + |
| 104 | + $uIdModel->where('type', 'hmac_sha256')->chunk( |
| 105 | + 100, |
| 106 | + static function ($identity) use ($uIdModel, $encrypter): void { |
| 107 | + $identity->secret2 = $encrypter->encrypt($identity->secret2); |
| 108 | + |
| 109 | + $uIdModel->save($identity); |
| 110 | + } |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Decrypt all encrypted HMAC Secret Keys |
| 116 | + * |
| 117 | + * @throws ReflectionException |
| 118 | + */ |
| 119 | + public function decrypt(): void |
| 120 | + { |
| 121 | + $uIdModel = new UserIdentityModel(); |
| 122 | + $encrypter = $this->encrypter; |
| 123 | + |
| 124 | + $uIdModel->where('type', 'hmac_sha256')->chunk( |
| 125 | + 100, |
| 126 | + static function ($identity) use ($uIdModel, $encrypter): void { |
| 127 | + $identity->secret2 = $encrypter->decrypt($identity->secret2); |
| 128 | + |
| 129 | + $uIdModel->save($identity); |
| 130 | + } |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments