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