Manager.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /*
  3. * This file is part of jwt-auth.
  4. *
  5. * (c) 2014-2021 Sean Tymon <tymon148@gmail.com>
  6. * (c) 2021 PHP Open Source Saver
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace PHPOpenSourceSaver\JWTAuth;
  12. use PHPOpenSourceSaver\JWTAuth\Contracts\Providers\JWT as JWTContract;
  13. use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException;
  14. use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenBlacklistedException;
  15. use PHPOpenSourceSaver\JWTAuth\Support\CustomClaims;
  16. use PHPOpenSourceSaver\JWTAuth\Support\RefreshFlow;
  17. class Manager
  18. {
  19. use CustomClaims;
  20. use RefreshFlow;
  21. /**
  22. * The provider.
  23. *
  24. * @var JWTContract
  25. */
  26. protected $provider;
  27. /**
  28. * The blacklist.
  29. *
  30. * @var Blacklist
  31. */
  32. protected $blacklist;
  33. /**
  34. * the payload factory.
  35. *
  36. * @var Factory
  37. */
  38. protected $payloadFactory;
  39. /**
  40. * The blacklist flag.
  41. *
  42. * @var bool
  43. */
  44. protected $blacklistEnabled = true;
  45. /**
  46. * the persistent claims.
  47. *
  48. * @var array
  49. */
  50. protected $persistentClaims = [];
  51. /**
  52. * @var bool
  53. */
  54. protected $showBlackListException = true;
  55. /**
  56. * Constructor.
  57. *
  58. * @return void
  59. */
  60. public function __construct(JWTContract $provider, Blacklist $blacklist, Factory $payloadFactory)
  61. {
  62. $this->provider = $provider;
  63. $this->blacklist = $blacklist;
  64. $this->payloadFactory = $payloadFactory;
  65. }
  66. /**
  67. * Encode a Payload and return the Token.
  68. *
  69. * @return Token
  70. */
  71. public function encode(Payload $payload)
  72. {
  73. $token = $this->provider->encode($payload->get());
  74. return new Token($token);
  75. }
  76. /**
  77. * Decode a Token and return the Payload.
  78. *
  79. * @param bool $checkBlacklist
  80. *
  81. * @return Payload
  82. *
  83. * @throws \PHPOpenSourceSaver\JWTAuth\Exceptions\TokenBlacklistedException
  84. */
  85. public function decode(Token $token, $checkBlacklist = true)
  86. {
  87. $payloadArray = $this->provider->decode($token->get());
  88. $payload = $this->payloadFactory
  89. ->setRefreshFlow($this->refreshFlow)
  90. ->customClaims($payloadArray)
  91. ->make();
  92. if (
  93. $checkBlacklist &&
  94. $this->blacklistEnabled &&
  95. $this->getBlackListExceptionEnabled() &&
  96. $this->blacklist->has($payload)
  97. ) {
  98. throw new TokenBlacklistedException('The token has been blacklisted');
  99. }
  100. return $payload;
  101. }
  102. /**
  103. * Refresh a Token and return a new Token.
  104. *
  105. * @param bool $forceForever
  106. * @param bool $resetClaims
  107. *
  108. * @return Token
  109. */
  110. public function refresh(Token $token, $forceForever = false, $resetClaims = false)
  111. {
  112. $this->setRefreshFlow();
  113. $claims = $this->buildRefreshClaims($this->decode($token));
  114. if ($this->blacklistEnabled) {
  115. // Invalidate old token
  116. $this->invalidate($token, $forceForever);
  117. }
  118. // Return the new token
  119. return $this->encode(
  120. $this->payloadFactory->customClaims($claims)->make($resetClaims)
  121. );
  122. }
  123. /**
  124. * Invalidate a Token by adding it to the blacklist.
  125. *
  126. * @param bool $forceForever
  127. *
  128. * @return bool
  129. *
  130. * @throws JWTException
  131. */
  132. public function invalidate(Token $token, $forceForever = false)
  133. {
  134. if (!$this->blacklistEnabled) {
  135. throw new JWTException('You must have the blacklist enabled to invalidate a token.');
  136. }
  137. return call_user_func(
  138. [$this->blacklist, $forceForever ? 'addForever' : 'add'],
  139. $this->decode($token, false)
  140. );
  141. }
  142. /**
  143. * Build the claims to go into the refreshed token.
  144. *
  145. * @return array
  146. */
  147. protected function buildRefreshClaims(Payload $payload)
  148. {
  149. // Get the claims to be persisted from the payload
  150. $persistentClaims = collect($payload->toArray())
  151. ->only($this->persistentClaims)
  152. ->toArray();
  153. // persist the relevant claims
  154. return array_merge(
  155. $this->customClaims,
  156. $persistentClaims,
  157. [
  158. 'sub' => $payload['sub'],
  159. 'iat' => $payload['iat'],
  160. ]
  161. );
  162. }
  163. /**
  164. * Get the Payload Factory instance.
  165. *
  166. * @return Factory
  167. */
  168. public function getPayloadFactory()
  169. {
  170. return $this->payloadFactory;
  171. }
  172. /**
  173. * Get the JWTProvider instance.
  174. *
  175. * @return JWTContract
  176. */
  177. public function getJWTProvider()
  178. {
  179. return $this->provider;
  180. }
  181. /**
  182. * Get the Blacklist instance.
  183. *
  184. * @return Blacklist
  185. */
  186. public function getBlacklist()
  187. {
  188. return $this->blacklist;
  189. }
  190. /**
  191. * Set whether the blacklist is enabled.
  192. *
  193. * @param bool $enabled
  194. *
  195. * @return $this
  196. */
  197. public function setBlacklistEnabled($enabled)
  198. {
  199. $this->blacklistEnabled = $enabled;
  200. return $this;
  201. }
  202. /**
  203. * Configuration to set up if show the TokenBlacklistedException
  204. * can be throwable or not.
  205. *
  206. * @param bool $showBlackListException
  207. *
  208. * @removed this
  209. */
  210. public function setBlackListExceptionEnabled($showBlackListException = true)
  211. {
  212. $this->showBlackListException = $showBlackListException;
  213. return $this;
  214. }
  215. /**
  216. * Get if the blacklist instance is enabled.
  217. *
  218. * @return bool
  219. */
  220. public function getBlackListExceptionEnabled()
  221. {
  222. return $this->showBlackListException;
  223. }
  224. /**
  225. * Set the claims to be persisted when refreshing a token.
  226. *
  227. * @return $this
  228. */
  229. public function setPersistentClaims(array $claims)
  230. {
  231. $this->persistentClaims = $claims;
  232. return $this;
  233. }
  234. }