Skip to content

Commit 015b942

Browse files
committed
Merge branch 'release/0.11.0'
2 parents cd5cf11 + d9acea2 commit 015b942

File tree

3 files changed

+80
-23
lines changed

3 files changed

+80
-23
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ matrix:
99
include:
1010
- php: 7.0
1111
- php: 7.1
12+
- php: 7.2
1213
- php: nightly
1314
fast_finish: true
1415

src/Auth/Guard.php

+6-15
Original file line numberDiff line numberDiff line change
@@ -269,24 +269,15 @@ protected function getTokenFromParameter()
269269
*/
270270
protected function detectedToken()
271271
{
272-
// retrieve the token from the Authorization header.
273-
$headerToken = $this->getTokenFromHeader();
272+
// retrieve the token from request.
273+
$detectedToken = $this->getTokenFromHeader() ?? $this->getTokenFromParameter();
274274

275-
// if a token was found...
276-
if ($headerToken) {
277-
// return a new token instance.
278-
$this->token = $this->manager()->parseToken($headerToken);
275+
if ($detectedToken) {
276+
// update the currently used token
277+
$this->token = $this->manager()->parseToken($detectedToken);
279278
}
280279

281-
// try to find a token passed as parameter on the request.
282-
$parameterToken = $this->getTokenFromParameter();
283-
284-
// if found...
285-
if ($parameterToken) {
286-
$this->token = $this->manager()->parseToken($parameterToken);
287-
}
288-
289-
// return null if no token could be found.
280+
// return current token in use
290281
return $this->token;
291282
}
292283

src/Console/KeyGenerateCommand.php

+73-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Codecasts\Auth\JWT\Console;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Console\ConfirmableTrait;
67
use Symfony\Component\Console\Helper\FormatterHelper;
78

89
/**
@@ -12,12 +13,16 @@
1213
*/
1314
class KeyGenerateCommand extends Command
1415
{
16+
use ConfirmableTrait;
17+
1518
/**
1619
* The name and signature of the console command.
1720
*
1821
* @var string
1922
*/
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}';
2126

2227
/**
2328
* Make it 5.4 compatible.
@@ -29,26 +34,74 @@ public function fire()
2934

3035
/**
3136
* Execute the command that will generate and print a key.
32-
*
33-
* @return void
3437
*/
3538
public function handle()
3639
{
3740
// call the action to generate a new key.
3841
$key = $this->generateRandomKey();
3942

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;
4564

4665
// print the key block.
4766
$this->printBlock([
4867
"JWT_SECRET={$key}",
4968
], 'bg=yellow;fg=black');
5069
}
5170

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+
52105
/**
53106
* Generates a random, 32 bytes long, base64 encoded key.
54107
*
@@ -88,4 +141,16 @@ protected function printBlock(array $lines, $style, $firstBlock = false)
88141
// empty ending line.
89142
$this->line('');
90143
}
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+
}
91156
}

0 commit comments

Comments
 (0)