198a36116SAndreas Gohr<?php 298a36116SAndreas Gohr 398a36116SAndreas Gohrnamespace dokuwiki\plugin\oauth; 498a36116SAndreas Gohr 598a36116SAndreas Gohruse OAuth\Common\Storage\Exception\TokenNotFoundException; 698a36116SAndreas Gohruse OAuth\Common\Storage\TokenStorageInterface; 798a36116SAndreas Gohruse OAuth\Common\Token\TokenInterface; 898a36116SAndreas Gohr 998a36116SAndreas Gohr/** 1098a36116SAndreas Gohr * Implements custom handling for storing tokens 1198a36116SAndreas Gohr */ 1298a36116SAndreas Gohrclass Storage implements TokenStorageInterface 1398a36116SAndreas Gohr{ 14*74b4d4a4SAndreas Gohr /** @var string */ 15*74b4d4a4SAndreas Gohr protected $guid; 1698a36116SAndreas Gohr 1798a36116SAndreas Gohr /** 18*74b4d4a4SAndreas Gohr * @param string $guid The GUID identifying the user 19*74b4d4a4SAndreas Gohr */ 20*74b4d4a4SAndreas Gohr public function __construct($guid) 21*74b4d4a4SAndreas Gohr { 22*74b4d4a4SAndreas Gohr $this->guid = $guid; 23*74b4d4a4SAndreas Gohr } 24*74b4d4a4SAndreas Gohr 25*74b4d4a4SAndreas Gohr /** 26*74b4d4a4SAndreas Gohr * The path to the file where tokens for this service and user are stored 2798a36116SAndreas Gohr * 2898a36116SAndreas Gohr * @param string $service 2998a36116SAndreas Gohr * @return string 3098a36116SAndreas Gohr */ 3198a36116SAndreas Gohr protected function getServiceFile($service) 3298a36116SAndreas Gohr { 33*74b4d4a4SAndreas Gohr return getCacheName($this->guid . $service, '.oauth'); 3498a36116SAndreas Gohr } 3598a36116SAndreas Gohr 3698a36116SAndreas Gohr /** 3798a36116SAndreas Gohr * Load the data from disk 3898a36116SAndreas Gohr * 3998a36116SAndreas Gohr * @param string $service 4098a36116SAndreas Gohr * @return array 4198a36116SAndreas Gohr */ 4298a36116SAndreas Gohr protected function loadServiceFile($service) 4398a36116SAndreas Gohr { 4498a36116SAndreas Gohr $file = $this->getServiceFile($service); 4598a36116SAndreas Gohr if (file_exists($file)) { 4698a36116SAndreas Gohr return unserialize(io_readFile($file, false)); 4798a36116SAndreas Gohr } else { 4898a36116SAndreas Gohr return array(); 4998a36116SAndreas Gohr } 5098a36116SAndreas Gohr } 5198a36116SAndreas Gohr 5298a36116SAndreas Gohr /** 5398a36116SAndreas Gohr * Store the data to disk 5498a36116SAndreas Gohr * 5598a36116SAndreas Gohr * @param string $service 5698a36116SAndreas Gohr * @param array $data 5798a36116SAndreas Gohr */ 5898a36116SAndreas Gohr protected function saveServiceFile($service, $data) 5998a36116SAndreas Gohr { 6098a36116SAndreas Gohr $file = $this->getServiceFile($service); 6198a36116SAndreas Gohr io_saveFile($file, serialize($data)); 6298a36116SAndreas Gohr } 6398a36116SAndreas Gohr 6498a36116SAndreas Gohr /** @inheritDoc */ 6598a36116SAndreas Gohr public function retrieveAccessToken($service) 6698a36116SAndreas Gohr { 6798a36116SAndreas Gohr $data = $this->loadServiceFile($service); 6898a36116SAndreas Gohr if (!isset($data['token'])) { 6998a36116SAndreas Gohr throw new TokenNotFoundException('No token found in storage'); 7098a36116SAndreas Gohr } 7198a36116SAndreas Gohr return $data['token']; 7298a36116SAndreas Gohr } 7398a36116SAndreas Gohr 7498a36116SAndreas Gohr /** @inheritDoc */ 7598a36116SAndreas Gohr public function storeAccessToken($service, TokenInterface $token) 7698a36116SAndreas Gohr { 7798a36116SAndreas Gohr $data = $this->loadServiceFile($service); 7898a36116SAndreas Gohr $data['token'] = $token; 7998a36116SAndreas Gohr $this->saveServiceFile($service, $data); 8098a36116SAndreas Gohr } 8198a36116SAndreas Gohr 8298a36116SAndreas Gohr /** @inheritDoc */ 8398a36116SAndreas Gohr public function hasAccessToken($service) 8498a36116SAndreas Gohr { 8598a36116SAndreas Gohr $data = $this->loadServiceFile($service); 8698a36116SAndreas Gohr return isset($data['token']); 8798a36116SAndreas Gohr } 8898a36116SAndreas Gohr 8998a36116SAndreas Gohr /** @inheritDoc */ 9098a36116SAndreas Gohr public function clearToken($service) 9198a36116SAndreas Gohr { 9298a36116SAndreas Gohr $data = $this->loadServiceFile($service); 9398a36116SAndreas Gohr if (isset($data['token'])) unset($data['token']); 9498a36116SAndreas Gohr $this->saveServiceFile($service, $data); 9598a36116SAndreas Gohr 9698a36116SAndreas Gohr return $this; 9798a36116SAndreas Gohr } 9898a36116SAndreas Gohr 9998a36116SAndreas Gohr /** @inheritDoc */ 10098a36116SAndreas Gohr public function clearAllTokens() 10198a36116SAndreas Gohr { 10298a36116SAndreas Gohr // TODO: Implement clearAllTokens() method. 10398a36116SAndreas Gohr return $this; 10498a36116SAndreas Gohr } 10598a36116SAndreas Gohr 10698a36116SAndreas Gohr /** @inheritDoc */ 10798a36116SAndreas Gohr public function storeAuthorizationState($service, $state) 10898a36116SAndreas Gohr { 10998a36116SAndreas Gohr $data = $this->loadServiceFile($service); 11098a36116SAndreas Gohr $data['state'] = $state; 11198a36116SAndreas Gohr $this->saveServiceFile($service, $data); 11298a36116SAndreas Gohr return $this; 11398a36116SAndreas Gohr } 11498a36116SAndreas Gohr 11598a36116SAndreas Gohr /** @inheritDoc */ 11698a36116SAndreas Gohr public function hasAuthorizationState($service) 11798a36116SAndreas Gohr { 11898a36116SAndreas Gohr $data = $this->loadServiceFile($service); 11998a36116SAndreas Gohr return isset($data['state']); 12098a36116SAndreas Gohr } 12198a36116SAndreas Gohr 12298a36116SAndreas Gohr /** 12398a36116SAndreas Gohr * @inheritDoc 12498a36116SAndreas Gohr * @throws TokenNotFoundException 12598a36116SAndreas Gohr */ 12698a36116SAndreas Gohr public function retrieveAuthorizationState($service) 12798a36116SAndreas Gohr { 12898a36116SAndreas Gohr $data = $this->loadServiceFile($service); 12998a36116SAndreas Gohr if (!isset($data['state'])) { 13098a36116SAndreas Gohr throw new TokenNotFoundException('No state found in storage'); 13198a36116SAndreas Gohr } 13298a36116SAndreas Gohr return $data['state']; 13398a36116SAndreas Gohr } 13498a36116SAndreas Gohr 13598a36116SAndreas Gohr /** @inheritDoc */ 13698a36116SAndreas Gohr public function clearAuthorizationState($service) 13798a36116SAndreas Gohr { 13898a36116SAndreas Gohr $data = $this->loadServiceFile($service); 13998a36116SAndreas Gohr if (isset($data['state'])) unset($data['state']); 14098a36116SAndreas Gohr $this->saveServiceFile($service, $data); 14198a36116SAndreas Gohr 14298a36116SAndreas Gohr return $this; 14398a36116SAndreas Gohr } 14498a36116SAndreas Gohr 14598a36116SAndreas Gohr /** @inheritDoc */ 14698a36116SAndreas Gohr public function clearAllAuthorizationStates() 14798a36116SAndreas Gohr { 14898a36116SAndreas Gohr // TODO: Implement clearAllAuthorizationStates() method. 14998a36116SAndreas Gohr 15098a36116SAndreas Gohr return $this; 15198a36116SAndreas Gohr } 15298a36116SAndreas Gohr} 153