1
0

GenerateInstanceActor.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Schema;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\InstanceActor;
  7. use Cache;
  8. class GenerateInstanceActor extends Command
  9. {
  10. protected $signature = 'instance:actor';
  11. protected $description = 'Generate instance actor';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function handle()
  17. {
  18. if(Schema::hasTable('instance_actors') == false) {
  19. $this->line(' ');
  20. $this->error('Missing instance_actors table.');
  21. $this->info('Run "php artisan migrate" and try again.');
  22. $this->line(' ');
  23. exit;
  24. }
  25. if(InstanceActor::exists()) {
  26. $actor = InstanceActor::whereNotNull('public_key')
  27. ->whereNotNull('private_key')
  28. ->firstOrFail();
  29. Cache::rememberForever(InstanceActor::PKI_PUBLIC, function() use($actor) {
  30. return $actor->public_key;
  31. });
  32. Cache::rememberForever(InstanceActor::PKI_PRIVATE, function() use($actor) {
  33. return $actor->private_key;
  34. });
  35. $this->info('Instance actor succesfully generated. You do not need to run this command again.');
  36. return;
  37. }
  38. $pkiConfig = [
  39. 'digest_alg' => 'sha512',
  40. 'private_key_bits' => 2048,
  41. 'private_key_type' => OPENSSL_KEYTYPE_RSA,
  42. ];
  43. $pki = openssl_pkey_new($pkiConfig);
  44. openssl_pkey_export($pki, $pki_private);
  45. $pki_public = openssl_pkey_get_details($pki);
  46. $pki_public = $pki_public['key'];
  47. $actor = new InstanceActor();
  48. $actor->public_key = $pki_public;
  49. $actor->private_key = $pki_private;
  50. $actor->save();
  51. Cache::rememberForever(InstanceActor::PKI_PUBLIC, function() use($actor) {
  52. return $actor->public_key;
  53. });
  54. Cache::rememberForever(InstanceActor::PKI_PRIVATE, function() use($actor) {
  55. return $actor->private_key;
  56. });
  57. $this->info('Instance actor succesfully generated. You do not need to run this command again.');
  58. return 0;
  59. }
  60. }