MemStore.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * In-memory OpenID store implementation for testing only
  4. */
  5. require_once "Auth/OpenID/Interface.php";
  6. require_once 'Auth/OpenID/Nonce.php';
  7. class ServerAssocs {
  8. function ServerAssocs()
  9. {
  10. $this->assocs = array();
  11. }
  12. function set($assoc)
  13. {
  14. $this->assocs[$assoc->handle] = $assoc;
  15. }
  16. function get($handle)
  17. {
  18. return Auth_OpenID::arrayGet($this->assocs, $handle);
  19. }
  20. function remove($handle)
  21. {
  22. if (array_key_exists($handle, $this->assocs)) {
  23. unset($this->assocs[$handle]);
  24. return true;
  25. } else {
  26. return false;
  27. }
  28. }
  29. /*
  30. * Returns association with the oldest issued date.
  31. *
  32. * or null if there are no associations.
  33. */
  34. function best()
  35. {
  36. $best = null;
  37. foreach ($this->assocs as $handle => $assoc) {
  38. if (($best === null) || ($best->issued < $assoc->issued)) {
  39. $best = $assoc;
  40. }
  41. }
  42. return $best;
  43. }
  44. /*
  45. * Remove expired associations.
  46. *
  47. * @return (removed associations, remaining associations)
  48. */
  49. function cleanup()
  50. {
  51. $remove = array();
  52. foreach ($this->assocs as $handle => $assoc) {
  53. if ($assoc->getExpiresIn() == 0) {
  54. $remove[] = $handle;
  55. }
  56. }
  57. foreach ($remove as $handle) {
  58. unset($this->assocs[$handle]);
  59. }
  60. return array(count($remove), count($this->assocs));
  61. }
  62. }
  63. /*
  64. * In-process memory store.
  65. *
  66. * Use for single long-running processes. No persistence supplied.
  67. */
  68. class Tests_Auth_OpenID_MemStore extends Auth_OpenID_OpenIDStore {
  69. function Tests_Auth_OpenID_MemStore()
  70. {
  71. $this->server_assocs = array();
  72. $this->nonces = array();
  73. }
  74. function &_getServerAssocs($server_url)
  75. {
  76. if (!array_key_exists($server_url, $this->server_assocs)) {
  77. $this->server_assocs[$server_url] =& new ServerAssocs();
  78. }
  79. return $this->server_assocs[$server_url];
  80. }
  81. function storeAssociation($server_url, $assoc)
  82. {
  83. $assocs =& $this->_getServerAssocs($server_url);
  84. $assocs->set($assoc);
  85. }
  86. function getAssociation($server_url, $handle=null)
  87. {
  88. $assocs =& $this->_getServerAssocs($server_url);
  89. if ($handle === null) {
  90. return $assocs->best();
  91. } else {
  92. return $assocs->get($handle);
  93. }
  94. }
  95. function removeAssociation($server_url, $handle)
  96. {
  97. $assocs =& $this->_getServerAssocs($server_url);
  98. return $assocs->remove($handle);
  99. }
  100. function useNonce($server_url, $timestamp, $salt)
  101. {
  102. global $Auth_OpenID_SKEW;
  103. if (abs($timestamp - time()) > $Auth_OpenID_SKEW) {
  104. return false;
  105. }
  106. $anonce = array($server_url, intval($timestamp), $salt);
  107. if (in_array($anonce, $this->nonces)) {
  108. return false;
  109. } else {
  110. array_push($this->nonces, $anonce);
  111. return true;
  112. }
  113. }
  114. function cleanupNonces()
  115. {
  116. global $Auth_OpenID_SKEW;
  117. $now = time();
  118. $expired = array();
  119. foreach ($this->nonces as $anonce) {
  120. if (abs($anonce[1] - $now) > $Auth_OpenID_SKEW) {
  121. // removing items while iterating over the set could
  122. // be bad.
  123. $expired[] = $anonce;
  124. }
  125. }
  126. foreach ($expired as $anonce) {
  127. unset($this->nonces[array_search($anonce, $this->nonces)]);
  128. }
  129. return count($expired);
  130. }
  131. function cleanupAssociations()
  132. {
  133. $remove_urls = array();
  134. $removed_assocs = 0;
  135. foreach ($this->server_assocs as $server_url => $assocs) {
  136. list($removed, $remaining) = $assocs->cleanup();
  137. $removed_assocs += $removed;
  138. if (!$remaining) {
  139. $remove_urls[] = $server_url;
  140. }
  141. }
  142. // Remove entries from server_assocs that had none remaining.
  143. foreach ($remove_urls as $server_url) {
  144. unset($this->server_assocs[$server_url]);
  145. }
  146. return $removed_assocs;
  147. }
  148. }
  149. ?>