AuthenticationException.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Security\Core\Exception;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. /**
  13. * AuthenticationException is the base class for all authentication exceptions.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. * @author Alexander <iam.asm89@gmail.com>
  17. */
  18. class AuthenticationException extends RuntimeException
  19. {
  20. private $token;
  21. /**
  22. * Get the token.
  23. *
  24. * @return TokenInterface|null
  25. */
  26. public function getToken()
  27. {
  28. return $this->token;
  29. }
  30. public function setToken(TokenInterface $token)
  31. {
  32. $this->token = $token;
  33. }
  34. /**
  35. * Returns all the necessary state of the object for serialization purposes.
  36. *
  37. * There is no need to serialize any entry, they should be returned as-is.
  38. * If you extend this method, keep in mind you MUST guarantee parent data is present in the state.
  39. * Here is an example of how to extend this method:
  40. * <code>
  41. * public function __serialize(): array
  42. * {
  43. * return [$this->childAttribute, parent::__serialize()];
  44. * }
  45. * </code>
  46. *
  47. * @see __unserialize()
  48. */
  49. public function __serialize(): array
  50. {
  51. return [$this->token, $this->code, $this->message, $this->file, $this->line];
  52. }
  53. /**
  54. * Restores the object state from an array given by __serialize().
  55. *
  56. * There is no need to unserialize any entry in $data, they are already ready-to-use.
  57. * If you extend this method, keep in mind you MUST pass the parent data to its respective class.
  58. * Here is an example of how to extend this method:
  59. * <code>
  60. * public function __unserialize(array $data): void
  61. * {
  62. * [$this->childAttribute, $parentData] = $data;
  63. * parent::__unserialize($parentData);
  64. * }
  65. * </code>
  66. *
  67. * @see __serialize()
  68. */
  69. public function __unserialize(array $data): void
  70. {
  71. [$this->token, $this->code, $this->message, $this->file, $this->line] = $data;
  72. }
  73. /**
  74. * Message key to be used by the translation component.
  75. *
  76. * @return string
  77. */
  78. public function getMessageKey()
  79. {
  80. return 'An authentication exception occurred.';
  81. }
  82. /**
  83. * Message data to be used by the translation component.
  84. *
  85. * @return array
  86. */
  87. public function getMessageData()
  88. {
  89. return [];
  90. }
  91. /**
  92. * @internal
  93. */
  94. public function __sleep(): array
  95. {
  96. $this->serialized = $this->__serialize();
  97. return ['serialized'];
  98. }
  99. /**
  100. * @internal
  101. */
  102. public function __wakeup()
  103. {
  104. $this->__unserialize($this->serialized);
  105. unset($this->serialized);
  106. }
  107. }