PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Auth/OpenID/MemStore.php

http://github.com/openid/php-openid
PHP | 176 lines | 127 code | 28 blank | 21 comment | 14 complexity | 907e742ab6e5357b8d98c1881d930a29 MD5 | raw file
Possible License(s): Apache-2.0
  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 __construct()
  9. {
  10. $this->assocs = [];
  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 = [];
  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 [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 __construct()
  70. {
  71. $this->server_assocs = [];
  72. $this->nonces = [];
  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 = [$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 = [];
  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 = [];
  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. }