summaryrefslogtreecommitdiff
blob: 9349702ef274c9a6a0478d0c33d1fada055aad9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php

/**
 * SQL-backed OpenID stores for use with PEAR::MDB2.
 *
 * PHP versions 4 and 5
 *
 * LICENSE: See the COPYING file included in this distribution.
 *
 * @package OpenID
 * @author JanRain, Inc. <openid@janrain.com>
 * @copyright 2005 Janrain, Inc.
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 */

require_once 'MDB2.php';

/**
 * @access private
 */
require_once 'Auth/OpenID/Interface.php';

/**
 * @access private
 */
require_once 'Auth/OpenID.php';

/**
 * @access private
 */
require_once 'Auth/OpenID/Nonce.php';

/**
 * This store uses a PEAR::MDB2 connection to store persistence
 * information.
 *
 * The table names used are determined by the class variables
 * associations_table_name and nonces_table_name.  To change the name
 * of the tables used, pass new table names into the constructor.
 *
 * To create the tables with the proper schema, see the createTables
 * method.
 *
 * @package OpenID
 */
class Auth_OpenID_MDB2Store extends Auth_OpenID_OpenIDStore {
    /**
     * This creates a new MDB2Store instance.  It requires an
     * established database connection be given to it, and it allows
     * overriding the default table names.
     *
     * @param connection $connection This must be an established
     * connection to a database of the correct type for the SQLStore
     * subclass you're using.  This must be a PEAR::MDB2 connection
     * handle.
     *
     * @param associations_table: This is an optional parameter to
     * specify the name of the table used for storing associations.
     * The default value is 'oid_associations'.
     *
     * @param nonces_table: This is an optional parameter to specify
     * the name of the table used for storing nonces.  The default
     * value is 'oid_nonces'.
     */
    function __construct($connection,
                                  $associations_table = null,
                                  $nonces_table = null)
    {
        $this->associations_table_name = "oid_associations";
        $this->nonces_table_name = "oid_nonces";

        // Check the connection object type to be sure it's a PEAR
        // database connection.
        if (!is_object($connection) ||
            !is_subclass_of($connection, 'mdb2_driver_common')) {
            trigger_error("Auth_OpenID_MDB2Store expected PEAR connection " .
                          "object (got ".get_class($connection).")",
                          E_USER_ERROR);
            return;
        }

        $this->connection = $connection;

        // Be sure to set the fetch mode so the results are keyed on
        // column name instead of column index.
        $this->connection->setFetchMode(MDB2_FETCHMODE_ASSOC);
        
        if (@PEAR::isError($this->connection->loadModule('Extended'))) {
            trigger_error("Unable to load MDB2_Extended module", E_USER_ERROR);
            return;
        }

        if ($associations_table) {
            $this->associations_table_name = $associations_table;
        }

        if ($nonces_table) {
            $this->nonces_table_name = $nonces_table;
        }

        $this->max_nonce_age = 6 * 60 * 60;
    }

    function tableExists($table_name)
    {
        return !@PEAR::isError($this->connection->query(
                                  sprintf("SELECT * FROM %s LIMIT 0",
                                          $table_name)));
    }

    function createTables()
    {
        $n = $this->create_nonce_table();
        $a = $this->create_assoc_table();

        if (!$n || !$a) {
            return false;
        }
        return true;
    }

    function create_nonce_table()
    {
        if (!$this->tableExists($this->nonces_table_name)) {
            switch ($this->connection->phptype) {
                case "mysql":
                case "mysqli":
                    // Custom SQL for MySQL to use InnoDB and variable-
                    // length keys
                    $r = $this->connection->exec(
                        sprintf("CREATE TABLE %s (\n".
                                "  server_url VARCHAR(2047) NOT NULL DEFAULT '',\n".
                                "  timestamp INTEGER NOT NULL,\n".
                                "  salt CHAR(40) NOT NULL,\n".
                                "  UNIQUE (server_url(255), timestamp, salt)\n".
                                ") TYPE=InnoDB",
                                $this->nonces_table_name));
                    if (@PEAR::isError($r)) {
                        return false;
                    }
                    break;
                default:
                    if (@PEAR::isError(
                        $this->connection->loadModule('Manager'))) {
                        return false;
                    }
                    $fields = array(
                        "server_url" => array(
                            "type" => "text",
                            "length" => 2047,
                            "notnull" => true
                        ),
                        "timestamp" => array(
                            "type" => "integer",
                            "notnull" => true
                        ),
                        "salt" => array(
                            "type" => "text",
                            "length" => 40,
                            "fixed" => true,
                            "notnull" => true
                        )
                    );
                    $constraint = array(
                        "unique" => 1,
                        "fields" => array(
                            "server_url" => true,
                            "timestamp" => true,
                            "salt" => true
                        )
                    );
                    
                    $r = $this->connection->createTable($this->nonces_table_name,
                                                        $fields);
                    if (@PEAR::isError($r)) {
                        return false;
                    }
                    
                    $r = $this->connection->createConstraint(
                        $this->nonces_table_name,
                        $this->nonces_table_name . "_constraint",
                        $constraint);
                    if (@PEAR::isError($r)) {
                        return false;
                    }
                    break;
            }
        }
        return true;
    }

