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