Stores/PostgreSQLStore.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 132 CC 7 SCAR 21.3
CC
7
Cov
0%
storeAssociation
12.0
getAuthKey
12.0
storeNonce
12.0
CRAP 8.4
1 package Net::OpenID::JanRain::Stores::PostgreSQLStore;
2 use DBI;
3 use DBD::Pg;
4 use Net::OpenID::JanRain::Stores::SQLStore;
5 use Net::OpenID::JanRain::CryptUtil qw( randomString );
6 our @ISA=qw(Net::OpenID::JanRain::Stores::SQLStore);
7  
8 # SQL code template strings
9 my $sqlstrings = {
10 create_nonce_sql => '
11 CREATE TABLE <nonce_table>
12 (
13 nonce CHAR(8) UNIQUE PRIMARY KEY,
14 expires INTEGER
15 );
16 ',
17  
18 create_assoc_sql => '
19 CREATE TABLE <assoc_table>
20 (
21 server_url VARCHAR(2047),
22 handle VARCHAR(255),
23 secret BYTEA,
24 issued INTEGER,
25 lifetime INTEGER,
26 assoc_type VARCHAR(64),
27 PRIMARY KEY (server_url, handle),
28 CONSTRAINT secret_length_constraint CHECK (LENGTH(secret) <= 128)
29 );
30 ',
31  
32 create_settings_sql => '
33 CREATE TABLE <settings_table>
34 (
35 setting VARCHAR(128) UNIQUE PRIMARY KEY,
36 value BYTEA,
37 CONSTRAINT value_length_constraint CHECK (LENGTH(value) <= 20)
38 );
39 ',
40  
41 create_auth_sql => "INSERT INTO <settings_table> VALUES ('auth_key', ?);",
42 get_auth_sql => "SELECT value FROM <settings_table> WHERE setting = 'auth_key';",
43  
44 new_assoc_sql => 'INSERT INTO <assoc_table> '.
45 'VALUES (?, ?, ?, ?, ?, ?);',
46 update_assoc_sql => 'UPDATE <assoc_table> SET '.
47 'secret = (?), issued = ?, '.
48 'lifetime = ?, assoc_type = ? '.
49 'WHERE server_url = ? AND handle = ?;',
50 get_assocs_sql => 'SELECT handle, secret, issued, lifetime, assoc_type '.
51 'FROM <assoc_table> WHERE server_url = ?;',
52 get_assoc_sql => 'SELECT handle, secret, issued, lifetime, assoc_type '.
53 'FROM <assoc_table> WHERE server_url = ? AND handle = ?;',
54  
55 remove_assoc_sql => 'DELETE FROM <assoc_table> '.
56 'WHERE server_url = ? AND handle = ?;',
57  
58 update_nonce_sql => 'UPDATE <nonce_table> SET expires = ? WHERE nonce = ?;',
59 new_nonce_sql => 'INSERT INTO <nonce_table> VALUES (?, ?);',
60 get_nonce_sql => 'SELECT * FROM <nonce_table> WHERE nonce = ?;',
61 remove_nonce_sql => 'DELETE FROM <nonce_table> WHERE nonce = ?;',
62 };
63  
64 =head1 Net::OpenID::JanRain::Stores::PostgreSQLStore
65  
66 This module contains the PostGreSQL OpenID Store.
67  
68 =head2 Usage
69  
70 #Get a handle to your PostgreSQL database:
71 my $dbh = DBI->connect("dbi:Pg:yourdb", $un, $pw);
72  
73 #For the default table names, "settings", "associations", "nonces":
74 my $store = Net::OpenID::JanRain::Stores::PostgreSQLStore->new($dbh);
75  
76  
77 #If you desire to modify the table names:
78 my $store = Net::OpenID::JanRain::Stores::PostgreSQLStore->new($dbh,
79 {settings_table => "ajustes",
80 associations_table => "asociaciones",
81 nonces_table => "absurdos"});
82 #You need not modify all the table names.
83  
84 #If the database is new and these tables do not yet exist:
85 $store->createTables;
86  
87  
88 =cut
89  
90  
91 # Postgres constructor
92 sub new {
93 my $caller = shift;
94 my ($dbh, $tablenames) = @_;
95  
96 $dbh->{AutoCommit} = 1;
97 $dbh->{RaiseError} = 1;
98 $dbh->{PrintWarn} = 0;
99  
100 my $class = ref($caller) || $caller;
101  
102 defined($tablenames) or $tablenames = {};
103  
104 my $settings_table = ($tablenames->{settings_table} || 'settings');
105 my $assoc_table = ($tablenames->{assoc_table} || 'associations');
106 my $nonce_table = ($tablenames->{nonce_table} || 'nonces');
107  
108 my $self = {
109 settings_table_name => $settings_table,
110 assoc_table_name => $assoc_table,
111 nonce_table_name => $nonce_table,
112 max_nonce_age => 6 * 60 * 60, # six hours in seconds
113 sql_cache => {},
114 dbh => $dbh,
115 AUTH_KEY_LEN => 20,
116 sqlstrings => $sqlstrings
117 };
118  
119 bless($self, $class);
120 }
121  
122 # Postgres has no REPLACE INTO, and is picky about binary data.
123 sub storeAssociation {
124 my $self = shift;
125 my ($server_url, $assoc) = @_;
126  
127 my $dbh = $self->{dbh};
128 eval {
129 $dbh->begin_work;
130 # first check if there is such an association already
131 # unfortunately select does not give us reliable row number info
132 my ($sth, $junk) = $self->_execSQL('get_assoc_sql',
133 $server_url,
134 $assoc->{handle});
135 my $rows = $sth->fetchall_arrayref;
136 if(@$rows) { # update existing assoc
137 # We need to give some special treatment here
138 $sth = $self->_getSQL('update_assoc_sql');
139 $sth->bind_param(1, $assoc->{secret},
140 { pg_type => DBD::Pg::PG_BYTEA });
141 $sth->bind_param(2, $assoc->{issued});
142 $sth->bind_param(3, $assoc->{lifetime});
143 $sth->bind_param(4, $assoc->{assoc_type});
144 $sth->bind_param(5, $server_url);
145 $sth->bind_param(6, $assoc->{handle});
146 $rv = $sth->execute;
147 }
148 else { # Create a new assoc
149 $sth = $self->_getSQL('new_assoc_sql');
150 $sth->bind_param(1, $server_url);
151 $sth->bind_param(2, $assoc->{handle});
152 $sth->bind_param(3, $assoc->{secret},
153 { pg_type => DBD::Pg::PG_BYTEA });
154 $sth->bind_param(4, $assoc->{issued});
155 $sth->bind_param(5, $assoc->{lifetime});
156 $sth->bind_param(6, $assoc->{assoc_type});
157 $rv = $sth->execute;
158 }
159 $dbh->commit;
160 };
161 if ($@) {
162 warn "Database trouble: $@";
163 eval {$dbh->rollback};
164 }
165 }
166  
167 sub getAuthKey {
168 my $self = shift;
169 my $dbh = $self->{dbh};
170 my $auth_key;
171 eval {
172 $dbh->begin_work;
173 my ($sth, $foo) = $self->_execSQL('get_auth_sql');
174 my $data = $sth->fetchrow_arrayref;
175 if($data) {
176 $auth_key = $self->blobDecode($data->[0]);
177 $sth->finish;
178 }
179 else {
180 $auth_key = randomString($self->{AUTH_KEY_LEN});
181 my $sth = $self->_getSQL('create_auth_sql');
182 $sth->bind_param(1, $auth_key, { pg_type => DBD::Pg::PG_BYTEA });
183 $sth->execute;
184 }
185 $dbh->commit;
186 };
187 if ($@) {
188 warn "Database trouble: $@";
189 return undef;
190 }
191 return $auth_key;
192 }
193  
194  
195 sub storeNonce {
196 my $self = shift;
197 my ($nonce) = @_;
198  
199 my $dbh = $self->{dbh};
200 eval {
201 $dbh->begin_work;
202 my ($sth, $junk) = $self->_execSQL('get_nonce_sql', $nonce);
203 my $rows = $sth->fetchall_arrayref;
204 if(@$rows) { # update existing assoc
205 $self->_execSQL('update_nonce_sql', time, $nonce);
206 }
207 else {
208 $self->_execSQL('new_nonce_sql', $nonce, time);
209 }
210 $dbh->commit;
211 };
212 if ($@) {
213 warn "Database trouble: $@";
214 eval {$dbh->rollback};
215 }
216 }
217  
218  
219  
220  
221 1;