| 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
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | die "Could not remove $filename. $!" if -e $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
ref $caller || $caller
ref $caller
$caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 133 | 0 | print $fh $auth_key; | |||||||||||||||||||||||||||
| 134 | 0 | close $fh; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | unless(link($tmpfn, $self->{auth_key_name})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | unless(rename ($tmpfn, $self->{auth_key_name})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | $auth_key = $self->readAuthKey(); | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | unless ($auth_key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if (length($auth_key) != $self->{AUTH_KEY_LEN}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 173 | 25 | ▶ | unless($server_url =~ m!(.+)://([.\w]+)/?!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 218 | 0 | unlink $filename; | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | unless (rename $tmpfn, $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 17 | my $filename = $self->getAssociationFilename($server_url, $handle); | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 17 | ▶ | if ($handle) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 6 | my $assoc_s; | |||||||||||||||||||||||||||
| 269 | 6 | ▶ | unless (read FILE, $assoc_s, 1024) { #more bytes than needed | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 307 | 0 | my $mtime = $stats[10]; | |||||||||||||||||||||||||||
| 308 | 0 | ▶ | unlink $fn || return undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$fn || (return undef)
MC/DC0%0/1
$fn || (return undef)
$fn
Truth table0%0/2
A
A= $fn
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 321 | # tenth stat is modification time | ||||||||||||||||||||||||||||
| 322 | 0 | ▶ | if (($now - $stats[10]) > $self->{max_nonce_age} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$assoc and $assoc->getExpiresIn == 0
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
| |||||||||||||||||||||||||||||
| 332 | 0 | _removeIfPresent($fn); | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | } # end clean | ||||||||||||||||||||||||||||
| 336 | ######################################################################## | ||||||||||||||||||||||||||||
| 337 | 2 | 1; | |||||||||||||||||||||||||||