blib/lib/CGI/Application/Plugin/Authentication/Store/Cookie.pm

filter: Statementstmt 96.3% 78 / 81 Branchbran 71.4% 10 / 14 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 88.1% 112 / 127 CC 12 SCAR 7.7
CC
12
Cov
89%
_secret
3.8
_decode
3.1
initialize
3.0
CRAP 2.2
« blib/lib/CGI/Application/Plugin/Authentication/Driver/HTPasswd.pm ↑ Summary blib/lib/CGI/Application/Plugin/Authentication/Driver/Filter/crypt.pm »
1 package CGI::Application::Plugin::Authentication::Store::Cookie;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use base qw(CGI::Application::Plugin::Authentication::Store);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Digest::SHA1 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶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 9 our $SECRET = '';
23  
24  
25 =head1 NAME
26  
27 CGI::Application::Plugin::Authentication::Store::Cookie - Cookie based Store
28  
29  
30 =head1 SYNOPSIS
31  
32 use base qw(CGI::Application);
33 use CGI::Application::Plugin::Session;
34 use CGI::Application::Plugin::Authentication;
35  
36 __PACKAGE__->authen->config(
37 STORE => ['Cookie', SECRET => "Shhh, don't tell anyone", NAME => 'CAPAUTH_DATA', EXPIRY => '+1y'],
38 );
39  
40 =head1 DESCRIPTION
41  
42 This module uses a cookie to store authentication information across multiple requests.
43 It works by creating a cookie that contains the information we would like to store (like
44 the name of the user that is currently authenticated), and then base64 encoding
45 the data. In order to ensure that the information is not manipulated by the end-user, we include
46 a CRC checksum that is generated along with our secret. Since the user does not know the value
47 of the secret, they will not be able to recreate the checksum if they change some of the values, so we
48 will be able to tell if the information in the cookie has been manipulated.
49  
50 =head1 THE SECRET
51  
52 =head2 Choosing a good secret
53  
54 An easy way to generate a relatively good secret is to run the following perl snippet:
55  
56 perl -MDigest::MD5=md5_base64 -l -e 'print md5_base64($$,time(),rand(9999))'
57  
58 Just use the resulting string as your secret.
59  
60 =head2 Configuring the secret
61  
62 There are three ways that you can provide a secret to the module:
63  
64 =over 4
65  
66 =item Hardcode the secret
67  
68 You can hardcode a secret right in the CGI::Application::Plugin::Authentication::Store::Cookie module,
69 so that you don't have to remember to provide one every time you use the module. Just open the
70 source in a text editor and look at the top of the file where it defines 'our $SECRET' and follow
71 the instruction listed there.
72  
73 =item Provide the SECRET option when using the module
74  
75 You can also just provide the secret as an option when using the module using the SECRET
76 parameter.
77  
78 __PACKAGE__->authen->config(
79 STORE => ['Cookie', SECRET => "Shhh, don't tell anyone"],
80 );
81  
82 =item Let the module choose a secret for you
83  
84 And lastly, if you forget to do either of these, the module will use the name of your application
85 as the secret, but that is not a very good value to use, so a warning will be spit out everytime
86 it uses this. This is the least desirable choice, and is only included as a last resort.
87  
88 =back
89  
90 =head1 DEPENDENCIES
91  
92 This module requires the following modules to be available.
93  
94 =over 4
95  
96 =item MIME::Base64
97  
98 =item Digest::SHA1
99  
100 =item CGI::Cookie
101  
102 =back
103  
104 =head1 METHODS
105  
106 =head2 fetch
107  
108 This method accepts a list of parameters and fetches them from the cookie data.
109  
110 =cut
111  
112 sub fetch {
113 180 ▶ my $self = shift;
Subroutine fetch: called CC=1 SCAR=0.0
Pod documented
114 180 my @items = map { $self->{cookie}->{data}->{$_} } @_;
115 180 return @items[0..$#items];
116 }
117  
118 =head2 save
119  
120 This method accepts a hash of parameters and values and stores them in the cookie data.
121  
122 =cut
123  
124 sub save {
125 18 ▶ my $self = shift;
Subroutine save: called CC=2 SCAR=6.9
Pod documented
126 18 my %items = @_;
127 18 ▶ while (my ($param, $value) = each %items) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %items) 42 18 60
128 42 $self->{cookie}->{data}->{$param} = $value;
129 }
130 18 $self->_register_postrun_callback;
131 18 return 1;
132 }
133  
134 =head2 delete
135  
136 This method accepts a list of parameters and deletes them from the cookie data.
137  
138 =cut
139  
140 sub delete {
141 1 ▶ my $self = shift;
Subroutine delete: called CC=2 SCAR=6.9
Pod documented
142 1 foreach my $param (@_) {
143 4 delete $self->{cookie}->{data}->{$param};
144 }
145 1 $self->_register_postrun_callback;
146 1 return 1;
147 }
148  
149 =head2 initialize
150  
151 This method will check for an existing cookie, and decode the contents for later retrieval.
152  
153 =cut
154  
155 sub initialize {
156 38 ▶ my $self = shift;
Subroutine initialize: called CC=3 SCAR=11.0
Pod documented
157  
158 38 my @options = $self->options;
159 38 ▶ 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 0 38 38
160 38 my %options = @options;
161 38 $self->{cookie}->{options} = \%options;
162  
163 38 my %cookies = CGI::Cookie->fetch;
164 38 ▶ if ($cookies{$self->cookie_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookies{$self->cookie_name}) 2 36 38
165 2 my $rawdata = $cookies{$self->cookie_name}->value;
166 2 $self->{cookie}->{data} = $self->_decode($rawdata);
167 }
168 # $self->_register_postrun_callback;
169  
170 38 return;
171 }
172  
173 =head2 cookie_name
174  
175 This method will return the name of the cookie
176  
177 =cut
178  
179 sub cookie_name {
180 58 ▶ my $self = shift;
Subroutine cookie_name: called CC=2 SCAR=7.2
Pod documented
181 58 ▶ return $self->{cookie}->{options}->{NAME} || 'CAPAUTH_DATA';
Condition50%1/2
$self->{'cookie'}{'options'}{'NAME'} || 'CAPAUTH_DATA'
l!l
580
MC/DC0%0/1
$self->{'cookie'}{'options'}{'NAME'} || 'CAPAUTH_DATA'
$self->{'cookie'}{'options'}{'NAME'}
Truth table50%1/2
A
A= $self->{'cookie'}{'options'}{'NAME'}
Aresult
11
00
182 }
183  
184 ###
185 ### Helper methods
186 ###
187  
188 # _register_postrun_callback
189 #
190 # We only register the postrun callback once a change has been made to the data
191 # so that we don't unecesarily send out a cookie.
192 sub _register_postrun_callback {
193 19 ▶ my $self = shift;
Subroutine _register_postrun_callback: called CC=2 SCAR=6.9
194 19 ▶ return if $self->{cookie}->{postrun_registered}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cookie"}{"postrun_registered"}++ 1 18 19
195  
196 18 $self->authen->_cgiapp->add_callback('postrun', \&_postrun_callback);
197 18 return;
198 }
199  
200 # _postrun_callback
201 #
202 # This callback will add a cookie to the outgoing headers at the postrun stage
203 sub _postrun_callback {
204 18 ▶ my $self = shift;
Subroutine _postrun_callback: called CC=2 SCAR=7.0
205  
206 18 my $store = $self->authen->store;
207 18 my $rawdata = $store->_encode($store->{cookie}->{data});
208  
209 18 my %cookie_params = (
210 -name => $store->cookie_name,
211 -value => $rawdata,
212 );
213 18 ▶ $cookie_params{'-expires'} = $store->{cookie}->{options}->{EXPIRY} if $store->{cookie}->{options}->{EXPIRY};
Branch: 1 branch
BranchTrueFalseTotal
if $store->{"cookie"}{"options"}{"EXPIRY"} 18 0 18
214 18 my $cookie = new CGI::Cookie(%cookie_params);
215 18 $self->header_add(-cookie => [$cookie]);
216 18 return;
217 }
218  
219 # _decode
220 #
221 # Take a raw cookie value, and decode and verify the data
222 sub _decode {
223 5 ▶ my $self = shift;
Subroutine _decode: called CC=3 SCAR=11.4
224 5 ▶ my $rawdata = MIME::Base64::decode(shift) || return;
Condition50%1/2
&MIME::Base64::decode(shift()) || (return)
l!l
50
MC/DC0%0/1
&MIME::Base64::decode(shift()) || (return)
&MIME::Base64::decode(shift())
Truth table50%1/2
A
A= &MIME::Base64::decode(shift())
Aresult
11
00
225  
226 5 my %hash = map { split /\=/, $_, 2 } split /\0/, $rawdata;
227  
228 5 my $checksum = delete $hash{c};
229 # verify checksum
230 5 ▶ if ($checksum eq Digest::SHA1::sha1_base64(join("\0", $self->_secret, sort values %hash))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checksum eq Digest::SHA1::sha1_base64(join("\000", $self->_secret, sort(values %hash)))) { } 5 0 5
231 # Checksum verifies so the data is clean
232 5 return \%hash;
233 } else {
234 # The data could not be verified, so we trash it all
235 0 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 18 ▶ my $self = shift;
Subroutine _encode: called CC=1 SCAR=0.0
244 18 my $hash = shift;
245 18 my %hash = %$hash;
246  
247 18 my $checksum = Digest::SHA1::sha1_base64(join("\0", $self->_secret, sort values %hash));
248 18 $hash{c} = $checksum;
249 18 my $rawdata = join("\0", map { join('=', $_, $hash{$_}) } keys %hash);
250 18 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 23 ▶ my $self = shift;
Subroutine _secret: called CC=3 SCAR=13.5
258 23 ▶ my $secret = $self->{cookie}->{options}->{SECRET} || $SECRET;
Condition33%1/3
$self->{'cookie'}{'options'}{'SECRET'} || $CGI::Application::Plugin::Authentication::Store::Cookie::SECRET
l!l&&r!l&&!r
2300
MC/DC0%0/2
$self->{'cookie'}{'options'}{'SECRET'} || $CGI::Application::Plugin::Authentication::Store::Cookie::SECRET
$self->{'cookie'}{'options'}{'SECRET'} $CGI::Application::Plugin::Authentication::Store::Cookie::SECRET
Truth table33%1/3
A || B
A= $self->{'cookie'}{'options'}{'SECRET'}B= $CGI::Application::Plugin::Authentication::Store::Cookie::SECRET
ABresult
1X1
011
000
259 23 ▶ unless ($secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secret) 0 23 23
260 0 $secret = Digest::SHA1::sha1_base64( ref $self->authen->_cgiapp );
261 0 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 23 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 9 1;
« blib/lib/CGI/Application/Plugin/Authentication/Driver/HTPasswd.pm ↑ Summary blib/lib/CGI/Application/Plugin/Authentication/Driver/Filter/crypt.pm »