3
3
namespace Codecasts \Auth \JWT \Console ;
4
4
5
5
use Illuminate \Console \Command ;
6
+ use Illuminate \Console \ConfirmableTrait ;
6
7
use Symfony \Component \Console \Helper \FormatterHelper ;
7
8
8
9
/**
12
13
*/
13
14
class KeyGenerateCommand extends Command
14
15
{
16
+ use ConfirmableTrait;
17
+
15
18
/**
16
19
* The name and signature of the console command.
17
20
*
18
21
* @var string
19
22
*/
20
- protected $ signature = 'jwt:generate ' ;
23
+ protected $ signature = 'jwt:generate
24
+ {--show : Display the key instead of modifying files}
25
+ {--force : Force the operation to run when in production} ' ;
21
26
22
27
/**
23
28
* Make it 5.4 compatible.
@@ -29,26 +34,74 @@ public function fire()
29
34
30
35
/**
31
36
* Execute the command that will generate and print a key.
32
- *
33
- * @return void
34
37
*/
35
38
public function handle ()
36
39
{
37
40
// call the action to generate a new key.
38
41
$ key = $ this ->generateRandomKey ();
39
42
40
- // print the success block.
41
- $ this ->printBlock ([
42
- 'JWT Key Generated! ' ,
43
- 'Please Update your .env file manually with the following key: ' ,
44
- ], 'bg=green;fg=black ' , true );
43
+ if ($ this ->option ('show ' )) {
44
+ // print the success block.
45
+ $ this ->printBlock ([
46
+ 'JWT Key Generated! ' ,
47
+ 'Please Update your .env file manually with the following key: ' ,
48
+ ], 'bg=green;fg=black ' , true );
49
+
50
+ // print the key block.
51
+ return $ this ->printBlock ([
52
+ "JWT_SECRET= {$ key }" ,
53
+ ], 'bg=yellow;fg=black ' );
54
+ }
55
+
56
+ // Next, we will replace the application key in the environment file so it is
57
+ // automatically setup for this developer. This key gets generated using a
58
+ // secure random byte generator and is later base64 encoded for storage.
59
+ if (!$ this ->setKeyInEnvironmentFile ($ key )) {
60
+ return ;
61
+ }
62
+
63
+ $ this ->laravel ['config ' ]['jwt.secret ' ] = $ key ;
45
64
46
65
// print the key block.
47
66
$ this ->printBlock ([
48
67
"JWT_SECRET= {$ key }" ,
49
68
], 'bg=yellow;fg=black ' );
50
69
}
51
70
71
+ /**
72
+ * Set the application key in the environment file.
73
+ *
74
+ * @param string $key
75
+ *
76
+ * @return bool
77
+ */
78
+ protected function setKeyInEnvironmentFile ($ key )
79
+ {
80
+ $ currentKey = $ this ->laravel ['config ' ]['jwt.secret ' ];
81
+
82
+ if (0 !== strlen ($ currentKey ) && (!$ this ->confirmToProceed ())) {
83
+ return false ;
84
+ }
85
+
86
+ $ this ->writeNewEnvironmentFileWith ($ key );
87
+
88
+ return true ;
89
+ }
90
+
91
+ /**
92
+ * Write a new environment file with the given key.
93
+ *
94
+ * @param string $key
95
+ */
96
+ protected function writeNewEnvironmentFileWith ($ key )
97
+ {
98
+ file_put_contents ($ this ->laravel ->environmentFilePath (), preg_replace (
99
+ $ this ->keyReplacementPattern (),
100
+ 'JWT_SECRET= ' .$ key ,
101
+ file_get_contents ($ this ->laravel ->environmentFilePath ())
102
+ ));
103
+ }
104
+
52
105
/**
53
106
* Generates a random, 32 bytes long, base64 encoded key.
54
107
*
@@ -88,4 +141,16 @@ protected function printBlock(array $lines, $style, $firstBlock = false)
88
141
// empty ending line.
89
142
$ this ->line ('' );
90
143
}
144
+
145
+ /**
146
+ * Get a regex pattern that will match env APP_KEY with any random key.
147
+ *
148
+ * @return string
149
+ */
150
+ protected function keyReplacementPattern ()
151
+ {
152
+ $ escaped = preg_quote ('= ' .$ this ->laravel ['config ' ]['jwt.secret ' ], '/ ' );
153
+
154
+ return "/^JWT_SECRET {$ escaped }/m " ;
155
+ }
91
156
}
0 commit comments