    function create_assoc_table()
    {
        if (!$this->tableExists($this->associations_table_name)) {
            switch ($this->connection->phptype) {
                case "mysql":
                case "mysqli":
                    // Custom SQL for MySQL to use InnoDB and variable-
                    // length keys
                    $r = $this->connection->exec(
                        sprintf("CREATE TABLE %s(\n".
                                "  server_url VARCHAR(2047) NOT NULL DEFAULT '',\n".
                                "  handle VARCHAR(255) NOT NULL,\n".
                                "  secret BLOB NOT NULL,\n".
                                "  issued INTEGER NOT NULL,\n".
                                "  lifetime INTEGER NOT NULL,\n".
                                "  assoc_type VARCHAR(64) NOT NULL,\n".
                                "  PRIMARY KEY (server_url(255), handle)\n".
                                ") TYPE=InnoDB",
                            $this->associations_table_name));
                    if (@PEAR::isError($r)) {
                        return false;
                    }
                    break;
                default:
                    if (@PEAR::isError(
                        $this->connection->loadModule('Manager'))) {
                        return false;
                    }
                    $fields = array(
                        "server_url" => array(
                            "type" => "text",
                            "length" => 2047,
                            "notnull" => true
                        ),
                        "handle" => array(
                            "type" => "text",
                            "length" => 255,
                            "notnull" => true
                        ),
                        "secret" => array(
                            "type" => "blob",
                            "length" => "255",
                            "notnull" => true
                        ),
                        "issued" => array(
                            "type" => "integer",
                            "notnull" => true
                        ),
                        "lifetime" => array(
                            "type" => "integer",
                            "notnull" => true
                        ),
                        "assoc_type" => array(
                            "type" => "text",
                            "length" => 64,
                            "notnull" => true
                        )
                    );
                    $options = array(
                        "primary" => array(
                            "server_url" => true,
                            "handle" => true
                        )
                    );
                    
                    $r = $this->connection->createTable(
                        $this->associations_table_name,
                        $fields,
                        $options);
                    if (@PEAR::isError($r)) {
                        return false;
                    }
                    break;
            }
        }
        return true;
    }

    function storeAssociation($server_url, $association)
    {
        $fields = array(
            "server_url" => array(
                "value" => $server_url,
                "key" => true
            ),
            "handle" => array(
                "value" => $association->handle,
                "key" => true
            ),
            "secret" => array(
                "value" => $association->secret,
                "type" => "blob"
            ),
            "issued" => array(
                "value" => $association->issued
            ),
            "lifetime" => array(
                "value" => $association->lifetime
            ),
            "assoc_type" => array(
                "value" => $association->assoc_type
            )
        );
        
        return !@PEAR::isError($this->connection->replace(
                                  $this->associations_table_name,
                                  $fields));
    }

    function cleanupNonces()
    {
        global $Auth_OpenID_SKEW;
        $v = time() - $Auth_OpenID_SKEW;

        return $this->connection->exec(
            sprintf("DELETE FROM %s WHERE timestamp < %d",
                    $this->nonces_table_name, $v));
    }

    function cleanupAssociations()
    {
        return $this->connection->exec(
            sprintf("DELETE FROM %s WHERE issued + lifetime < %d",
                    $this->associations_table_name, time()));
    }

    function getAssociation($server_url, $handle = null)
    {
        $sql = "";
        $params = null;
        $types = array(
                       "text",
                       "blob",
                       "integer",
                       "integer",
                       "text"
                       );
        if ($handle !== null) {
            $sql = sprintf("SELECT handle, secret, issued, lifetime, assoc_type " .
                           "FROM %s WHERE server_url = ? AND handle = ?",
                           $this->associations_table_name);
            $params = array($server_url, $handle);
        } else {
            $sql = sprintf("SELECT handle, secret, issued, lifetime, assoc_type " .
                           "FROM %s WHERE server_url = ? ORDER BY issued DESC",
                           $this->associations_table_name);
            $params = array($server_url);
        }
        
        $assoc = $this->connection->getRow($sql, $types, $params);

        if (!$assoc || @PEAR::isError($assoc)) {
            return null;
        } else {
            $association = new Auth_OpenID_Association($assoc['handle'],
                                                       stream_get_contents(
                                                           $assoc['secret']),
                                                       $assoc['issued'],
                                                       $assoc['lifetime'],
                                                       $assoc['assoc_type']);
            fclose($assoc['secret']);
            return $association;
        }
    }

    function removeAssociation($server_url, $handle)
    {
        $r = $this->connection->execParam(
            sprintf("DELETE FROM %s WHERE server_url = ? AND handle = ?",
                    $this->associations_table_name),
            array($server_url, $handle));
        
        if (@PEAR::isError($r) || $r == 0) {
            return false;
        }
        return true;
    }

    function useNonce($server_url, $timestamp, $salt)
    {
        global $Auth_OpenID_SKEW;

        if (abs($timestamp - time()) > $Auth_OpenID_SKEW ) {
            return false;
        }
        
        $fields = array(
                        "timestamp" => $timestamp,
                        "salt" => $salt
                        );
        
        if (!empty($server_url)) {
            $fields["server_url"] = $server_url;
        }
        
        $r = $this->connection->autoExecute(
            $this->nonces_table_name,
            $fields,
            MDB2_AUTOQUERY_INSERT);
        
        if (@PEAR::isError($r)) {
            return false;
        }
        return true;
    }

    /**
     * Resets the store by removing all records from the store's
     * tables.
     */
    function reset()
    {
        $this->connection->query(sprintf("DELETE FROM %s",
                                         $this->associations_table_name));

        $this->connection->query(sprintf("DELETE FROM %s",
                                         $this->nonces_table_name));
    }

}

?>