1+ <?php
2+
3+ namespace Fd \HslBundle \Command ;
4+
5+ use Symfony \Component \Console \Command \Command ;
6+ use Symfony \Component \Console \Input \InputInterface ;
7+ use Symfony \Component \Console \Output \OutputInterface ;
8+ use Symfony \Component \Process \Exception \ProcessFailedException ;
9+ use Symfony \Component \Process \Process ;
10+
11+ /**
12+ * Generate SSH keys.
13+ */
14+ class LexikJWTKeyGeneratorCommand extends Command
15+ {
16+ protected static $ defaultName = 'lexik:generate-keys ' ;
17+
18+ protected function configure ()
19+ {
20+ $ this ->setDescription ('Generate SSH keys for LexikJWTAuthenticatorBundle. ' );
21+ }
22+
23+ protected function execute (InputInterface $ input , OutputInterface $ output )
24+ {
25+ $ output ->writeln ([
26+ '<info> ' ,
27+ '========================================================= ' ,
28+ '| SSH keys generator for LexikJWTAuthenticatorBundle | ' ,
29+ '| | ' ,
30+ "| Generated keys will be save to 'config/jwt' folder. | " ,
31+ '========================================================= ' ,
32+ '</info> ' ,
33+ '' ,
34+ ]);
35+
36+ $ output ->writeln ('Creating folder \'config/jwt \'... ' );
37+ $ output ->writeln ($ this ->makeDirectory ());
38+ $ output ->writeln ([
39+ 'Done. ' ,
40+ '' ,
41+ ]);
42+
43+ $ output ->writeln ('Generate Secret key... ' );
44+ $ output ->writeln ($ this ->generatePrivateKey ());
45+ $ output ->writeln ([
46+ 'Done. ' ,
47+ '' ,
48+ ]);
49+
50+ $ output ->writeln ('Generate Public key... ' );
51+ $ output ->writeln ($ this ->generatePublicKey ());
52+ $ output ->writeln ('Done. ' );
53+ $ output ->writeln ([
54+ '' ,
55+ '<info>Remember to double check your \'config/packages/lexik_jwt_authentication.yaml \' and \'.env \' for configuration.</info> ' ,
56+ ''
57+ ]);
58+
59+ return Command::SUCCESS ;
60+ }
61+
62+ public function makeDirectory ()
63+ {
64+ $ process = new Process (['mkdir ' , '-p ' , 'config/jwt ' ]);
65+ $ process ->run ();
66+
67+ // executes after the command finishes
68+ if (!$ process ->isSuccessful ()) {
69+ throw new ProcessFailedException ($ process );
70+ }
71+
72+ return $ process ->getOutput ();
73+ }
74+
75+ public function generatePrivateKey ()
76+ {
77+ $ process = new Process (['openssl ' , 'genpkey ' , '-out ' , 'config/jwt/private.pem ' , '-aes256 ' , '-algorithm ' , 'rsa ' , '-pkeyopt ' , 'rsa_keygen_bits:4096 ' ]);
78+ $ process ->run ();
79+
80+ // executes after the command finishes
81+ if (!$ process ->isSuccessful ()) {
82+ throw new ProcessFailedException ($ process );
83+ }
84+
85+ return $ process ->getOutput ();
86+ }
87+
88+ public function generatePublicKey ()
89+ {
90+ $ process = new Process (['openssl ' , 'pkey ' , '-in ' , 'config/jwt/private.pem ' , '-out ' , 'config/jwt/public.pem ' , '-pubout ' ]);
91+ $ process ->run ();
92+
93+ // executes after the command finishes
94+ if (!$ process ->isSuccessful ()) {
95+ throw new ProcessFailedException ($ process );
96+ }
97+
98+ return $ process ->getOutput ();
99+ }
100+ }
0 commit comments