diff --git a/README.md b/README.md index 2706588..e4cf614 100644 --- a/README.md +++ b/README.md @@ -155,8 +155,9 @@ return array( Add your credentials to ``app/config/packages/artdarek/oauth-4-laravel/config.php`` or ``app/config/oauth-4-laravel.php`` (depending on which option of configuration you choose) -The `Storage` attribute is optional and defaults to `Session`. -Other [options](https://github.com/Lusitanian/PHPoAuthLib/tree/master/src/OAuth/Common/Storage). +The `Storage` attribute is optional and defaults to `Session`. In addition to the +[other options](https://github.com/Lusitanian/PHPoAuthLib/tree/master/src/OAuth/Common/Storage) +provided by PHPoAuthLib, there is an additional session driver called `LaravelSession` which will use whatever is configured for Laravels own session driver. ## Usage diff --git a/src/Artdarek/OAuth/Common/Storage/LaravelSession.php b/src/Artdarek/OAuth/Common/Storage/LaravelSession.php new file mode 100644 index 0000000..e8ab9b1 --- /dev/null +++ b/src/Artdarek/OAuth/Common/Storage/LaravelSession.php @@ -0,0 +1,161 @@ +sessionNamespace = $sessionNamespace; + $this->sessionVariableName = $sessionNamespace.'.'.$sessionVariableName; + $this->stateVariableName = $sessionNamespace.'.'.$stateVariableName; + + } + + /** + * {@inheritDoc} + */ + public function retrieveAccessToken($service) + { + if ($this->hasAccessToken($service)) { + return Session::get($this->sessionVariableName.'.'.$service); + } + + throw new TokenNotFoundException('Token not found in session, are you sure you stored it?'); + } + + /** + * {@inheritDoc} + */ + public function storeAccessToken($service, TokenInterface $token) + { + Session::put($this->sessionVariableName.".".$service, $token); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAccessToken($service) + { + $token = Session::get($this->sessionVariableName.'.'.$service); + return !empty($token); + } + + /** + * {@inheritDoc} + */ + public function clearToken($service) + { + Session::forget($this->sessionVariableName . '.' . $service); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllTokens() + { + Session::forget($this->sessionVariableName); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function storeAuthorizationState($service, $state) + { + Session::put($this->stateVariableName.'.'.$service, $state); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function hasAuthorizationState($service) + { + $state = Session::get($this->stateVariableName.'.'.$service); + return !empty($state); + } + + /** + * {@inheritDoc} + */ + public function retrieveAuthorizationState($service) + { + if ($this->hasAuthorizationState($service)) { + return Session::get($this->stateVariableName.'.'.$service); + } + + throw new AuthorizationStateNotFoundException('State not found in session, are you sure you stored it?'); + } + + /** + * {@inheritDoc} + */ + public function clearAuthorizationState($service) + { + Session::forget($this->stateVariableName.'.'.$service); + + // allow chaining + return $this; + } + + /** + * {@inheritDoc} + */ + public function clearAllAuthorizationStates() + { + Session::forget($this->stateVariableName); + + // allow chaining + return $this; + } + + public function __destruct() + { + + } +} diff --git a/src/Artdarek/OAuth/OAuth.php b/src/Artdarek/OAuth/OAuth.php index a462283..3ae45ca 100644 --- a/src/Artdarek/OAuth/OAuth.php +++ b/src/Artdarek/OAuth/OAuth.php @@ -75,7 +75,7 @@ public function setConfig( $service ) $this->_client_secret = Config::get("oauth-4-laravel.consumers.$service.client_secret"); $this->_scope = Config::get("oauth-4-laravel.consumers.$service.scope", array() ); - // esle try to find config in packages configs + // else try to find config in packages configs } else { $this->_storage_name = Config::get('oauth-4-laravel::storage', 'Session'); $this->_client_id = Config::get("oauth-4-laravel::consumers.$service.client_id"); @@ -88,11 +88,15 @@ public function setConfig( $service ) * Create storage instance * * @param string $storageName - * @return OAuth\Common\\Storage + * @return \OAuth\Common\Storage */ public function createStorageInstance($storageName) { - $storageClass = "\\OAuth\\Common\\Storage\\$storageName"; + if ($storageName == "LaravelSession") { + $storageClass = "\\Artdarek\\OAuth\\Common\\Storage\\$storageName"; + } else { + $storageClass = "\\OAuth\\Common\\Storage\\$storageName"; + } $storage = new $storageClass(); return $storage;