47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
final class EditorialSetup extends Command
|
|
{
|
|
protected $signature = 'editorial:setup';
|
|
|
|
protected $description = 'Create a private credential for the local FNA_V3 editorial API';
|
|
|
|
public function handle(): int
|
|
{
|
|
$c = config('database.connections.mysql');
|
|
if (! app()->environment('local') || $c['database'] !== 'fna_v3' || ! in_array($c['host'], ['localhost', '127.0.0.1'])) {
|
|
$this->error('Local FNA_V3 only. Production credentials require separate provisioning.');
|
|
|
|
return 1;
|
|
}
|
|
$path = config('editorial.credential_file');
|
|
if (is_file($path)) {
|
|
$this->info('Credential already exists; not replaced.');
|
|
|
|
return 0;
|
|
}
|
|
if (! is_dir(dirname($path))) {
|
|
mkdir(dirname($path), 0700, true);
|
|
}
|
|
$data = ['project' => 'fna-v3', 'environment' => 'local', 'base_url' => config('editorial.base_url'), 'schema' => 'gemdocs.credential.v1', 'fields' => ['api_key' => 'fna_ed_'.bin2hex(random_bytes(32))]];
|
|
$old = umask(0077);
|
|
$file = fopen($path, 'x');
|
|
umask($old);
|
|
if (! $file) {
|
|
$this->error('Credential could not be created.');
|
|
|
|
return 1;
|
|
}
|
|
fwrite($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
|
fclose($file);
|
|
chmod($path, 0600);
|
|
$this->info('Local API credential created in private storage. Secret not displayed.');
|
|
|
|
return 0;
|
|
}
|
|
}
|