Skip to content

Commit

Permalink
Merge branch 'develop' into configurable_toke_key_name
Browse files Browse the repository at this point in the history
* develop:
  remove idea
  Adding macroable capabilities to the Guard (tymondesigns#1604)
  Reset claims collection when creating a payload. (tymondesigns#1589)
  make lock_subject config available for JWTAuth (tymondesigns#1521)
  Apply fixes from StyleCI (tymondesigns#1605)
  Fixed command to generate a key not working laravel version less than 5.4.17 (tymondesigns#1470)
  Update config.php (tymondesigns#1497)
  • Loading branch information
ElRochito committed Jun 26, 2018
2 parents c1b8b54 + 6e41925 commit 5ee388a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
| Some people may want this behaviour for e.g. a mobile app.
| This is not particularly recommended, so make sure you have appropriate
| systems in place to revoke the token if necessary.
| Notice: If you set this to null you should remove 'exp' element from 'required_claims' list.
|
*/

Expand Down
5 changes: 5 additions & 0 deletions src/Console/JWTGenerateSecretCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ protected function envPath()
return $this->laravel->environmentFilePath();
}

// check if laravel version Less than 5.4.17
if (version_compare($this->laravel->version(), '5.4.17', '<')) {
return $this->laravel->basePath().DIRECTORY_SEPARATOR.'.env';
}

return $this->laravel->basePath('.env');
}
}
4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public function __construct(ClaimFactory $claimFactory, PayloadValidator $valida
*/
public function make($resetClaims = false)
{
$payload = $this->withClaims($this->buildClaimsCollection());

if ($resetClaims) {
$this->emptyClaims();
}

$payload = $this->withClaims($this->buildClaimsCollection());

return $payload;
}

Expand Down
9 changes: 8 additions & 1 deletion src/JWTGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Guard;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Support\Traits\Macroable;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Contracts\Auth\UserProvider;
use Tymon\JWTAuth\Exceptions\UserNotDefinedException;

class JWTGuard implements Guard
{
use GuardHelpers;
use GuardHelpers, Macroable {
__call as macroCall;
}

/**
* The user we last attempted to retrieve.
Expand Down Expand Up @@ -435,6 +438,10 @@ public function __call($method, $parameters)
return call_user_func_array([$this->jwt, $method], $parameters);
}

if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}

throw new BadMethodCallException("Method [$method] does not exist.");
}
}
2 changes: 1 addition & 1 deletion src/Validators/PayloadValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function check($value)
*/
protected function validateStructure(Collection $claims)
{
if (! $claims->hasAllClaims($this->requiredClaims)) {
if ($this->requiredClaims && ! $claims->hasAllClaims($this->requiredClaims)) {
throw new TokenInvalidException('JWT payload does not contain the required claims');
}
}
Expand Down
13 changes: 7 additions & 6 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,18 @@ public function it_should_exclude_the_exp_claim_when_setting_ttl_to_null()
/** @test */
public function it_should_exclude_claims_from_previous_payloads()
{
$validator = new PayloadValidator();
$factory = new Factory($this->claimFactory, $validator);

$fooClaim = new Custom('foo', 'bar');
$barClaim = new Custom('baz', 'qux');

$this->claimFactory->shouldReceive('getTTL')->andReturn(60);
$this->claimFactory->shouldReceive('get')->with('foo', 'bar')->twice()->andReturn($fooClaim);
$this->claimFactory->shouldReceive('get')->with('baz', 'qux')->twice()->andReturn($barClaim);
$this->validator->shouldReceive('setRefreshFlow->check')->once()->andReturn(new Collection([$fooClaim, $barClaim]));
$this->claimFactory->shouldReceive('get')->with('baz', 'qux')->once()->andReturn($barClaim);

$payload = $this->factory->setDefaultClaims([])
$validator->setRequiredClaims([]);
$payload = $factory->setDefaultClaims([])
->customClaims([
'foo' => 'bar',
'baz' => 'qux',
Expand All @@ -201,9 +204,7 @@ public function it_should_exclude_claims_from_previous_payloads()
$this->assertSame($payload->get('foo'), 'bar');
$this->assertSame($payload->get('baz'), 'qux');

$this->validator->shouldReceive('setRefreshFlow->check')->once()->andReturn(new Collection([$fooClaim]));

$payload = $this->factory->setDefaultClaims([])->customClaims(['foo' => 'bar'])->make(true);
$payload = $factory->setDefaultClaims([])->customClaims(['foo' => 'bar'])->make(true);

$this->assertSame($payload->get('foo'), 'bar');
$this->assertFalse($payload->hasKey('baz'));
Expand Down
13 changes: 13 additions & 0 deletions tests/JWTGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,17 @@ public function it_should_get_the_payload()
$this->jwt->shouldReceive('getPayload')->once()->andReturn(Mockery::mock(Payload::class));
$this->assertInstanceOf(Payload::class, $this->guard->payload());
}

/**
* @test
* @group laravel-5.2
*/
public function it_should_be_macroable()
{
$this->guard->macro('foo', function () {
return 'bar';
});

$this->assertEquals('bar', $this->guard->foo());
}
}

0 comments on commit 5ee388a

Please sign in to comment.