1<?php 2 3namespace OAuth\Common\Storage; 4 5use OAuth\Common\Token\TokenInterface; 6use OAuth\Common\Storage\Exception\TokenNotFoundException; 7use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException; 8use Symfony\Component\HttpFoundation\Session\SessionInterface; 9 10class SymfonySession implements TokenStorageInterface 11{ 12 private $session; 13 private $sessionVariableName; 14 private $stateVariableName; 15 16 /** 17 * @param SessionInterface $session 18 * @param bool $startSession 19 * @param string $sessionVariableName 20 * @param string $stateVariableName 21 */ 22 public function __construct( 23 SessionInterface $session, 24 $startSession = true, 25 $sessionVariableName = 'lusitanian_oauth_token', 26 $stateVariableName = 'lusitanian_oauth_state' 27 ) { 28 $this->session = $session; 29 $this->sessionVariableName = $sessionVariableName; 30 $this->stateVariableName = $stateVariableName; 31 } 32 33 /** 34 * {@inheritDoc} 35 */ 36 public function retrieveAccessToken($service) 37 { 38 if ($this->hasAccessToken($service)) { 39 // get from session 40 $tokens = $this->session->get($this->sessionVariableName); 41 42 // one item 43 return $tokens[$service]; 44 } 45 46 throw new TokenNotFoundException('Token not found in session, are you sure you stored it?'); 47 } 48 49 /** 50 * {@inheritDoc} 51 */ 52 public function storeAccessToken($service, TokenInterface $token) 53 { 54 // get previously saved tokens 55 $tokens = $this->session->get($this->sessionVariableName); 56 57 if (!is_array($tokens)) { 58 $tokens = array(); 59 } 60 61 $tokens[$service] = $token; 62 63 // save 64 $this->session->set($this->sessionVariableName, $tokens); 65 66 // allow chaining 67 return $this; 68 } 69 70 /** 71 * {@inheritDoc} 72 */ 73 public function hasAccessToken($service) 74 { 75 // get from session 76 $tokens = $this->session->get($this->sessionVariableName); 77 78 return is_array($tokens) 79 && isset($tokens[$service]) 80 && $tokens[$service] instanceof TokenInterface; 81 } 82 83 /** 84 * {@inheritDoc} 85 */ 86 public function clearToken($service) 87 { 88 // get previously saved tokens 89 $tokens = $this->session->get($this->sessionVariableName); 90 91 if (is_array($tokens) && array_key_exists($service, $tokens)) { 92 unset($tokens[$service]); 93 94 // Replace the stored tokens array 95 $this->session->set($this->sessionVariableName, $tokens); 96 } 97 98 // allow chaining 99 return $this; 100 } 101 102 /** 103 * {@inheritDoc} 104 */ 105 public function clearAllTokens() 106 { 107 $this->session->remove($this->sessionVariableName); 108 109 // allow chaining 110 return $this; 111 } 112 113 /** 114 * {@inheritDoc} 115 */ 116 public function retrieveAuthorizationState($service) 117 { 118 if ($this->hasAuthorizationState($service)) { 119 // get from session 120 $states = $this->session->get($this->stateVariableName); 121 122 // one item 123 return $states[$service]; 124 } 125 126 throw new AuthorizationStateNotFoundException('State not found in session, are you sure you stored it?'); 127 } 128 129 /** 130 * {@inheritDoc} 131 */ 132 public function storeAuthorizationState($service, $state) 133 { 134 // get previously saved tokens 135 $states = $this->session->get($this->stateVariableName); 136 137 if (!is_array($states)) { 138 $states = array(); 139 } 140 141 $states[$service] = $state; 142 143 // save 144 $this->session->set($this->stateVariableName, $states); 145 146 // allow chaining 147 return $this; 148 } 149 150 /** 151 * {@inheritDoc} 152 */ 153 public function hasAuthorizationState($service) 154 { 155 // get from session 156 $states = $this->session->get($this->stateVariableName); 157 158 return is_array($states) 159 && isset($states[$service]) 160 && null !== $states[$service]; 161 } 162 163 /** 164 * {@inheritDoc} 165 */ 166 public function clearAuthorizationState($service) 167 { 168 // get previously saved tokens 169 $states = $this->session->get($this->stateVariableName); 170 171 if (is_array($states) && array_key_exists($service, $states)) { 172 unset($states[$service]); 173 174 // Replace the stored tokens array 175 $this->session->set($this->stateVariableName, $states); 176 } 177 178 // allow chaining 179 return $this; 180 } 181 182 /** 183 * {@inheritDoc} 184 */ 185 public function clearAllAuthorizationStates() 186 { 187 $this->session->remove($this->stateVariableName); 188 189 // allow chaining 190 return $this; 191 } 192 193 /** 194 * @return Session 195 */ 196 public function getSession() 197 { 198 return $this->session; 199 } 200} 201