OAuthCommonStorageSession

Stores a token in a PHP session.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/src/OAuth/Common/Storage/Session.php  
  1. class Session implements TokenStorageInterface 
  2. /** 
  3. * @var string 
  4. */ 
  5. protected $sessionVariableName; 
  6.  
  7. /** 
  8. * @var string 
  9. */ 
  10. protected $stateVariableName; 
  11.  
  12. /** 
  13. * @param bool $startSession Whether or not to start the session upon construction. 
  14. * @param string $sessionVariableName the variable name to use within the _SESSION superglobal 
  15. * @param string $stateVariableName 
  16. */ 
  17. public function __construct( 
  18. $startSession = true,  
  19. $sessionVariableName = 'lusitanian_oauth_token',  
  20. $stateVariableName = 'lusitanian_oauth_state' 
  21. ) { 
  22. if ($startSession && !isset($_SESSION)) { 
  23. session_start(); 
  24.  
  25. $this->sessionVariableName = $sessionVariableName; 
  26. $this->stateVariableName = $stateVariableName; 
  27. if (!isset($_SESSION[$sessionVariableName])) { 
  28. $_SESSION[$sessionVariableName] = array(); 
  29. if (!isset($_SESSION[$stateVariableName])) { 
  30. $_SESSION[$stateVariableName] = array(); 
  31.  
  32. /** 
  33. * {@inheritDoc} 
  34. */ 
  35. public function retrieveAccessToken($service) 
  36. if ($this->hasAccessToken($service)) { 
  37. return unserialize($_SESSION[$this->sessionVariableName][$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. $serializedToken = serialize($token); 
  46.  
  47. if (isset($_SESSION[$this->sessionVariableName]) 
  48. && is_array($_SESSION[$this->sessionVariableName]) 
  49. ) { 
  50. $_SESSION[$this->sessionVariableName][$service] = $serializedToken; 
  51. } else { 
  52. $_SESSION[$this->sessionVariableName] = array( 
  53. $service => $serializedToken,  
  54. ); 
  55.  
  56. // allow chaining 
  57. return $this; 
  58.  
  59. /** 
  60. * {@inheritDoc} 
  61. */ 
  62. public function hasAccessToken($service) 
  63. return isset($_SESSION[$this->sessionVariableName], $_SESSION[$this->sessionVariableName][$service]); 
  64.  
  65. /** 
  66. * {@inheritDoc} 
  67. */ 
  68. public function clearToken($service) 
  69. if (array_key_exists($service, $_SESSION[$this->sessionVariableName])) { 
  70. unset($_SESSION[$this->sessionVariableName][$service]); 
  71.  
  72. // allow chaining 
  73. return $this; 
  74.  
  75. /** 
  76. * {@inheritDoc} 
  77. */ 
  78. public function clearAllTokens() 
  79. unset($_SESSION[$this->sessionVariableName]); 
  80.  
  81. // allow chaining 
  82. return $this; 
  83.  
  84. /** 
  85. * {@inheritDoc} 
  86. */ 
  87. public function storeAuthorizationState($service, $state) 
  88. if (isset($_SESSION[$this->stateVariableName]) 
  89. && is_array($_SESSION[$this->stateVariableName]) 
  90. ) { 
  91. $_SESSION[$this->stateVariableName][$service] = $state; 
  92. } else { 
  93. $_SESSION[$this->stateVariableName] = array( 
  94. $service => $state,  
  95. ); 
  96.  
  97. // allow chaining 
  98. return $this; 
  99.  
  100. /** 
  101. * {@inheritDoc} 
  102. */ 
  103. public function hasAuthorizationState($service) 
  104. return isset($_SESSION[$this->stateVariableName], $_SESSION[$this->stateVariableName][$service]); 
  105.  
  106. /** 
  107. * {@inheritDoc} 
  108. */ 
  109. public function retrieveAuthorizationState($service) 
  110. if ($this->hasAuthorizationState($service)) { 
  111. return $_SESSION[$this->stateVariableName][$service]; 
  112.  
  113. throw new AuthorizationStateNotFoundException('State not found in session, are you sure you stored it?'); 
  114.  
  115. /** 
  116. * {@inheritDoc} 
  117. */ 
  118. public function clearAuthorizationState($service) 
  119. if (array_key_exists($service, $_SESSION[$this->stateVariableName])) { 
  120. unset($_SESSION[$this->stateVariableName][$service]); 
  121.  
  122. // allow chaining 
  123. return $this; 
  124.  
  125. /** 
  126. * {@inheritDoc} 
  127. */ 
  128. public function clearAllAuthorizationStates() 
  129. unset($_SESSION[$this->stateVariableName]); 
  130.  
  131. // allow chaining 
  132. return $this; 
  133.  
  134. public function __destruct() 
  135. session_write_close();