| 1 | package Net::OpenID::JanRain::Stores::SQLStore; | ||
| 2 | 1 | ▶ | use Net::OpenID::JanRain::Stores; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | use Net::OpenID::JanRain::CryptUtil qw( randomString ); | ||
| 5 | our @ISA=qw(Net::OpenID::JanRain::Stores); | ||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | |||
| 9 | =head1 Net::OpenID::JanRain::Stores::SQLStore | ||
| 10 | |||
| 11 | This module contains the base class for the SQL stores. It cannot be | ||
| 12 | instantiated itself, but its included subclasses can. The library includes | ||
| 13 | three subclasses, MySQLStore, PostGreSQLStore and SQLiteStore. (link these) | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | sub blobDecode { | ||
| 18 | my $self = shift; | ||
| 19 | my ($blob) = @_; | ||
| 20 | return $blob; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub blobEncode { | ||
| 24 | my $self = shift; | ||
| 25 | my ($blob) = @_; | ||
| 26 | return $blob; | ||
| 27 | } | ||
| 28 | |||
| 29 | # statement cache | ||
| 30 | # I use this instead of prepare_cached since it saves on regexp ops | ||
| 31 | sub _getSQL { | ||
| 32 | my $self = shift; | ||
| 33 | my ($sqlname) = @_; | ||
| 34 | |||
| 35 | my $cache = $self->{sql_cache}; | ||
| 36 | |||
| 37 | return $cache->{$sqlname} if defined($cache->{$sqlname}); | ||
| 38 | |||
| 39 | my $sql = $self->{sqlstrings}->{$sqlname}; | ||
| 40 | $sql =~ s/<assoc_table>/$self->{assoc_table_name}/sg; | ||
| 41 | $sql =~ s/<nonce_table>/$self->{nonce_table_name}/sg; | ||
| 42 | $sql =~ s/<settings_table>/$self->{settings_table_name}/sg; | ||
| 43 | |||
| 44 | my $sth = $self->{dbh}->prepare($sql); | ||
| 45 | $cache->{$sqlname} = $sth; | ||
| 46 | return $sth; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _execSQL { | ||
| 50 | my $self = shift; | ||
| 51 | my $sqlname = shift; | ||
| 52 | my $sth = $self->_getSQL($sqlname); | ||
| 53 | my $rv = $sth->execute(@_); | ||
| 54 | return ($sth, $rv); | ||
| 55 | } | ||
| 56 | |||
| 57 | =head2 Interface | ||
| 58 | |||
| 59 | This class uses the interface of Net::OpenID::JanRain::Stores and | ||
| 60 | implements one additional method. | ||
| 61 | |||
| 62 | =head3 createTables | ||
| 63 | |||
| 64 | The one new external method for SQLStores. It creates | ||
| 65 | the database tables necessary for the store to work. It should not | ||
| 66 | be called if the tables exist already. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub createTables { | ||
| 71 | my $self = shift; | ||
| 72 | |||
| 73 | my $dbh = $self->{dbh}; | ||
| 74 | eval { | ||
| 75 | $dbh->begin_work; | ||
| 76 | $self->_execSQL('create_nonce_sql'); | ||
| 77 | $self->_execSQL('create_assoc_sql'); | ||
| 78 | $self->_execSQL('create_settings_sql'); | ||
| 79 | $dbh->commit; | ||
| 80 | }; | ||
| 81 | if ($@) { | ||
| 82 | warn "Database trouble: $@"; | ||
| 83 | eval{ $dbh->rollback }; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | # The following are the store methods | ||
| 88 | sub getAuthKey { | ||
| 89 | my $self = shift; | ||
| 90 | my $dbh = $self->{dbh}; | ||
| 91 | my $auth_key; | ||
| 92 | eval { | ||
| 93 | $dbh->begin_work; | ||
| 94 | my ($sth, $foo) = $self->_execSQL('get_auth_sql'); | ||
| 95 | my $data = $sth->fetchrow_arrayref; | ||
| 96 | if($data) { | ||
| 97 | $auth_key = $self->blobDecode($data->[0]); | ||
| 98 | $sth->finish; | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | $auth_key = randomString($self->{AUTH_KEY_LEN}); | ||
| 102 | $self->_execSQL('create_auth_sql', $auth_key); | ||
| 103 | } | ||
| 104 | $dbh->commit; | ||
| 105 | }; | ||
| 106 | if ($@) { | ||
| 107 | warn "Database trouble: $@"; | ||
| 108 | return undef; | ||
| 109 | } | ||
| 110 | return $auth_key; | ||
| 111 | } | ||
| 112 | |||
| 113 | #write assoc to db | ||
| 114 | sub storeAssociation { | ||
| 115 | my $self = shift; | ||
| 116 | my ($server_url, $assoc) = @_; | ||
| 117 | |||
| 118 | my $dbh = $self->{dbh}; | ||
| 119 | eval { | ||
| 120 | $dbh->begin_work; | ||
| 121 | $self->_execSQL('set_assoc_sql', | ||
| 122 | $server_url, | ||
| 123 | $assoc->{handle}, | ||
| 124 | $self->blobEncode($assoc->{secret}), | ||
| 125 | $assoc->{issued}, | ||
| 126 | $assoc->{lifetime}, | ||
| 127 | $assoc->{assoc_type}); | ||
| 128 | $dbh->commit; | ||
| 129 | }; | ||
| 130 | if ($@) { | ||
| 131 | warn "Database trouble: $@"; | ||
| 132 | eval {$dbh->rollback}; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | #read assoc from db. return undef if no exist | ||
| 137 | sub getAssociation { | ||
| 138 | use Net::OpenID::JanRain::Association; | ||
| 139 | my $self = shift; | ||
| 140 | my ($server_url, $handle) = @_; | ||
| 141 | my $dbh = $self->{dbh}; | ||
| 142 | my @associations = (); | ||
| 143 | eval { | ||
| 144 | $dbh->begin_work; | ||
| 145 | my ($sth, $junk); | ||
| 146 | if (defined($handle)) { | ||
| 147 | ($sth, $junk) = $self->_execSQL('get_assoc_sql', $server_url, $handle); | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | ($sth, $junk) = $self->_execSQL('get_assocs_sql', $server_url); | ||
| 151 | } | ||
| 152 | #get the stuff | ||
| 153 | while( my @row = $sth->fetchrow_array) { | ||
| 154 | my $assoc = Net::OpenID::JanRain::Association->new(@row); | ||
| 155 | if ($assoc->expiresIn == 0) { | ||
| 156 | $self->_execSQL('remove_assoc_sql', | ||
| 157 | $server_url, | ||
| 158 | $assoc->{handle}); | ||
| 159 | } | ||
| 160 | else { | ||
| 161 | $assoc->{secret} = $self->blobDecode($assoc->{secret}); | ||
| 162 | push @associations, $assoc; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | $dbh->commit; | ||
| 166 | }; | ||
| 167 | if ($@) { #hmm | ||
| 168 | warn "Database trouble: $@"; | ||
| 169 | eval {$dbh->rollback}; | ||
| 170 | return undef; | ||
| 171 | } | ||
| 172 | if (@associations) { | ||
| 173 | @associations = sort {$a->{issued} <=> $b->{issued}} @associations if $#associations > 0; | ||
| 174 | return pop @associations; | ||
| 175 | } | ||
| 176 | else { | ||
| 177 | return undef; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | # kill! | ||
| 182 | sub removeAssociation { | ||
| 183 | my $self = shift; | ||
| 184 | my ($server_url, $handle) = @_; | ||
| 185 | |||
| 186 | my $dbh = $self->{dbh}; | ||
| 187 | my ($sth, $count); | ||
| 188 | eval { | ||
| 189 | $dbh->begin_work; | ||
| 190 | ($sth, $count) = $self->_execSQL('remove_assoc_sql', $server_url, $handle); | ||
| 191 | $dbh->commit; | ||
| 192 | }; | ||
| 193 | if ($@) { | ||
| 194 | warn "Database trouble: $@"; | ||
| 195 | eval {$dbh->rollback}; | ||
| 196 | } | ||
| 197 | return ($count > 0); | ||
| 198 | } | ||
| 199 | |||
| 200 | # add nonce to db. ignore if present | ||
| 201 | sub storeNonce { | ||
| 202 | my $self = shift; | ||
| 203 | my ($nonce) = @_; | ||
| 204 | |||
| 205 | my $dbh = $self->{dbh}; | ||
| 206 | eval { | ||
| 207 | $dbh->begin_work; | ||
| 208 | $self->_execSQL('add_nonce_sql', $nonce, time); | ||
| 209 | $dbh->commit; | ||
| 210 | }; | ||
| 211 | if ($@) { | ||
| 212 | warn "Database trouble: $@"; | ||
| 213 | eval {$dbh->rollback}; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | # return true if nonce is present, false otherwise | ||
| 218 | sub useNonce { | ||
| 219 | my $self = shift; | ||
| 220 | my ($nonce) = @_; | ||
| 221 | my $dbh = $self->{dbh}; | ||
| 222 | my $present = 0; | ||
| 223 | eval { | ||
| 224 | $dbh->begin_work; | ||
| 225 | my ($sth, $foo) = $self->_execSQL('get_nonce_sql', $nonce); | ||
| 226 | my $row = $sth->fetchrow_arrayref; | ||
| 227 | if (defined($row)) { | ||
| 228 | my ($crap, $timestamp) = @$row; | ||
| 229 | my $nonce_age = time - $timestamp; | ||
| 230 | $present = ($nonce_age < $self->{max_nonce_age}); | ||
| 231 | $self->_execSQL('remove_nonce_sql', $nonce); | ||
| 232 | } | ||
| 233 | $dbh->commit; | ||
| 234 | }; | ||
| 235 | if ($@) { | ||
| 236 | warn "Database trouble: $@"; | ||
| 237 | eval{$dbh->rollback}; | ||
| 238 | } | ||
| 239 | return $present; | ||
| 240 | } | ||
| 241 | |||
| 242 | # this one's easy. | ||
| 243 | sub isDumb { | ||
| 244 | return undef; | ||
| 245 | } | ||
| 246 | |||
| 247 | 1; | ||
| 248 | |||
| 249 | |||