TokenStorage.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2018 RocketTheme, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Login\RememberMe;
  9. use Grav\Common\File\CompiledYamlFile;
  10. use Grav\Common\Filesystem\Folder;
  11. use Grav\Common\Grav;
  12. use Birke\Rememberme\Storage\StorageInterface;
  13. /**
  14. * Token Storage wrapper
  15. *
  16. * Used for storing the credential/token/persistentToken triplets.
  17. */
  18. class TokenStorage implements StorageInterface
  19. {
  20. /** @var string */
  21. protected $path;
  22. /** @var int */
  23. protected $timeout;
  24. /**
  25. * Constructor
  26. *
  27. * @param string $path Path to storage directory
  28. * @throws \InvalidArgumentException
  29. */
  30. public function __construct($path = 'user://data/rememberme', $timeout = 604800)
  31. {
  32. $this->path = Grav::instance()['locator']->findResource($path, true, true);
  33. $this->timeout = $timeout;
  34. }
  35. /**
  36. * Return Tri-state value constant
  37. *
  38. * @param mixed $credential Unique credential (user id, email address, user name)
  39. * @param string $token One-Time Token
  40. * @param string $persistentToken Persistent Token
  41. *
  42. * @return int
  43. */
  44. public function findTriplet($credential, $token, $persistentToken)
  45. {
  46. // Hash the tokens, because they can contain a salt and can be accessed in the file system
  47. $persistentToken = sha1($persistentToken);
  48. $token = sha1($token);
  49. $file = $this->getFile($credential);
  50. $tokens = (array)$file->content();
  51. if (!isset($tokens[$persistentToken]) || $tokens[$persistentToken] < time() + $this->timeout) {
  52. return self::TRIPLET_NOT_FOUND;
  53. }
  54. $stored = key($tokens[$persistentToken]);
  55. if ($stored !== $token) {
  56. return self::TRIPLET_INVALID;
  57. }
  58. return self::TRIPLET_FOUND;
  59. }
  60. /**
  61. * Store the new token for the credential and the persistent token. Create a new storage entry, if the combination
  62. * of credential and persistent token does not exist.
  63. *
  64. * @param mixed $credential
  65. * @param string $token
  66. * @param string $persistentToken
  67. * @param int $expire Timestamp when this triplet will expire (0 = no expiry)
  68. */
  69. public function storeTriplet($credential, $token, $persistentToken, $expire = null)
  70. {
  71. // Hash the tokens, because they can contain a salt and can be accessed in the file system
  72. $persistentToken = sha1($persistentToken);
  73. $token = sha1($token);
  74. $file = $this->getFile($credential);
  75. $tokens = (array)$file->content();
  76. // Update token
  77. $tokens[$persistentToken] = [$token => time()];
  78. $file->save($tokens);
  79. }
  80. /**
  81. * Replace current token after successful authentication
  82. *
  83. * @param mixed $credential
  84. * @param string $token
  85. * @param string $persistentToken
  86. * @param int $expire
  87. */
  88. public function replaceTriplet($credential, $token, $persistentToken, $expire = null)
  89. {
  90. $this->storeTriplet($credential, $token, $persistentToken, $expire);
  91. }
  92. /**
  93. * Remove one triplet of the user from the store
  94. *
  95. * @param mixed $credential
  96. * @param string $persistentToken
  97. */
  98. public function cleanTriplet($credential, $persistentToken)
  99. {
  100. // Hash the tokens, because they can contain a salt and can be accessed in the file system
  101. $persistentToken = sha1($persistentToken);
  102. $file = $this->getFile($credential);
  103. if (!$file->exists()) {
  104. return;
  105. }
  106. $tokens = (array)$file->content();
  107. if (isset($tokens[$persistentToken])) {
  108. // Delete token from storage
  109. unset($tokens[$persistentToken]);
  110. if ($tokens) {
  111. $file->save($tokens);
  112. } else {
  113. $file->delete();
  114. }
  115. }
  116. }
  117. /**
  118. * Remove all triplets of a user, effectively logging him out on all
  119. * machines
  120. *
  121. * @param mixed $credential
  122. */
  123. public function cleanAllTriplets($credential)
  124. {
  125. $file = $this->getFile($credential);
  126. if ($file->exists()) {
  127. $file->delete();
  128. }
  129. }
  130. /**
  131. * Helper method to clear RememberMe cache
  132. */
  133. public function clearCache()
  134. {
  135. if (is_dir($this->path)) {
  136. Folder::delete($this->path, false);
  137. }
  138. }
  139. /**
  140. * @param string $credential
  141. * @return CompiledYamlFile
  142. */
  143. protected function getFile($credential)
  144. {
  145. return CompiledYamlFile::instance($this->path . '/' . sha1($credential) . '.yaml');
  146. }
  147. }