EncryptedStore.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Illuminate\Session;
  3. use SessionHandlerInterface;
  4. use Illuminate\Contracts\Encryption\DecryptException;
  5. use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
  6. class EncryptedStore extends Store
  7. {
  8. /**
  9. * The encrypter instance.
  10. *
  11. * @var \Illuminate\Contracts\Encryption\Encrypter
  12. */
  13. protected $encrypter;
  14. /**
  15. * Create a new session instance.
  16. *
  17. * @param string $name
  18. * @param \SessionHandlerInterface $handler
  19. * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
  20. * @param string|null $id
  21. * @return void
  22. */
  23. public function __construct($name, SessionHandlerInterface $handler, EncrypterContract $encrypter, $id = null)
  24. {
  25. $this->encrypter = $encrypter;
  26. parent::__construct($name, $handler, $id);
  27. }
  28. /**
  29. * Prepare the raw string data from the session for unserialization.
  30. *
  31. * @param string $data
  32. * @return string
  33. */
  34. protected function prepareForUnserialize($data)
  35. {
  36. try {
  37. return $this->encrypter->decrypt($data);
  38. } catch (DecryptException $e) {
  39. return serialize([]);
  40. }
  41. }
  42. /**
  43. * Prepare the serialized session data for storage.
  44. *
  45. * @param string $data
  46. * @return string
  47. */
  48. protected function prepareForStorage($data)
  49. {
  50. return $this->encrypter->encrypt($data);
  51. }
  52. /**
  53. * Get the encrypter instance.
  54. *
  55. * @return \Illuminate\Contracts\Encryption\Encrypter
  56. */
  57. public function getEncrypter()
  58. {
  59. return $this->encrypter;
  60. }
  61. }