菜谱项目

PayloadFactory.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /*
  3. * This file is part of jwt-auth.
  4. *
  5. * (c) Sean Tymon <tymon148@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Tymon\JWTAuth;
  11. use Illuminate\Support\Str;
  12. use Illuminate\Http\Request;
  13. use Tymon\JWTAuth\Claims\Factory;
  14. use Tymon\JWTAuth\Validators\PayloadValidator;
  15. class PayloadFactory
  16. {
  17. /**
  18. * @var \Tymon\JWTAuth\Claims\Factory
  19. */
  20. protected $claimFactory;
  21. /**
  22. * @var \Illuminate\Http\Request
  23. */
  24. protected $request;
  25. /**
  26. * @var \Tymon\JWTAuth\Validators\PayloadValidator
  27. */
  28. protected $validator;
  29. /**
  30. * @var int
  31. */
  32. protected $ttl = 60;
  33. /**
  34. * @var bool
  35. */
  36. protected $refreshFlow = false;
  37. /**
  38. * @var array
  39. */
  40. protected $defaultClaims = ['iss', 'iat', 'exp', 'nbf', 'jti'];
  41. /**
  42. * @var array
  43. */
  44. protected $claims = [];
  45. /**
  46. * @param \Tymon\JWTAuth\Claims\Factory $claimFactory
  47. * @param \Illuminate\Http\Request $request
  48. * @param \Tymon\JWTAuth\Validators\PayloadValidator $validator
  49. */
  50. public function __construct(Factory $claimFactory, Request $request, PayloadValidator $validator)
  51. {
  52. $this->claimFactory = $claimFactory;
  53. $this->request = $request;
  54. $this->validator = $validator;
  55. }
  56. /**
  57. * Create the Payload instance.
  58. *
  59. * @param array $customClaims
  60. * @return \Tymon\JWTAuth\Payload
  61. */
  62. public function make(array $customClaims = [])
  63. {
  64. $claims = $this->buildClaims($customClaims)->resolveClaims();
  65. return new Payload($claims, $this->validator, $this->refreshFlow);
  66. }
  67. /**
  68. * Add an array of claims to the Payload.
  69. *
  70. * @param array $claims
  71. * @return $this
  72. */
  73. public function addClaims(array $claims)
  74. {
  75. foreach ($claims as $name => $value) {
  76. $this->addClaim($name, $value);
  77. }
  78. return $this;
  79. }
  80. /**
  81. * Add a claim to the Payload.
  82. *
  83. * @param string $name
  84. * @param mixed $value
  85. * @return $this
  86. */
  87. public function addClaim($name, $value)
  88. {
  89. $this->claims[$name] = $value;
  90. return $this;
  91. }
  92. /**
  93. * Build the default claims.
  94. *
  95. * @param array $customClaims
  96. * @return $this
  97. */
  98. protected function buildClaims(array $customClaims)
  99. {
  100. // add any custom claims first
  101. $this->addClaims($customClaims);
  102. foreach ($this->defaultClaims as $claim) {
  103. if (! array_key_exists($claim, $customClaims)) {
  104. $this->addClaim($claim, $this->$claim());
  105. }
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Build out the Claim DTO's.
  111. *
  112. * @return array
  113. */
  114. public function resolveClaims()
  115. {
  116. $resolved = [];
  117. foreach ($this->claims as $name => $value) {
  118. $resolved[] = $this->claimFactory->get($name, $value);
  119. }
  120. return $resolved;
  121. }
  122. /**
  123. * Set the Issuer (iss) claim.
  124. *
  125. * @return string
  126. */
  127. public function iss()
  128. {
  129. return $this->request->url();
  130. }
  131. /**
  132. * Set the Issued At (iat) claim.
  133. *
  134. * @return int
  135. */
  136. public function iat()
  137. {
  138. return Utils::now()->timestamp;
  139. }
  140. /**
  141. * Set the Expiration (exp) claim.
  142. *
  143. * @return int
  144. */
  145. public function exp()
  146. {
  147. return Utils::now()->addMinutes($this->ttl)->timestamp;
  148. }
  149. /**
  150. * Set the Not Before (nbf) claim.
  151. *
  152. * @return int
  153. */
  154. public function nbf()
  155. {
  156. return Utils::now()->timestamp;
  157. }
  158. /**
  159. * Set a unique id (jti) for the token.
  160. *
  161. * @return string
  162. */
  163. protected function jti()
  164. {
  165. return Str::random();
  166. }
  167. /**
  168. * Set the token ttl (in minutes).
  169. *
  170. * @param int $ttl
  171. * @return $this
  172. */
  173. public function setTTL($ttl)
  174. {
  175. $this->ttl = $ttl;
  176. return $this;
  177. }
  178. /**
  179. * Get the token ttl.
  180. *
  181. * @return int
  182. */
  183. public function getTTL()
  184. {
  185. return $this->ttl;
  186. }
  187. /**
  188. * Set the refresh flow.
  189. *
  190. * @param bool $refreshFlow
  191. * @return $this
  192. */
  193. public function setRefreshFlow($refreshFlow = true)
  194. {
  195. $this->refreshFlow = $refreshFlow;
  196. return $this;
  197. }
  198. /**
  199. * Magically add a claim.
  200. *
  201. * @param string $method
  202. * @param array $parameters
  203. * @return PayloadFactory
  204. * @throws \BadMethodCallException
  205. */
  206. public function __call($method, $parameters)
  207. {
  208. $this->addClaim($method, $parameters[0]);
  209. return $this;
  210. }
  211. }