Authentication/Store/Cookie.pm

filter: Statementstmt 100% 83 / 83 Branchbran 100% 16 / 16 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 100% 128 / 128 SCAR 24.8
CC
12
Cov
100%
initialize
3.0
_decode
3.0
_secret
3.0
CRAP 12.0
« Authentication/Driver/Filter/sha1.pm ↑ Summary Authentication/Driver/Generic.pm »
1 package CGI::Application::Plugin::Authentication::Store::Cookie;
2 11 $CGI::Application::Plugin::Authentication::Store::Cookie::VERSION = '0.21';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use base qw(CGI::Application::Plugin::Authentication::Store);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use CGI::Cookie ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # CONFIGURABLE OPTIONS
12 #
13 # - SECRET
14 #
15 # If this value is defined, it will be used to protect
16 # the Cookie values from tampering. To generate a good
17 # secret, run the following perl script and cut and paste
18 # the value it generates into this variable.
19 #
20 # perl -MDigest::MD5=md5_base64 -l -e 'print md5_base64($$,time(),rand(9999))'
21 #
22 11 our $SECRET = '';
23  
24  
25 =head1 NAME
26  
27 CGI::Application::Plugin::Authentication::Store::Cookie - Cookie based Store
28  
29 =head1 SYNOPSIS
30  
31 use base qw(CGI::Application);
32 use CGI::Application::Plugin::Session;
33 use CGI::Application::Plugin::Authentication;
34  
35 __PACKAGE__->authen->config(
36 STORE => ['Cookie', SECRET => "Shhh, don't tell anyone", NAME => 'CAPAUTH_DATA', EXPIRY => '+1y'],
37 );
38  
39 =head1 DESCRIPTION
40  
41 This module uses a cookie to store authentication information across multiple requests.
42 It works by creating a cookie that contains the information we would like to store (like
43 the name of the user that is currently authenticated), and then base64 encoding
44 the data. In order to ensure that the information is not manipulated by the end-user, we include
45 a CRC checksum that is generated along with our secret. Since the user does not know the value
46 of the secret, they will not be able to recreate the checksum if they change some of the values, so we
47 will be able to tell if the information in the cookie has been manipulated.
48  
49 =head1 THE SECRET
50  
51 =head2 Choosing a good secret
52  
53 An easy way to generate a relatively good secret is to run the following perl snippet:
54  
55 perl -MDigest::MD5=md5_base64 -l -e 'print md5_base64($$,time(),rand(9999))'
56  
57 Just use the resulting string as your secret.
58  
59 =head2 Configuring the secret
60  
61 There are three ways that you can provide a secret to the module:
62  
63 =over 4
64  
65 =item Hardcode the secret
66  
67 You can hardcode a secret right in the CGI::Application::Plugin::Authentication::Store::Cookie module,
68 so that you don't have to remember to provide one every time you use the module. Just open the
69 source in a text editor and look at the top of the file where it defines 'our $SECRET' and follow
70 the instruction listed there.
71  
72 =item Provide the SECRET option when using the module
73  
74 You can also just provide the secret as an option when using the module using the SECRET
75 parameter.
76  
77 __PACKAGE__->authen->config(
78 STORE => ['Cookie', SECRET => "Shhh, don't tell anyone"],
79 );
80  
81 =item Let the module choose a secret for you
82  
83 And lastly, if you forget to do either of these, the module will use the name of your application
84 as the secret, but that is not a very good value to use, so a warning will be spit out everytime
85 it uses this. This is the least desirable choice, and is only included as a last resort.
86  
87 =back
88  
89 =head1 DEPENDENCIES
90  
91 This module requires the following modules to be available.
92  
93 =over 4
94  
95 =item MIME::Base64
96  
97 =item Digest::SHA
98  
99 =item CGI::Cookie
100  
101 =back
102  
103 =head1 METHODS
104  
105 =head2 fetch
106  
107 This method accepts a list of parameters and fetches them from the cookie data.
108  
109 =cut
110  
111 sub fetch {
112 233 ▶ my $self = shift;
Subroutine fetch: called CC=1 SCAR=0.0
Pod documented
113 233 my @items = map { $self->{cookie}->{data}->{$_} } @_;
114 233 return @items[0..$#items];
115 }
116  
117 =head2 save
118  
119 This method accepts a hash of parameters and values and stores them in the cookie data.
120  
121 =cut
122  
123 sub save {
124 25 ▶ my $self = shift;
Subroutine save: called CC=2 SCAR=6.9
Pod documented
125 25 my %items = @_;
126 25 ▶ while (my ($param, $value) = each %items) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %items) 55 25 80
127 55 $self->{cookie}->{data}->{$param} = $value;
128 }
129 25 $self->_register_postrun_callback;
130 25 return 1;
131 }
132  
133 =head2 delete
134  
135 This method accepts a list of parameters and deletes them from the cookie data.
136  
137 =cut
138  
139 sub delete {
140 1 ▶ my $self = shift;
Subroutine delete: called CC=2 SCAR=6.9
Pod documented
141 1 foreach my $param (@_) {
142 4 delete $self->{cookie}->{data}->{$param};
143 }
144 1 $self->_register_postrun_callback;
145 1 return 1;
146 }
147  
148 =head2 initialize
149  
150 This method will check for an existing cookie, and decode the contents for later retrieval.
151  
152 =cut
153  
154 sub initialize {
155 51 ▶ my $self = shift;
Subroutine initialize: called CC=3 SCAR=11.0
Pod documented
156  
157 51 my @options = $self->options;
158 51 ▶ die "Invalid Store Configuration for the Cookie store - options section must contain a hash of values" if @options % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @options % 2 1 50 51
159 50 my %options = @options;
160 50 $self->{cookie}->{options} = \%options;
161  
162 50 my %cookies = CGI::Cookie->fetch;
163 50 ▶ if ($cookies{$self->cookie_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookies{$self->cookie_name}) 6 44 50
164 6 my $rawdata = $cookies{$self->cookie_name}->value;
165 6 $self->{cookie}->{data} = $self->_decode($rawdata);
166 }
167 # $self->_register_postrun_callback;
168  
169 50 return;
170 }
171  
172 =head2 cookie_name
173  
174 This method will return the name of the cookie
175  
176 =cut
177  
178 sub cookie_name {
179 81 ▶ my $self = shift;
Subroutine cookie_name: called CC=2 SCAR=6.9
Pod documented
180 81 ▶ return $self->{cookie}->{options}->{NAME} || 'CAPAUTH_DATA';
Condition100%2/2
$self->{'cookie'}{'options'}{'NAME'} || 'CAPAUTH_DATA'
l!l
6120
MC/DC100%1/1
$self->{'cookie'}{'options'}{'NAME'} || 'CAPAUTH_DATA'
$self->{'cookie'}{'options'}{'NAME'}
Truth table100%2/2
A
A= $self->{'cookie'}{'options'}{'NAME'}
Aresult
11
00
181 }
182  
183 ###
184 ### Helper methods
185 ###
186  
187 # _register_postrun_callback
188 #
189 # We only register the postrun callback once a change has been made to the data
190 # so that we don't unecesarily send out a cookie.
191 sub _register_postrun_callback {
192 26 ▶ my $self = shift;
Subroutine _register_postrun_callback: called CC=2 SCAR=6.9
193 26 ▶ return if $self->{cookie}->{postrun_registered}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cookie"}{"postrun_registered"}++ 1 25 26
194  
195 25 $self->authen->_cgiapp->add_callback('postrun', \&_postrun_callback);
196 25 return;
197 }
198  
199 # _postrun_callback
200 #
201 # This callback will add a cookie to the outgoing headers at the postrun stage
202 sub _postrun_callback {
203 25 ▶ my $self = shift;
Subroutine _postrun_callback: called CC=2 SCAR=6.9
204  
205 25 my $store = $self->authen->store;
206 25 my $rawdata = $store->_encode($store->{cookie}->{data});
207  
208 25 my %cookie_params = (
209 -name => $store->cookie_name,
210 -value => $rawdata,
211 );
212 25 ▶ $cookie_params{'-expires'} = $store->{cookie}->{options}->{EXPIRY} if $store->{cookie}->{options}->{EXPIRY};
Branch: 1 branch
BranchTrueFalseTotal
if $store->{"cookie"}{"options"}{"EXPIRY"} 22 3 25
213 25 my $cookie = new CGI::Cookie(%cookie_params);
214 25 $self->header_add(-cookie => [$cookie]);
215 25 return;
216 }
217  
218 # _decode
219 #
220 # Take a raw cookie value, and decode and verify the data
221 sub _decode {
222 18 ▶ my $self = shift;
Subroutine _decode: called CC=3 SCAR=11.0
223 18 my $rawdata = MIME::Base64::decode(shift);
224 18 ▶ return if not $rawdata;
Branch: 1 branch
BranchTrueFalseTotal
unless $rawdata 1 17 18
225  
226 17 my %hash = map { split /\=/, $_, 2 } split /\0/, $rawdata;
227  
228 17 my $checksum = delete $hash{c};
229 # verify checksum
230 17 ▶ if ($checksum eq Digest::SHA::sha1_base64(join("\0", $self->_secret, sort values %hash))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checksum eq &Digest::SHA::sha1_base64(join("\000", $self->_secret, sort(values %hash)))) { } 15 2 17
231 # Checksum verifies so the data is clean
232 15 return \%hash;
233 } else {
234 # The data could not be verified, so we trash it all
235 2 return;
236 }
237 }
238  
239 # _encode
240 #
241 # Take the data we want to store and encode the data into a cookie
242 sub _encode {
243 25 ▶ my $self = shift;
Subroutine _encode: called CC=1 SCAR=0.0
244 25 my $hash = shift;
245 25 my %hash = %$hash;
246  
247 25 my $checksum = Digest::SHA::sha1_base64(join("\0", $self->_secret, sort values %hash));
248 25 $hash{c} = $checksum;
249 25 my $rawdata = join("\0", map { join('=', $_, $hash{$_}) } keys %hash);
250 25 return MIME::Base64::encode($rawdata, "");
251 }
252  
253 # _secret
254 #
255 # A unique value for this application that is used to secure the Cookies
256 sub _secret {
257 42 ▶ my $self = shift;
Subroutine _secret: called CC=3 SCAR=11.0
258 42 ▶ my $secret = $self->{cookie}->{options}->{SECRET} || $SECRET;
Condition100%3/3
$self->{'cookie'}{'options'}{'SECRET'} || $CGI::Application::Plugin::Authentication::Store::Cookie::SECRET
l!l&&r!l&&!r
24117
MC/DC100%2/2
$self->{'cookie'}{'options'}{'SECRET'} || $CGI::Application::Plugin::Authentication::Store::Cookie::SECRET
$self->{'cookie'}{'options'}{'SECRET'} $CGI::Application::Plugin::Authentication::Store::Cookie::SECRET
Truth table100%3/3
A || B
A= $self->{'cookie'}{'options'}{'SECRET'}B= $CGI::Application::Plugin::Authentication::Store::Cookie::SECRET
ABresult
1X1
011
000
259 42 ▶ unless ($secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secret) 17 25 42
260 17 $secret = Digest::SHA::sha1_base64( ref $self->authen->_cgiapp );
261 17 warn "using default SECRET! Please provide a proper SECRET when using the Cookie store (See CGI::Application::Plugin::Authentication::Store::Cookie for details)";
262 }
263 42 return $secret;
264 }
265  
266 =head1 SEE ALSO
267  
268 L<CGI::Application::Plugin::Authentication::Store>, L<CGI::Application::Plugin::Authentication>, perl(1)
269  
270  
271 =head1 AUTHOR
272  
273 Cees Hek <ceeshek@gmail.com>
274  
275  
276 =head1 LICENCE AND COPYRIGHT
277  
278 Copyright (c) 2005, SiteSuite. All rights reserved.
279  
280 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
281  
282  
283 =head1 DISCLAIMER OF WARRANTY
284  
285 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
286  
287 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
288  
289 =cut
290  
291 11 1;
« Authentication/Driver/Filter/sha1.pm ↑ Summary Authentication/Driver/Generic.pm »