PostgreSQLStore.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * A PostgreSQL store.
  4. *
  5. * @package OpenID
  6. */
  7. /**
  8. * Require the base class file.
  9. */
  10. require_once "Auth/OpenID/SQLStore.php";
  11. /**
  12. * An SQL store that uses PostgreSQL as its backend.
  13. *
  14. * @package OpenID
  15. */
  16. class Auth_OpenID_PostgreSQLStore extends Auth_OpenID_SQLStore {
  17. /**
  18. * @access private
  19. */
  20. function setSQL()
  21. {
  22. $this->sql['nonce_table'] =
  23. "CREATE TABLE %s (server_url VARCHAR(2047) NOT NULL, ".
  24. "timestamp INTEGER NOT NULL, ".
  25. "salt CHAR(40) NOT NULL, ".
  26. "UNIQUE (server_url, timestamp, salt))";
  27. $this->sql['assoc_table'] =
  28. "CREATE TABLE %s (server_url VARCHAR(2047) NOT NULL, ".
  29. "handle VARCHAR(255) NOT NULL, ".
  30. "secret BYTEA NOT NULL, ".
  31. "issued INTEGER NOT NULL, ".
  32. "lifetime INTEGER NOT NULL, ".
  33. "assoc_type VARCHAR(64) NOT NULL, ".
  34. "PRIMARY KEY (server_url, handle), ".
  35. "CONSTRAINT secret_length_constraint CHECK ".
  36. "(LENGTH(secret) <= 128))";
  37. $this->sql['set_assoc'] =
  38. array(
  39. 'insert_assoc' => "INSERT INTO %s (server_url, handle, ".
  40. "secret, issued, lifetime, assoc_type) VALUES ".
  41. "(?, ?, '!', ?, ?, ?)",
  42. 'update_assoc' => "UPDATE %s SET secret = '!', issued = ?, ".
  43. "lifetime = ?, assoc_type = ? WHERE server_url = ? AND ".
  44. "handle = ?"
  45. );
  46. $this->sql['get_assocs'] =
  47. "SELECT handle, secret, issued, lifetime, assoc_type FROM %s ".
  48. "WHERE server_url = ?";
  49. $this->sql['get_assoc'] =
  50. "SELECT handle, secret, issued, lifetime, assoc_type FROM %s ".
  51. "WHERE server_url = ? AND handle = ?";
  52. $this->sql['remove_assoc'] =
  53. "DELETE FROM %s WHERE server_url = ? AND handle = ?";
  54. $this->sql['add_nonce'] =
  55. "INSERT INTO %s (server_url, timestamp, salt) VALUES ".
  56. "(?, ?, ?)"
  57. ;
  58. $this->sql['clean_nonce'] =
  59. "DELETE FROM %s WHERE timestamp < ?";
  60. $this->sql['clean_assoc'] =
  61. "DELETE FROM %s WHERE issued + lifetime < ?";
  62. }
  63. /**
  64. * @access private
  65. */
  66. function _set_assoc($server_url, $handle, $secret, $issued, $lifetime,
  67. $assoc_type)
  68. {
  69. $result = $this->_get_assoc($server_url, $handle);
  70. if ($result) {
  71. // Update the table since this associations already exists.
  72. $this->connection->query($this->sql['set_assoc']['update_assoc'],
  73. array($secret, $issued, $lifetime,
  74. $assoc_type, $server_url, $handle));
  75. } else {
  76. // Insert a new record because this association wasn't
  77. // found.
  78. $this->connection->query($this->sql['set_assoc']['insert_assoc'],
  79. array($server_url, $handle, $secret,
  80. $issued, $lifetime, $assoc_type));
  81. }
  82. }
  83. /**
  84. * @access private
  85. */
  86. function blobEncode($blob)
  87. {
  88. return $this->_octify($blob);
  89. }
  90. /**
  91. * @access private
  92. */
  93. function blobDecode($blob)
  94. {
  95. return $this->_unoctify($blob);
  96. }
  97. }