diff options
author | Frédéric Guillot <fguillot@users.noreply.github.com> | 2014-05-03 22:24:03 -0400 |
---|---|---|
committer | Frédéric Guillot <fguillot@users.noreply.github.com> | 2014-05-03 22:24:03 -0400 |
commit | 560a12f0bd6347a335f8ed5201d6d9562d03d4bc (patch) | |
tree | 00510d25c1cf5e747573543fa88d44ef003b1c9a /vendor/OAuth/Common/Storage | |
parent | 9531e439cd99fb7dbcfb039f422f1d1ba414ec30 (diff) |
Add Google authentication
Diffstat (limited to 'vendor/OAuth/Common/Storage')
-rwxr-xr-x | vendor/OAuth/Common/Storage/Exception/AuthorizationStateNotFoundException.php | 10 | ||||
-rwxr-xr-x | vendor/OAuth/Common/Storage/Exception/StorageException.php | 12 | ||||
-rwxr-xr-x | vendor/OAuth/Common/Storage/Exception/TokenNotFoundException.php | 10 | ||||
-rwxr-xr-x | vendor/OAuth/Common/Storage/Memory.php | 139 | ||||
-rwxr-xr-x | vendor/OAuth/Common/Storage/Redis.php | 230 | ||||
-rwxr-xr-x | vendor/OAuth/Common/Storage/Session.php | 188 | ||||
-rwxr-xr-x | vendor/OAuth/Common/Storage/SymfonySession.php | 200 | ||||
-rwxr-xr-x | vendor/OAuth/Common/Storage/TokenStorageInterface.php | 98 |
8 files changed, 887 insertions, 0 deletions
diff --git a/vendor/OAuth/Common/Storage/Exception/AuthorizationStateNotFoundException.php b/vendor/OAuth/Common/Storage/Exception/AuthorizationStateNotFoundException.php new file mode 100755 index 00000000..b3daeabb --- /dev/null +++ b/vendor/OAuth/Common/Storage/Exception/AuthorizationStateNotFoundException.php @@ -0,0 +1,10 @@ +<?php + +namespace OAuth\Common\Storage\Exception; + +/** + * Exception thrown when a state is not found in storage. + */ +class AuthorizationStateNotFoundException extends StorageException +{ +} diff --git a/vendor/OAuth/Common/Storage/Exception/StorageException.php b/vendor/OAuth/Common/Storage/Exception/StorageException.php new file mode 100755 index 00000000..4378ee8f --- /dev/null +++ b/vendor/OAuth/Common/Storage/Exception/StorageException.php @@ -0,0 +1,12 @@ +<?php + +namespace OAuth\Common\Storage\Exception; + +use OAuth\Common\Exception\Exception; + +/** + * Generic storage exception. + */ +class StorageException extends Exception +{ +} diff --git a/vendor/OAuth/Common/Storage/Exception/TokenNotFoundException.php b/vendor/OAuth/Common/Storage/Exception/TokenNotFoundException.php new file mode 100755 index 00000000..06222508 --- /dev/null +++ b/vendor/OAuth/Common/Storage/Exception/TokenNotFoundException.php @@ -0,0 +1,10 @@ +<?php + +namespace OAuth\Common\Storage\Exception; + +/** + * Exception thrown when a token is not found in storage. + */ +class TokenNotFoundException extends StorageException +{ +} diff --git a/vendor/OAuth/Common/Storage/Memory.php b/vendor/OAuth/Common/Storage/Memory.php new file mode 100755 index 00000000..d42c2251 --- /dev/null +++ b/vendor/OAuth/Common/Storage/Memory.php @@ -0,0 +1,139 @@ +<?php + +namespace OAuth\Common\Storage; + +use OAuth\Common\Token\TokenInterface; +use OAuth\Common\Storage\Exception\TokenNotFoundException; +use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException; + +/* + * Stores a token in-memory only (destroyed at end of script execution). + */ +class Memory implements TokenStorageInterface +{ + /** + * @var object|TokenInterface + */ + protected $tokens; + + /** + * @var array + */ + protected $states; + + public function __construct() + { + $this->tokens = array(); + $this->states = array(); + } + + /** + * {@inheritDoc} + */ + public function retrieveAccessToken($service) + { + if ($this->hasAccessToken($service)) { + return $this->tokens[$service]; + } + + throw new TokenNotFoundException('Token not stored'); + } + + /** + * {@inheritDoc} + */ + public function storeAccessToken($service, TokenInterface $token) + { + $this->tokens[$service] = $token; + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAccessToken($service) + { + return isset($this->tokens[$service]) && $this->tokens[$service] instanceof TokenInterface; + } + + /** + * {@inheritDoc} + */ + public function clearToken($service) + { + if (array_key_exists($service, $this->tokens)) { + unset($this->tokens[$service]); + } + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllTokens() + { + $this->tokens = array(); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function retrieveAuthorizationState($service) + { + if ($this->hasAuthorizationState($service)) { + return $this->states[$service]; + } + + throw new AuthorizationStateNotFoundException('State not stored'); + } + + /** + * {@inheritDoc} + */ + public function storeAuthorizationState($service, $state) + { + $this->states[$service] = $state; + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAuthorizationState($service) + { + return isset($this->states[$service]) && null !== $this->states[$service]; + } + + /** + * {@inheritDoc} + */ + public function clearAuthorizationState($service) + { + if (array_key_exists($service, $this->states)) { + unset($this->states[$service]); + } + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllAuthorizationStates() + { + $this->states = array(); + + // allow chaining + return $this; + } +} diff --git a/vendor/OAuth/Common/Storage/Redis.php b/vendor/OAuth/Common/Storage/Redis.php new file mode 100755 index 00000000..77318bd6 --- /dev/null +++ b/vendor/OAuth/Common/Storage/Redis.php @@ -0,0 +1,230 @@ +<?php + +namespace OAuth\Common\Storage; + +use OAuth\Common\Token\TokenInterface; +use OAuth\Common\Storage\Exception\TokenNotFoundException; +use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException; +use Predis\Client as Predis; + +/* + * Stores a token in a Redis server. Requires the Predis library available at https://github.com/nrk/predis + */ +class Redis implements TokenStorageInterface +{ + /** + * @var string + */ + protected $key; + + protected $stateKey; + + /** + * @var object|\Redis + */ + protected $redis; + + /** + * @var object|TokenInterface + */ + protected $cachedTokens; + + /** + * @var object + */ + protected $cachedStates; + + /** + * @param Predis $redis An instantiated and connected redis client + * @param string $key The key to store the token under in redis + * @param string $stateKey The key to store the state under in redis. + */ + public function __construct(Predis $redis, $key, $stateKey) + { + $this->redis = $redis; + $this->key = $key; + $this->stateKey = $stateKey; + $this->cachedTokens = array(); + $this->cachedStates = array(); + } + + /** + * {@inheritDoc} + */ + public function retrieveAccessToken($service) + { + if (!$this->hasAccessToken($service)) { + throw new TokenNotFoundException('Token not found in redis'); + } + + if (isset($this->cachedTokens[$service])) { + return $this->cachedTokens[$service]; + } + + $val = $this->redis->hget($this->key, $service); + + return $this->cachedTokens[$service] = unserialize($val); + } + + /** + * {@inheritDoc} + */ + public function storeAccessToken($service, TokenInterface $token) + { + // (over)write the token + $this->redis->hset($this->key, $service, serialize($token)); + $this->cachedTokens[$service] = $token; + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAccessToken($service) + { + if (isset($this->cachedTokens[$service]) + && $this->cachedTokens[$service] instanceof TokenInterface + ) { + return true; + } + + return $this->redis->hexists($this->key, $service); + } + + /** + * {@inheritDoc} + */ + public function clearToken($service) + { + $this->redis->hdel($this->key, $service); + unset($this->cachedTokens[$service]); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllTokens() + { + // memory + $this->cachedTokens = array(); + + // redis + $keys = $this->redis->hkeys($this->key); + $me = $this; // 5.3 compat + + // pipeline for performance + $this->redis->pipeline( + function ($pipe) use ($keys, $me) { + foreach ($keys as $k) { + $pipe->hdel($me->getKey(), $k); + } + } + ); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function retrieveAuthorizationState($service) + { + if (!$this->hasAuthorizationState($service)) { + throw new AuthorizationStateNotFoundException('State not found in redis'); + } + + if (isset($this->cachedStates[$service])) { + return $this->cachedStates[$service]; + } + + $val = $this->redis->hget($this->stateKey, $service); + + return $this->cachedStates[$service] = unserialize($val); + } + + /** + * {@inheritDoc} + */ + public function storeAuthorizationState($service, $state) + { + // (over)write the token + $this->redis->hset($this->stateKey, $service, $state); + $this->cachedStates[$service] = $state; + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAuthorizationState($service) + { + if (isset($this->cachedStates[$service]) + && null !== $this->cachedStates[$service] + ) { + return true; + } + + return $this->redis->hexists($this->stateKey, $service); + } + + /** + * {@inheritDoc} + */ + public function clearAuthorizationState($service) + { + $this->redis->hdel($this->stateKey, $service); + unset($this->cachedStates[$service]); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllAuthorizationStates() + { + // memory + $this->cachedStates = array(); + + // redis + $keys = $this->redis->hkeys($this->stateKey); + $me = $this; // 5.3 compat + + // pipeline for performance + $this->redis->pipeline( + function ($pipe) use ($keys, $me) { + foreach ($keys as $k) { + $pipe->hdel($me->getKey(), $k); + } + } + ); + + // allow chaining + return $this; + } + + /** + * @return Predis $redis + */ + public function getRedis() + { + return $this->redis; + } + + /** + * @return string $key + */ + public function getKey() + { + return $this->key; + } +} diff --git a/vendor/OAuth/Common/Storage/Session.php b/vendor/OAuth/Common/Storage/Session.php new file mode 100755 index 00000000..e908a67e --- /dev/null +++ b/vendor/OAuth/Common/Storage/Session.php @@ -0,0 +1,188 @@ +<?php + +namespace OAuth\Common\Storage; + +use OAuth\Common\Token\TokenInterface; +use OAuth\Common\Storage\Exception\TokenNotFoundException; +use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException; + +/** + * Stores a token in a PHP session. + */ +class Session implements TokenStorageInterface +{ + /** + * @var bool + */ + protected $startSession; + + /** + * @var string + */ + protected $sessionVariableName; + + /** + * @var string + */ + protected $stateVariableName; + + /** + * @param bool $startSession Whether or not to start the session upon construction. + * @param string $sessionVariableName the variable name to use within the _SESSION superglobal + * @param string $stateVariableName + */ + public function __construct( + $startSession = true, + $sessionVariableName = 'lusitanian_oauth_token', + $stateVariableName = 'lusitanian_oauth_state' + ) { + if ($startSession && !isset($_SESSION)) { + session_start(); + } + + $this->startSession = $startSession; + $this->sessionVariableName = $sessionVariableName; + $this->stateVariableName = $stateVariableName; + if (!isset($_SESSION[$sessionVariableName])) { + $_SESSION[$sessionVariableName] = array(); + } + if (!isset($_SESSION[$stateVariableName])) { + $_SESSION[$stateVariableName] = array(); + } + } + + /** + * {@inheritDoc} + */ + public function retrieveAccessToken($service) + { + if ($this->hasAccessToken($service)) { + return unserialize($_SESSION[$this->sessionVariableName][$service]); + } + + throw new TokenNotFoundException('Token not found in session, are you sure you stored it?'); + } + + /** + * {@inheritDoc} + */ + public function storeAccessToken($service, TokenInterface $token) + { + $serializedToken = serialize($token); + + if (isset($_SESSION[$this->sessionVariableName]) + && is_array($_SESSION[$this->sessionVariableName]) + ) { + $_SESSION[$this->sessionVariableName][$service] = $serializedToken; + } else { + $_SESSION[$this->sessionVariableName] = array( + $service => $serializedToken, + ); + } + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAccessToken($service) + { + return isset($_SESSION[$this->sessionVariableName], $_SESSION[$this->sessionVariableName][$service]); + } + + /** + * {@inheritDoc} + */ + public function clearToken($service) + { + if (array_key_exists($service, $_SESSION[$this->sessionVariableName])) { + unset($_SESSION[$this->sessionVariableName][$service]); + } + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllTokens() + { + unset($_SESSION[$this->sessionVariableName]); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function storeAuthorizationState($service, $state) + { + if (isset($_SESSION[$this->stateVariableName]) + && is_array($_SESSION[$this->stateVariableName]) + ) { + $_SESSION[$this->stateVariableName][$service] = $state; + } else { + $_SESSION[$this->stateVariableName] = array( + $service => $state, + ); + } + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAuthorizationState($service) + { + return isset($_SESSION[$this->stateVariableName], $_SESSION[$this->stateVariableName][$service]); + } + + /** + * {@inheritDoc} + */ + public function retrieveAuthorizationState($service) + { + if ($this->hasAuthorizationState($service)) { + return $_SESSION[$this->stateVariableName][$service]; + } + + throw new AuthorizationStateNotFoundException('State not found in session, are you sure you stored it?'); + } + + /** + * {@inheritDoc} + */ + public function clearAuthorizationState($service) + { + if (array_key_exists($service, $_SESSION[$this->stateVariableName])) { + unset($_SESSION[$this->stateVariableName][$service]); + } + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllAuthorizationStates() + { + unset($_SESSION[$this->stateVariableName]); + + // allow chaining + return $this; + } + + public function __destruct() + { + if ($this->startSession) { + session_write_close(); + } + } +} diff --git a/vendor/OAuth/Common/Storage/SymfonySession.php b/vendor/OAuth/Common/Storage/SymfonySession.php new file mode 100755 index 00000000..6c5fbf60 --- /dev/null +++ b/vendor/OAuth/Common/Storage/SymfonySession.php @@ -0,0 +1,200 @@ +<?php + +namespace OAuth\Common\Storage; + +use OAuth\Common\Token\TokenInterface; +use OAuth\Common\Storage\Exception\TokenNotFoundException; +use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException; +use Symfony\Component\HttpFoundation\Session\SessionInterface; + +class SymfonySession implements TokenStorageInterface +{ + private $session; + private $sessionVariableName; + private $stateVariableName; + + /** + * @param SessionInterface $session + * @param bool $startSession + * @param string $sessionVariableName + * @param string $stateVariableName + */ + public function __construct( + SessionInterface $session, + $startSession = true, + $sessionVariableName = 'lusitanian_oauth_token', + $stateVariableName = 'lusitanian_oauth_state' + ) { + $this->session = $session; + $this->sessionVariableName = $sessionVariableName; + $this->stateVariableName = $stateVariableName; + } + + /** + * {@inheritDoc} + */ + public function retrieveAccessToken($service) + { + if ($this->hasAccessToken($service)) { + // get from session + $tokens = $this->session->get($this->sessionVariableName); + + // one item + return $tokens[$service]; + } + + throw new TokenNotFoundException('Token not found in session, are you sure you stored it?'); + } + + /** + * {@inheritDoc} + */ + public function storeAccessToken($service, TokenInterface $token) + { + // get previously saved tokens + $tokens = $this->session->get($this->sessionVariableName); + + if (!is_array($tokens)) { + $tokens = array(); + } + + $tokens[$service] = $token; + + // save + $this->session->set($this->sessionVariableName, $tokens); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAccessToken($service) + { + // get from session + $tokens = $this->session->get($this->sessionVariableName); + + return is_array($tokens) + && isset($tokens[$service]) + && $tokens[$service] instanceof TokenInterface; + } + + /** + * {@inheritDoc} + */ + public function clearToken($service) + { + // get previously saved tokens + $tokens = $this->session->get($this->sessionVariableName); + + if (is_array($tokens) && array_key_exists($service, $tokens)) { + unset($tokens[$service]); + + // Replace the stored tokens array + $this->session->set($this->sessionVariableName, $tokens); + } + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllTokens() + { + $this->session->remove($this->sessionVariableName); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function retrieveAuthorizationState($service) + { + if ($this->hasAuthorizationState($service)) { + // get from session + $states = $this->session->get($this->stateVariableName); + + // one item + return $states[$service]; + } + + throw new AuthorizationStateNotFoundException('State not found in session, are you sure you stored it?'); + } + + /** + * {@inheritDoc} + */ + public function storeAuthorizationState($service, $state) + { + // get previously saved tokens + $states = $this->session->get($this->stateVariableName); + + if (!is_array($states)) { + $states = array(); + } + + $states[$service] = $state; + + // save + $this->session->set($this->stateVariableName, $states); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAuthorizationState($service) + { + // get from session + $states = $this->session->get($this->stateVariableName); + + return is_array($states) + && isset($states[$service]) + && null !== $states[$service]; + } + + /** + * {@inheritDoc} + */ + public function clearAuthorizationState($service) + { + // get previously saved tokens + $states = $this->session->get($this->stateVariableName); + + if (is_array($states) && array_key_exists($service, $states)) { + unset($states[$service]); + + // Replace the stored tokens array + $this->session->set($this->stateVariableName, $states); + } + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllAuthorizationStates() + { + $this->session->remove($this->stateVariableName); + + // allow chaining + return $this; + } + + /** + * @return Session + */ + public function getSession() + { + return $this->session; + } +} diff --git a/vendor/OAuth/Common/Storage/TokenStorageInterface.php b/vendor/OAuth/Common/Storage/TokenStorageInterface.php new file mode 100755 index 00000000..46552cee --- /dev/null +++ b/vendor/OAuth/Common/Storage/TokenStorageInterface.php @@ -0,0 +1,98 @@ +<?php + +namespace OAuth\Common\Storage; + +use OAuth\Common\Token\TokenInterface; +use OAuth\Common\Storage\Exception\TokenNotFoundException; + +/** + * All token storage providers must implement this interface. + */ +interface TokenStorageInterface +{ + /** + * @param string $service + * + * @return TokenInterface + * + * @throws TokenNotFoundException + */ + public function retrieveAccessToken($service); + + /** + * @param string $service + * @param TokenInterface $token + * + * @return TokenStorageInterface + */ + public function storeAccessToken($service, TokenInterface $token); + + /** + * @param string $service + * + * @return bool + */ + public function hasAccessToken($service); + + /** + * Delete the users token. Aka, log out. + * + * @param string $service + * + * @return TokenStorageInterface + */ + public function clearToken($service); + + /** + * Delete *ALL* user tokens. Use with care. Most of the time you will likely + * want to use clearToken() instead. + * + * @return TokenStorageInterface + */ + public function clearAllTokens(); + + /** + * Store the authorization state related to a given service + * + * @param string $service + * @param string $state + * + * @return TokenStorageInterface + */ + public function storeAuthorizationState($service, $state); + + /** + * Check if an authorization state for a given service exists + * + * @param string $service + * + * @return bool + */ + public function hasAuthorizationState($service); + + /** + * Retrieve the authorization state for a given service + * + * @param string $service + * + * @return string + */ + public function retrieveAuthorizationState($service); + + /** + * Clear the authorization state of a given service + * + * @param string $service + * + * @return TokenStorageInterface + */ + public function clearAuthorizationState($service); + + /** + * Delete *ALL* user authorization states. Use with care. Most of the time you will likely + * want to use clearAuthorization() instead. + * + * @return TokenStorageInterface + */ + public function clearAllAuthorizationStates(); +} |