1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Oauth\Console\Commands; |
6
|
|
|
|
7
|
|
|
use phpseclib\Crypt\RSA; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
|
11
|
|
|
class KeysCommand extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name and signature of the console command. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'rinvex:oauth:keys |
19
|
|
|
{--force : Overwrite keys they already exist} |
20
|
|
|
{--length=4096 : The length of the private key}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Create the encryption keys for API authentication'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute the console command. |
31
|
|
|
* |
32
|
|
|
* @param \phpseclib\Crypt\RSA $rsa |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function handle(RSA $rsa) |
37
|
|
|
{ |
38
|
|
|
$this->alert($this->description); |
39
|
|
|
|
40
|
|
|
[$publicKey, $privateKey] = [ |
|
|
|
|
41
|
|
|
self::keyPath('oauth-public.key'), |
42
|
|
|
self::keyPath('oauth-private.key'), |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
if ((file_exists($publicKey) || file_exists($privateKey)) && ! $this->option('force')) { |
46
|
|
|
$this->error('Encryption keys already exist. Use the --force option to overwrite them.'); |
47
|
|
|
} else { |
48
|
|
|
$keys = $rsa->createKey($this->input ? (int) $this->option('length') : 4096); |
49
|
|
|
|
50
|
|
|
file_put_contents($publicKey, Arr::get($keys, 'publickey')); |
51
|
|
|
file_put_contents($privateKey, Arr::get($keys, 'privatekey')); |
52
|
|
|
|
53
|
|
|
$this->info('Encryption keys generated successfully.'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The location of the encryption keys. |
59
|
|
|
* |
60
|
|
|
* @param string $file |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public static function keyPath($file) |
65
|
|
|
{ |
66
|
|
|
$file = ltrim($file, '/\\'); |
67
|
|
|
|
68
|
|
|
return config('rinvex.oauth.key_path') |
69
|
|
|
? rtrim(config('rinvex.oauth.key_path'), '/\\').DIRECTORY_SEPARATOR.$file |
70
|
|
|
: storage_path($file); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.