Skip to content

Commit

Permalink
adding ability to pass a Collection of claims to the factory instance
Browse files Browse the repository at this point in the history
  • Loading branch information
tymondesigns committed Mar 2, 2016
1 parent 0918e31 commit 58903f9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function make()
{
$claims = $this->buildClaims()->resolveClaims();

return new Payload($claims, $this->validator, $this->refreshFlow);
return $this->withClaims($claims);
}

/**
Expand Down Expand Up @@ -146,6 +146,18 @@ protected function resolveClaims()
});
}

/**
* Get a Payload instance with a claims collection.
*
* @param Collection $claims
*
* @return \Tymon\JWTAuth\Payload
*/
public function withClaims(Collection $claims)
{
return new Payload($claims, $this->validator, $this->refreshFlow);
}

/**
* Get the Issuer (iss) claim.
*
Expand Down Expand Up @@ -196,6 +208,20 @@ protected function jti()
return md5(sprintf('%s.%s', $this->claims->toJson(), Str::quickRandom()));
}

/**
* Set the request instance.
*
* @param \Illuminate\Http\Request $request
*
* @return $this
*/
public function setRequest(Request $request)
{
$this->request = $request;

return $this;
}

/**
* Set the token ttl (in minutes).
*
Expand Down Expand Up @@ -254,20 +280,6 @@ public function validator()
return $this->validator;
}

/**
* Set the request instance.
*
* @param \Illuminate\Http\Request $request
*
* @return $this
*/
public function setRequest(Request $request)
{
$this->request = $request;

return $this;
}

/**
* Magically add a claim.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Tymon\JWTAuth\Claims\Issuer;
use Tymon\JWTAuth\Claims\Subject;
use Tymon\JWTAuth\Claims\IssuedAt;
use Illuminate\Support\Collection;
use Tymon\JWTAuth\Claims\NotBefore;
use Tymon\JWTAuth\Claims\Expiration;
use Tymon\JWTAuth\Validators\PayloadValidator;
Expand Down Expand Up @@ -159,6 +160,26 @@ public function it_should_set_the_default_claims()
$this->assertSame($this->factory->getDefaultClaims(), ['sub', 'iat']);
}

/** @test */
public function it_should_get_payload_with_a_predefined_collection_of_claims()
{
$this->validator->shouldReceive('setRefreshFlow->check');

$claims = [
'sub' => new Subject(1),
'iss' => new Issuer('http://example.com'),
'exp' => new Expiration($this->testNowTimestamp + 3600),
'nbf' => new NotBefore($this->testNowTimestamp),
'iat' => new IssuedAt($this->testNowTimestamp),
'jti' => new JwtId('foo'),
];

$payload = $this->factory->withClaims(Collection::make($claims));

$this->assertInstanceOf(Payload::class, $payload);
$this->assertSame($payload->get('sub'), 1);
}

/** @test */
public function it_should_get_the_validator()
{
Expand Down

0 comments on commit 58903f9

Please sign in to comment.