Stores/FileStore.pm

filter: Statementstmt 55.3% 99 / 179 Branchbran 25.0% 14 / 56 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 66.6% 18 / 27 Podpod 0.0% 0 / 12 totaltotal 45.9% 132 / 287 CC 35 SCAR 25.7
CC
35
Cov
47%
clean
56.0
createAuthKey
30.0
readAuthKey
12.0
CRAP 13.1
« Util.pm ↑ Summary Consumer/LinkParser.pm »
1 package Net::OpenID::JanRain::Stores::FileStore;
2  
3 =head1 JanRain OpenID File Store
4  
5 This module maintains a directory structure that saves state for the
6 JanRain OpenID Library.
7  
8 =head2 Synopsis:
9  
10 C<< Net::OpenID::JanRain::Stores::FileStore->new("directory") >>
11  
12 =cut
13  
14 # vi:ts=4:sw=4
15  
16 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Net::OpenID::JanRain::CryptUtil qw( sha1 randomString );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 our @ISA = qw(
26 Net::OpenID::JanRain::Stores
27 );
28  
29 # Functions
30 50 ▶sub _safe64 {
Subroutine _safe64: called CC=1 SCAR=0.0
31 50 my ($s) = @_;
32 50 my $h64 = encode_base64(sha1($s));
33 50 $h64 =~ s/\+/_/g;
34 50 $h64 =~ s!/!.!g;
35 50 $h64 =~ s/=//g;
36 50 $h64 =~ s/\n//g;
37 50 return $h64;
38 }
39 ########################################################################
40 0 ▶sub _isFilenameSafe {
Subroutine _isFilenameSafe: not called CC=1 SCAR=6.9
41 0 my ($c) = @_;
42 return
43 0 }
44 ########################################################################
45 0 ▶sub _filenameEscape {
Subroutine _filenameEscape: not called CC=1 SCAR=6.9
46 0 my ($s) = @_;
47 }
48 ########################################################################
49 # Attempt to remove a file, returning whether the file existed at
50 # the time of the call.
51 1 ▶sub _removeIfPresent {
Subroutine _removeIfPresent: called CC=3 SCAR=15.1
52 1 my ($filename) = @_;
53 1 ▶ if ((unlink $filename) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink($filename) == 0) 0 1 1
54 0 ▶ die "Could not remove $filename. $!" if -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -e $filename 0 0 0
55 0 return 0;
56 }
57 1 return 1;
58 } # end removeIfPresent
59 ########################################################################
60 # _ensureDir
61 # Create dir_name as a directory if it does not exist. If it
62 # exists, make sure that it is, in fact, a directory.
63 4 ▶sub _ensureDir {
Subroutine _ensureDir: called CC=1 SCAR=0.0
64 4 my ($dir_name) = @_;
65 4 mkdir $dir_name, 0755
66 || -d $dir_name
67 || die "Unable to make directory $dir_name. $!";
68 4 return -d $dir_name;
69 } # end ensureDir
70 ########################################################################
71  
72 # Methods
73 ########################################################################
74 # new
75 # Call with the directory where the files should go.
76 # All files must reside on the same filesystem.
77 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
78 1 my $caller = shift;
79 1 my ($dir) = @_;
80 1 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
81 1 $dir = File::Spec->rel2abs($dir);
82 1 my $noncedir = File::Spec->catdir($dir, "nonces");
83 1 my $assocdir = File::Spec->catdir($dir, "associations");
84 1 my $tempdir = File::Spec->catdir($dir, "temp");
85 1 my $authkeyn = File::Spec->catfile($dir, "auth_key");
86 1 my $maxnonceage = 6 * 60 * 60;
87 1 my $AUTH_KEY_LEN = 20;
88 1 my $self = {nonce_dir => $noncedir,
89 assoc_dir => $assocdir,
90 temp_dir => $tempdir,
91 auth_key_name => $authkeyn,
92 max_nonce_age => $maxnonceage,
93 AUTH_KEY_LEN => $AUTH_KEY_LEN};
94
95 1 _ensureDir($dir);
96 1 _ensureDir($noncedir);
97 1 _ensureDir($assocdir);
98 1 _ensureDir($tempdir);
99  
100 1 bless($self, $class);
101 } # end new
102 ########################################################################
103 # isDumb
104 # true if we are a dumb store, which we aren't.
105 0 ▶sub isDumb {
Subroutine isDumb: not called CC=1 SCAR=6.9
Pod undocumented
106 0 my $self = shift;
107 0 return 0;
108 }
109 ########################################################################
110 # readAuthKey
111 # Read the auth key from the auth key file. Will return None
112 # if there is currently no key.
113 0 ▶sub readAuthKey {
Subroutine readAuthKey: not called CC=3 SCAR=24.8
Pod undocumented
114 0 my $self = shift;
115 0 my $key;
116 0 ▶ open AKF, "< $self->{auth_key_name}" or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open AKF, qq[< $self->{"auth_key_name"}] or return undef 0 0 0
117 # Read one more byte than necessary to detect corruption
118 0 my $keylen = (read AKF, $key, $self->{AUTH_KEY_LEN}+1);
119 0 ▶ return undef if $keylen == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $keylen == 0 0 0 0
120 0 close AKF;
121 0 return $key;
122 } # end readAuthKey
123 ########################################################################
124 # createAuthKey
125 # Generate a new random auth key and safely store it in the
126 # location specified by self.auth_key_name.
127 0 ▶sub createAuthKey {
Subroutine createAuthKey: not called CC=5 SCAR=34.0
Pod undocumented
128 0 my $self = shift;
129 0 my $auth_key = randomString($self->{AUTH_KEY_LEN});
130  
131 0 my ($fh, $tmpfn) = tempfile(DIR => $self->{temp_dir});
132 0 ▶ die "Could not open a temporary file" unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
133 0 print $fh $auth_key;
134 0 close $fh;
135  
136 0 ▶ unless(link($tmpfn, $self->{auth_key_name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (link $tmpfn, $self->{"auth_key_name"}) 0 0 0
137 0 ▶ unless(rename ($tmpfn, $self->{auth_key_name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $tmpfn, $self->{"auth_key_name"}) 0 0 0
138 0 $auth_key = $self->readAuthKey();
139 0 ▶ unless ($auth_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth_key) 0 0 0
140 0 die 'Failed to create or read Auth Key'
141 }
142 }
143 }
144 0 $self->_removeIfPresent($tmpfn);
145 0 return $auth_key;
146  
147 } # end createAuthKey
148 ########################################################################
149 # getAuthKey
150 # Retrieve the auth key from the file specified by
151 # self.auth_key_name, creating it if it does not exist.
152 0 ▶sub getAuthKey {
Subroutine getAuthKey: not called CC=3 SCAR=24.8
Pod undocumented
153 0 my $self = shift;
154 0 my $auth_key = $self->readAuthKey();
155 0 ▶ $auth_key = $self->createAuthKey() unless $auth_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth_key 0 0 0
156 0 ▶ if (length($auth_key) != $self->{AUTH_KEY_LEN}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $auth_key != $self->{"AUTH_KEY_LEN"}) 0 0 0
157 0 die "Got invalid auth key from $self->{auth_key_name}. Expected ".
158 "$self->{AUTH_KEY_LEN} byte string. Got: $auth_key";
159 }
160 0 return $auth_key;
161 } # end getAuthKey
162 ########################################################################
163 # getAssociationFilename
164 # Create a unique filename for a given server url and
165 # handle. This implementation does not assume anything about the
166 # format of the handle. The filename that is returned will
167 # contain the domain name from the server URL for ease of human
168 # inspection of the data directory.
169 25 ▶sub getAssociationFilename {
Subroutine getAssociationFilename: called CC=4 SCAR=14.5
Pod undocumented
170 25 my $self = shift;
171 25 my ($server_url, $handle) = @_;
172 25 ▶ defined($server_url) || die "getAssociationFilename called without server url";
Branch: 1 branch
BranchTrueFalseTotal
defined $server_url or die "getAssociationFilename called without server url" 25 0 25
173 25 ▶ unless($server_url =~ m!(.+)://([.\w]+)/?!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($server_url =~ m[(.+)://([.\w]+)/?]) 0 25 25
174 0 die "Bad server URL: $server_url";
175 }
176 25 my $proto = $1;
177 25 my $domain = $2;
178
179 25 my $url_hash = _safe64($server_url);
180
181 25 my $handle_hash;
182  
183 25 ▶ if ($handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle) { } 25 0 25
184 25 $handle_hash = _safe64($handle);
185 }
186 else {
187 0 $handle_hash = '';
188 }
189  
190 25 my $filename = "${proto}-${domain}-${url_hash}-${handle_hash}";
191  
192 25 return File::Spec->catfile($self->{assoc_dir}, $filename);
193 } # end getAssociationFilename
194 ########################################################################
195 # storeAssociation
196 # Create a unique filename for a given server url and
197 # handle. This implementation does not assume anything about the
198 # format of the handle. The filename that is returned will
199 # contain the domain name from the server URL for ease of human
200 # inspection of the data directory.
201 7 ▶sub storeAssociation {
Subroutine storeAssociation: called CC=4 SCAR=18.4
Pod undocumented
202 7 my $self = shift;
203 7 my ($server_url, $association) = @_;
204
205 7 my $association_s = $association->serialize();
206 7 my $filename=$self->getAssociationFilename($server_url, $association->{handle});
207 7 my ($fh, $tmpfn) = tempfile(DIR => $self->{temp_dir});
208  
209 7 ▶ unless (print $fh $association_s) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $fh $association_s) 0 7 7
210 0 warn "Unable to write association to $tmpfn";
211 0 close $fh;
212 0 return;
213 }
214 # os.fsync(tmp_file.fileno())
215 7 close $fh;
216  
217 7 ▶ unless (rename $tmpfn, $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $tmpfn, $filename) 0 7 7
218 0 unlink $filename;
219 0 ▶ unless (rename $tmpfn, $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $tmpfn, $filename) 0 0 0
220 0 warn "Unable to rename $tmpfn to $filename. $!";
221 0 unlink $tmpfn;
222 }
223 }
224 } # end storeAssociation
225 ########################################################################
226 # getAssociation
227 # Retrieve an association. If no handle is specified, return
228 # the association with the latest expiration.
229 # If no matching association exists, returns undef
230 17 ▶sub getAssociation {
Subroutine getAssociation: called CC=5 SCAR=20.9
Pod undocumented
231 2 ▶ use Net::OpenID::JanRain::Association;
Subroutine BEGIN: called CC=1 SCAR=0.0
232 17 my $self = shift;
233 17 my ($server_url, $handle) = @_;
234
235 17 ▶ defined($handle) or $handle = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $handle or $handle = "" 17 0 17
236
237 17 my $filename = $self->getAssociationFilename($server_url, $handle);
238
239 17 ▶ if ($handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle) { } 17 0 17
240 17 return $self->_getAssociation($filename);
241 }
242 else {
243 0 my @associations = ();
244 # The filename with an empty handle is a prefix of all association
245 # filenames for a given server URL.
246 0 my $file_match = "$filename*";
247 0 my $file;
248 0 ▶ for $file (glob($file_match)) {
Subroutine BEGIN: called CC=1 SCAR=0.2
249 0 my $assoc = $self->_getAssociation($file);
250 0 ▶ if ($assoc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assoc) 0 0 0
251 0 push @associations, $assoc;
252 }
253 }
254 0 @associations = sort {$a->{issued} <=> $b->{issued}} @associations;
255 0 return pop @associations; # undef if array is empty
256 }
257 } # end getAssociation
258 ########################################################################
259 # _getAssociation
260 # Read an association file and return an association object.
261 # undef if we have no such association.
262 17 ▶sub _getAssociation {
Subroutine _getAssociation: called CC=4 SCAR=14.7
263 17 my $self = shift;
264 17 my ($filename) = @_;
265  
266 17 ▶ open FILE, "< $filename" or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "< $filename" or return undef 6 11 17
267
268 6 my $assoc_s;
269 6 ▶ unless (read FILE, $assoc_s, 1024) { #more bytes than needed
Branch: 1 branch
BranchTrueFalseTotal
unless (read FILE, $assoc_s, 1024) 0 6 6
270 0 warn "Unable to read $filename";
271 0 close FILE;
272 0 return undef;
273 }
274 6 close FILE;
275 6 my $association = Net::OpenID::JanRain::Association->deserialize($assoc_s);
276 #If we find a bunk association, remove it.
277 6 ▶ _removeIfPresent($filename) unless $association;
Branch: 1 branch
BranchTrueFalseTotal
unless $association 0 6 6
278 6 return $association;
279 }
280 ########################################################################
281 # removeAssociation
282 # Remove an association if it exists. Do nothing if it does not.
283 5 ▶sub removeAssociation {
Subroutine removeAssociation: called CC=2 SCAR=6.9
Pod undocumented
284 5 my $self = shift;
285 5 my ($server_url, $handle) = @_;
286 5 my $assoc = $self->getAssociation($server_url, $handle);
287 5 ▶ if ($assoc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assoc) 1 4 5
288 1 return _removeIfPresent($self->getAssociationFilename($server_url, $handle));
289 }
290 4 return 0;
291 } # end removeAssociation
292 ########################################################################
293 0 ▶sub storeNonce {
Subroutine storeNonce: not called CC=2 SCAR=17.9
Pod undocumented
294 0 my $self = shift;
295 0 my ($nonce) = @_;
296 0 my $fn = File::Spec->catfile($self->{nonce_dir}, $nonce);
297 0 ▶ open FILE, "> $fn" or die "Could not open nonce file $fn - $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "> $fn" or die "Could not open nonce file $fn - $!\n" 0 0 0
298 0 close FILE;
299 } # end storeNonce
300 ########################################################################
301 0 ▶sub useNonce {
Subroutine useNonce: not called CC=3 SCAR=24.8
Pod undocumented
302 0 my $self = shift;
303 0 my ($nonce) = @_;
304 0 my $fn = File::Spec->catfile($self->{nonce_dir}, $nonce);
305 0 my @stats = stat $fn;
306 0 ▶ return undef unless @stats;
Branch: 1 branch
BranchTrueFalseTotal
unless @stats 0 0 0
307 0 my $mtime = $stats[10];
308 0 ▶ unlink $fn || return undef;
Condition0%0/2
$fn || (return undef)
l!l
00
MC/DC0%0/1
$fn || (return undef)
$fn
Truth table0%0/2
A
A= $fn
Aresult
11
00
309 0 return (($mtime - time) < $self->{max_nonce_age});
310 } # end useNonce
311 ########################################################################
312 0 ▶sub clean {
Subroutine clean: not called CC=7 SCAR=40.3
Pod undocumented
313 0 my $self = shift;
314 0 my $now = time; # now is the time
315
316 # Check all nonces for expiration
317 0 my $fn;
318 0 for $fn (glob(File::Spec->catfile($self->{nonce_dir}, "*"))) {
319 0 my @stats = stat $fn;
320 0 ▶ if (@stats) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stats) 0 0 0
321 # tenth stat is modification time
322 0 ▶ if (($now - $stats[10]) > $self->{max_nonce_age} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $stats[10] > $self->{"max_nonce_age"}) 0 0 0
323 0 _removeIfPresent($fn);
324 }
325 }
326 }
327  
328 # Check all associations for corruption and expiration
329 0 for $fn (glob(File::Spec->catfile($self->assoc_dir,"*"))) {
330 0 my $assoc = _getAssociation($fn); #cleans up corrupted files.
331 0 ▶ if($assoc && $assoc->getExpiresIn() == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assoc and $assoc->getExpiresIn == 0) 0 0 0
Condition0%0/3
$assoc and $assoc->getExpiresIn == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$assoc and $assoc->getExpiresIn == 0
$assoc $assoc->getExpiresIn == 0
Truth table0%0/3
A and B
A= $assocB= $assoc->getExpiresIn == 0
ABresult
0X0
100
111
332 0 _removeIfPresent($fn);
333 }
334 }
335 } # end clean
336 ########################################################################
337 2 1;
« Util.pm ↑ Summary Consumer/LinkParser.pm »