| 1 | use Net::OpenID::JanRain::Stores::SQLStore; | ||
| 2 | package Net::OpenID::JanRain::Stores::SQLiteStore; | ||
| 3 | use DBI; | ||
| 4 | |||
| 5 | our @ISA=qw(Net::OpenID::JanRain::Stores::SQLStore); | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | =head1 Net::OpenID::JanRain::Stores::SQLiteStore | ||
| 11 | |||
| 12 | This store uses the SQLite2 database to store data for the JanRain OpenID | ||
| 13 | Consumer and Server. | ||
| 14 | |||
| 15 | =head2 Usage | ||
| 16 | |||
| 17 | |||
| 18 | #Get a handle to your SQLite database: | ||
| 19 | my $dbh = DBI->connect("dbi:SQLite2:yourdb", $un, $pw); | ||
| 20 | |||
| 21 | #For the default table names, "settings", "associations", "nonces": | ||
| 22 | my $store = Net::OpenID::JanRain::Stores::SQLiteStore->new($dbh); | ||
| 23 | |||
| 24 | |||
| 25 | #If you desire to modify the table names: | ||
| 26 | my $store = Net::OpenID::JanRain::Stores::SQLiteStore->new($dbh, | ||
| 27 | {settings_table => "ajustes", | ||
| 28 | associations_table => "asociaciones", | ||
| 29 | nonces_table => "absurdos"}); | ||
| 30 | #You need not modify all the table names. | ||
| 31 | |||
| 32 | #If the database is new and these tables do not yet exist: | ||
| 33 | $store->createTables; | ||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | # SQL code template strings | ||
| 40 | my $sqlstrings = { | ||
| 41 | create_nonce_sql => ' | ||
| 42 | CREATE TABLE <nonce_table> | ||
| 43 | ( | ||
| 44 | nonce CHAR(8) UNIQUE PRIMARY KEY, | ||
| 45 | expires INTEGER | ||
| 46 | ); | ||
| 47 | ', | ||
| 48 | |||
| 49 | create_assoc_sql => ' | ||
| 50 | CREATE TABLE <assoc_table> | ||
| 51 | ( | ||
| 52 | server_url VARCHAR(2047), | ||
| 53 | handle VARCHAR(255), | ||
| 54 | secret BLOB(128), | ||
| 55 | issued INTEGER, | ||
| 56 | lifetime INTEGER, | ||
| 57 | assoc_type VARCHAR(64), | ||
| 58 | PRIMARY KEY (server_url, handle) | ||
| 59 | ); | ||
| 60 | ', | ||
| 61 | |||
| 62 | create_settings_sql => ' | ||
| 63 | CREATE TABLE <settings_table> | ||
| 64 | ( | ||
| 65 | setting VARCHAR(128) UNIQUE PRIMARY KEY, | ||
| 66 | value BLOB(20) | ||
| 67 | ); | ||
| 68 | ', | ||
| 69 | |||
| 70 | create_auth_sql => 'INSERT INTO <settings_table> VALUES ("auth_key", ?);', | ||
| 71 | get_auth_sql => 'SELECT value FROM <settings_table> WHERE setting = "auth_key";', | ||
| 72 | |||
| 73 | set_assoc_sql => 'INSERT OR REPLACE INTO <assoc_table> '. | ||
| 74 | 'VALUES (?, ?, ?, ?, ?, ?);', | ||
| 75 | get_assocs_sql => 'SELECT handle, secret, issued, lifetime, assoc_type '. | ||
| 76 | 'FROM <assoc_table> WHERE server_url = ?;', | ||
| 77 | get_assoc_sql => 'SELECT handle, secret, issued, lifetime, assoc_type '. | ||
| 78 | 'FROM <assoc_table> WHERE server_url = ? AND handle = ?;', | ||
| 79 | |||
| 80 | remove_assoc_sql => 'DELETE FROM <assoc_table> '. | ||
| 81 | 'WHERE server_url = ? AND handle = ?;', | ||
| 82 | |||
| 83 | add_nonce_sql => 'INSERT OR REPLACE INTO <nonce_table> VALUES (?, ?);', | ||
| 84 | get_nonce_sql => 'SELECT * FROM <nonce_table> WHERE nonce = ?;', | ||
| 85 | remove_nonce_sql => 'DELETE FROM <nonce_table> WHERE nonce = ?;', | ||
| 86 | }; | ||
| 87 | |||
| 88 | # SQLite store constructor | ||
| 89 | sub new { | ||
| 90 | my $caller = shift; | ||
| 91 | my ($dbh, $tablenames) = @_; | ||
| 92 | |||
| 93 | $dbh->{AutoCommit} = 1; | ||
| 94 | $dbh->{RaiseError} = 1; | ||
| 95 | $dbh->{sqlite_handle_binary_nulls} = 1; | ||
| 96 | |||
| 97 | my $class = ref($caller) || $caller; | ||
| 98 | |||
| 99 | defined($tablenames) or $tablenames = {}; | ||
| 100 | |||
| 101 | my $settings_table = ($tablenames->{settings_table} || 'settings'); | ||
| 102 | my $assoc_table = ($tablenames->{assoc_table} || 'associations'); | ||
| 103 | my $nonce_table = ($tablenames->{nonce_table} || 'nonces'); | ||
| 104 | |||
| 105 | my $self = { | ||
| 106 | settings_table_name => $settings_table, | ||
| 107 | assoc_table_name => $assoc_table, | ||
| 108 | nonce_table_name => $nonce_table, | ||
| 109 | max_nonce_age => 6 * 60 * 60, # six hours in seconds | ||
| 110 | sql_cache => {}, | ||
| 111 | dbh => $dbh, | ||
| 112 | AUTH_KEY_LEN => 20, | ||
| 113 | sqlstrings => $sqlstrings | ||
| 114 | }; | ||
| 115 | |||
| 116 | bless($self, $class); | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; |