1<?php 2 3namespace dokuwiki\plugin\oauth; 4 5use OAuth\Common\Storage\Exception\TokenNotFoundException; 6use OAuth\Common\Storage\TokenStorageInterface; 7use OAuth\Common\Token\TokenInterface; 8 9/** 10 * Implements custom handling for storing tokens 11 */ 12class Storage implements TokenStorageInterface 13{ 14 15 /** 16 * The path to the file where tokens for this service are stored 17 * 18 * @param string $service 19 * @return string 20 */ 21 protected function getServiceFile($service) 22 { 23 return getCacheName($service, '.oauth'); 24 } 25 26 /** 27 * Load the data from disk 28 * 29 * @param string $service 30 * @return array 31 */ 32 protected function loadServiceFile($service) 33 { 34 $file = $this->getServiceFile($service); 35 if (file_exists($file)) { 36 return unserialize(io_readFile($file, false)); 37 } else { 38 return array(); 39 } 40 } 41 42 /** 43 * Store the data to disk 44 * 45 * @param string $service 46 * @param array $data 47 */ 48 protected function saveServiceFile($service, $data) 49 { 50 $file = $this->getServiceFile($service); 51 io_saveFile($file, serialize($data)); 52 } 53 54 /** @inheritDoc */ 55 public function retrieveAccessToken($service) 56 { 57 $data = $this->loadServiceFile($service); 58 if (!isset($data['token'])) { 59 throw new TokenNotFoundException('No token found in storage'); 60 } 61 return $data['token']; 62 } 63 64 /** @inheritDoc */ 65 public function storeAccessToken($service, TokenInterface $token) 66 { 67 $data = $this->loadServiceFile($service); 68 $data['token'] = $token; 69 $this->saveServiceFile($service, $data); 70 } 71 72 /** @inheritDoc */ 73 public function hasAccessToken($service) 74 { 75 $data = $this->loadServiceFile($service); 76 return isset($data['token']); 77 } 78 79 /** @inheritDoc */ 80 public function clearToken($service) 81 { 82 $data = $this->loadServiceFile($service); 83 if (isset($data['token'])) unset($data['token']); 84 $this->saveServiceFile($service, $data); 85 86 return $this; 87 } 88 89 /** @inheritDoc */ 90 public function clearAllTokens() 91 { 92 // TODO: Implement clearAllTokens() method. 93 return $this; 94 } 95 96 /** @inheritDoc */ 97 public function storeAuthorizationState($service, $state) 98 { 99 $data = $this->loadServiceFile($service); 100 $data['state'] = $state; 101 $this->saveServiceFile($service, $data); 102 return $this; 103 } 104 105 /** @inheritDoc */ 106 public function hasAuthorizationState($service) 107 { 108 $data = $this->loadServiceFile($service); 109 return isset($data['state']); 110 } 111 112 /** 113 * @inheritDoc 114 * @throws TokenNotFoundException 115 */ 116 public function retrieveAuthorizationState($service) 117 { 118 $data = $this->loadServiceFile($service); 119 if (!isset($data['state'])) { 120 throw new TokenNotFoundException('No state found in storage'); 121 } 122 return $data['state']; 123 } 124 125 /** @inheritDoc */ 126 public function clearAuthorizationState($service) 127 { 128 $data = $this->loadServiceFile($service); 129 if (isset($data['state'])) unset($data['state']); 130 $this->saveServiceFile($service, $data); 131 132 return $this; 133 } 134 135 /** @inheritDoc */ 136 public function clearAllAuthorizationStates() 137 { 138 // TODO: Implement clearAllAuthorizationStates() method. 139 140 return $this; 141 } 142} 143