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            [
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                [
86                    $secret,
87                    $issued,
88                    $lifetime,
89                    $assoc_type,
90                    $server_url,
91                    $handle,
92                ]);
93        } else {
94            // Insert a new record because this association wasn't
95            // found.
96            $this->connection->query($this->sql['set_assoc']['insert_assoc'],
97                [
98                    $server_url,
99                    $handle,
100                    $secret,
101                    $issued,
102                    $lifetime,
103                    $assoc_type,
104                ]
105            );
106        }
107    }
108
109    /**
110     * @access private
111     */
112    function blobEncode($blob)
113    {
114        return $this->_octify($blob);
115    }
116
117    /**
118     * @access private
119     */
120    function blobDecode($blob)
121    {
122        return $this->_unoctify($blob);
123    }
124}
125
126