123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- /**
- * @package Grav\Plugin\Login
- *
- * @copyright Copyright (C) 2014 - 2018 RocketTheme, LLC. All rights reserved.
- * @license MIT License; see LICENSE file for details.
- */
- namespace Grav\Plugin\Login\RememberMe;
- use Grav\Common\File\CompiledYamlFile;
- use Grav\Common\Filesystem\Folder;
- use Grav\Common\Grav;
- use Birke\Rememberme\Storage\StorageInterface;
- /**
- * Token Storage wrapper
- *
- * Used for storing the credential/token/persistentToken triplets.
- */
- class TokenStorage implements StorageInterface
- {
- /** @var string */
- protected $path;
- /** @var int */
- protected $timeout;
- /**
- * Constructor
- *
- * @param string $path Path to storage directory
- * @throws \InvalidArgumentException
- */
- public function __construct($path = 'user://data/rememberme', $timeout = 604800)
- {
- $this->path = Grav::instance()['locator']->findResource($path, true, true);
- $this->timeout = $timeout;
- }
- /**
- * Return Tri-state value constant
- *
- * @param mixed $credential Unique credential (user id, email address, user name)
- * @param string $token One-Time Token
- * @param string $persistentToken Persistent Token
- *
- * @return int
- */
- public function findTriplet($credential, $token, $persistentToken)
- {
- // Hash the tokens, because they can contain a salt and can be accessed in the file system
- $persistentToken = sha1($persistentToken);
- $token = sha1($token);
- $file = $this->getFile($credential);
- $tokens = (array)$file->content();
- if (!isset($tokens[$persistentToken]) || $tokens[$persistentToken] < time() + $this->timeout) {
- return self::TRIPLET_NOT_FOUND;
- }
- $stored = key($tokens[$persistentToken]);
- if ($stored !== $token) {
- return self::TRIPLET_INVALID;
- }
- return self::TRIPLET_FOUND;
- }
- /**
- * Store the new token for the credential and the persistent token. Create a new storage entry, if the combination
- * of credential and persistent token does not exist.
- *
- * @param mixed $credential
- * @param string $token
- * @param string $persistentToken
- * @param int $expire Timestamp when this triplet will expire (0 = no expiry)
- */
- public function storeTriplet($credential, $token, $persistentToken, $expire = null)
- {
- // Hash the tokens, because they can contain a salt and can be accessed in the file system
- $persistentToken = sha1($persistentToken);
- $token = sha1($token);
- $file = $this->getFile($credential);
- $tokens = (array)$file->content();
- // Update token
- $tokens[$persistentToken] = [$token => time()];
- $file->save($tokens);
- }
- /**
- * Replace current token after successful authentication
- *
- * @param mixed $credential
- * @param string $token
- * @param string $persistentToken
- * @param int $expire
- */
- public function replaceTriplet($credential, $token, $persistentToken, $expire = null)
- {
- $this->storeTriplet($credential, $token, $persistentToken, $expire);
- }
- /**
- * Remove one triplet of the user from the store
- *
- * @param mixed $credential
- * @param string $persistentToken
- */
- public function cleanTriplet($credential, $persistentToken)
- {
- // Hash the tokens, because they can contain a salt and can be accessed in the file system
- $persistentToken = sha1($persistentToken);
- $file = $this->getFile($credential);
- if (!$file->exists()) {
- return;
- }
- $tokens = (array)$file->content();
- if (isset($tokens[$persistentToken])) {
- // Delete token from storage
- unset($tokens[$persistentToken]);
- if ($tokens) {
- $file->save($tokens);
- } else {
- $file->delete();
- }
- }
- }
- /**
- * Remove all triplets of a user, effectively logging him out on all
- * machines
- *
- * @param mixed $credential
- */
- public function cleanAllTriplets($credential)
- {
- $file = $this->getFile($credential);
- if ($file->exists()) {
- $file->delete();
- }
- }
- /**
- * Helper method to clear RememberMe cache
- */
- public function clearCache()
- {
- if (is_dir($this->path)) {
- Folder::delete($this->path, false);
- }
- }
- /**
- * @param string $credential
- * @return CompiledYamlFile
- */
- protected function getFile($credential)
- {
- return CompiledYamlFile::instance($this->path . '/' . sha1($credential) . '.yaml');
- }
- }
|