123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- /*
- * This file is part of jwt-auth.
- *
- * (c) Sean Tymon <tymon148@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Tymon\JWTAuth;
- use Illuminate\Support\Str;
- use Illuminate\Http\Request;
- use Tymon\JWTAuth\Claims\Factory;
- use Tymon\JWTAuth\Validators\PayloadValidator;
- class PayloadFactory
- {
- /**
- * @var \Tymon\JWTAuth\Claims\Factory
- */
- protected $claimFactory;
- /**
- * @var \Illuminate\Http\Request
- */
- protected $request;
- /**
- * @var \Tymon\JWTAuth\Validators\PayloadValidator
- */
- protected $validator;
- /**
- * @var int
- */
- protected $ttl = 60;
- /**
- * @var bool
- */
- protected $refreshFlow = false;
- /**
- * @var array
- */
- protected $defaultClaims = ['iss', 'iat', 'exp', 'nbf', 'jti'];
- /**
- * @var array
- */
- protected $claims = [];
- /**
- * @param \Tymon\JWTAuth\Claims\Factory $claimFactory
- * @param \Illuminate\Http\Request $request
- * @param \Tymon\JWTAuth\Validators\PayloadValidator $validator
- */
- public function __construct(Factory $claimFactory, Request $request, PayloadValidator $validator)
- {
- $this->claimFactory = $claimFactory;
- $this->request = $request;
- $this->validator = $validator;
- }
- /**
- * Create the Payload instance.
- *
- * @param array $customClaims
- * @return \Tymon\JWTAuth\Payload
- */
- public function make(array $customClaims = [])
- {
- $claims = $this->buildClaims($customClaims)->resolveClaims();
- return new Payload($claims, $this->validator, $this->refreshFlow);
- }
- /**
- * Add an array of claims to the Payload.
- *
- * @param array $claims
- * @return $this
- */
- public function addClaims(array $claims)
- {
- foreach ($claims as $name => $value) {
- $this->addClaim($name, $value);
- }
- return $this;
- }
- /**
- * Add a claim to the Payload.
- *
- * @param string $name
- * @param mixed $value
- * @return $this
- */
- public function addClaim($name, $value)
- {
- $this->claims[$name] = $value;
- return $this;
- }
- /**
- * Build the default claims.
- *
- * @param array $customClaims
- * @return $this
- */
- protected function buildClaims(array $customClaims)
- {
- // add any custom claims first
- $this->addClaims($customClaims);
- foreach ($this->defaultClaims as $claim) {
- if (! array_key_exists($claim, $customClaims)) {
- $this->addClaim($claim, $this->$claim());
- }
- }
- return $this;
- }
- /**
- * Build out the Claim DTO's.
- *
- * @return array
- */
- public function resolveClaims()
- {
- $resolved = [];
- foreach ($this->claims as $name => $value) {
- $resolved[] = $this->claimFactory->get($name, $value);
- }
- return $resolved;
- }
- /**
- * Set the Issuer (iss) claim.
- *
- * @return string
- */
- public function iss()
- {
- return $this->request->url();
- }
- /**
- * Set the Issued At (iat) claim.
- *
- * @return int
- */
- public function iat()
- {
- return Utils::now()->timestamp;
- }
- /**
- * Set the Expiration (exp) claim.
- *
- * @return int
- */
- public function exp()
- {
- return Utils::now()->addMinutes($this->ttl)->timestamp;
- }
- /**
- * Set the Not Before (nbf) claim.
- *
- * @return int
- */
- public function nbf()
- {
- return Utils::now()->timestamp;
- }
- /**
- * Set a unique id (jti) for the token.
- *
- * @return string
- */
- protected function jti()
- {
- return Str::random();
- }
- /**
- * Set the token ttl (in minutes).
- *
- * @param int $ttl
- * @return $this
- */
- public function setTTL($ttl)
- {
- $this->ttl = $ttl;
- return $this;
- }
- /**
- * Get the token ttl.
- *
- * @return int
- */
- public function getTTL()
- {
- return $this->ttl;
- }
- /**
- * Set the refresh flow.
- *
- * @param bool $refreshFlow
- * @return $this
- */
- public function setRefreshFlow($refreshFlow = true)
- {
- $this->refreshFlow = $refreshFlow;
- return $this;
- }
- /**
- * Magically add a claim.
- *
- * @param string $method
- * @param array $parameters
- * @return PayloadFactory
- * @throws \BadMethodCallException
- */
- public function __call($method, $parameters)
- {
- $this->addClaim($method, $parameters[0]);
- return $this;
- }
- }
|