SymfonySession.php 4.8 KB

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