From 58903f9039d81f29ef25a22e9d5797f6b022a203 Mon Sep 17 00:00:00 2001 From: Sean Date: Wed, 2 Mar 2016 20:27:02 +0000 Subject: [PATCH] adding ability to pass a Collection of claims to the factory instance --- src/Factory.php | 42 +++++++++++++++++++++++++++--------------- tests/FactoryTest.php | 21 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index ae512d675..20f79c228 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -77,7 +77,7 @@ public function make() { $claims = $this->buildClaims()->resolveClaims(); - return new Payload($claims, $this->validator, $this->refreshFlow); + return $this->withClaims($claims); } /** @@ -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. * @@ -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). * @@ -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. * diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 11d37b520..a5f13454c 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -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; @@ -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() {