MxScreen/Session/Medium/Browser.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 142 CC 3 SCAR 9.4
CC
3
Cov
0%
session_id
6.0
retrieve
6.0
make
2.0
CRAP 2.6
1 # -*- Mode: perl -*-
2 #
3 # $Id: Browser.pm,v 0.1 2001/04/22 17:57:03 ram Exp $
4 #
5 # Copyright (c) 1998-2001, Raphael Manfredi
6 # Copyright (c) 2000-2001, Christophe Dehaudt
7 #
8 # You may redistribute only under the terms of the Artistic License,
9 # as specified in the README file that comes with the distribution.
10 #
11 # HISTORY
12 # $Log: Browser.pm,v $
13 # Revision 0.1 2001/04/22 17:57:03 ram
14 # Baseline for first Alpha release.
15 #
16 # $EndLog$
17 #
18  
19 use strict;
20  
21 package CGI::MxScreen::Session::Medium::Browser;
22  
23 #
24 # Session storage is within the browser, session ID is not used.
25 #
26 # In the generated HTML, the session is identified by two parameters:
27 #
28 # _mxscreen_context encrypted compressed context
29 # _mxscreen_md5 MD5 checksum
30 #
31 # The encryption key is defined once and for all in the creation routine,
32 # i.e. it is a "secret" key for this application. The aim is more to avoid
33 # user-tampering of the context than to really protect the content.
34 #
35 # The secret key should be changed on a regular basis, depending on the
36 # amount of protection you want, and the nature of the information propagated
37 #
38  
39 require CGI::MxScreen::Session::Medium;
40 use vars qw(@ISA);
41 @ISA = qw(CGI::MxScreen::Session::Medium);
42  
43 use Carp::Datum;
44 use Getargs::Long;
45 use Log::Agent;
46 use Fcntl;
47 require LockFile::Simple;
48 require CGI;
49  
50 use constant MX_CONTEXT => "_mxscreen_context";
51 use constant MX_MD5 => "_mxscreen_md5";
52 use constant NO_SESSION_ID => 0;
53 use constant CRYPT_ALGO => "Blowfish";
54  
55 #
56 # ->make
57 #
58 # Creation routine.
59 #
60 # Arguments:
61 # -key encryption key
62 #
63 sub make {
64 DFEATURE my $f_;
65 my $self = bless {}, shift;
66  
67 my ($key) = xgetargs(@_,
68 -key => 's',
69 );
70  
71 $self->{key} = $key;
72  
73 return DVAL $self;
74 }
75  
76 #
77 # Attribute access
78 #
79  
80 sub key { $_[0]->{key} }
81  
82 #
83 # ->session_id -- defined
84 #
85 # Retrieve session ID from the CGI environment.
86 # This is unused for sessions stored within the browser.
87 #
88 sub session_id {
89 DFEATURE my $f_;
90 my $self = shift;
91  
92 return DVAL NO_SESSION_ID if length CGI::param(MX_MD5);
93 return DVAL undef; # no session yet
94 }
95  
96 #
97 # ->allocate_id -- redefined
98 #
99 # Always return NO_SESSION_ID.
100 #
101 sub allocate_id {
102 DFEATURE my $f_;
103 my $self = shift;
104 return DVAL NO_SESSION_ID;
105 }
106  
107 #
108 # ->is_available -- defined
109 #
110 # Look whether ID is free to use as a session ID.
111 # If it is free, atomically reserve it.
112 #
113 # Always returns false, since we don't use session IDs.
114 #
115 sub is_available {
116 DFEATURE my $f_;
117 my $self = shift;
118 my ($id) = @_;
119  
120 return DVAL 0; # No session ID can be used
121 }
122  
123 #
124 # ->retrieve -- defined
125 #
126 # Retrieve context by session ID.
127 #
128 sub retrieve {
129 DFEATURE my $f_;
130 my $self = shift;
131 my ($id) = @_;
132  
133 DREQUIRE $id == NO_SESSION_ID, "session ID unused";
134 DREQUIRE defined $self->serializer, "already called set_serializer()";
135  
136 my $md5 = CGI::param(MX_MD5);
137 DASSERT length $md5, "session MD5 checksum exists";
138 CGI::delete(MX_MD5);
139  
140 require MIME::Base64;
141 require Crypt::CBC;
142 require Digest::MD5;
143  
144 my $decoded = MIME::Base64::decode(CGI::param(MX_CONTEXT));
145 my $cipher = new Crypt::CBC($self->key, CRYPT_ALGO);
146 my $decrypted = $cipher->decrypt($decoded);
147 CGI::delete(MX_CONTEXT);
148  
149 #
150 # Before attempting to de-serialize, check the MD5 certificate.
151 # Deserialization would fail anyway if the context was "corrupted".
152 #
153  
154 my $digest = Digest::MD5::md5_base64($decrypted);
155 if ($digest ne $md5) {
156 logerr "invalid MD5 certificate";
157 return DVAL undef;
158 }
159  
160 #
161 # Deserialize context.
162 #
163  
164 return DVAL $self->serializer->deserialize($decrypted);
165 }
166  
167 #
168 # ->store -- defined
169 #
170 # Store context within browser.
171 #
172 # Returns hash of (parameter => value) to be generated in the HTML
173 # to identify the session.
174 #
175 sub store {
176 DFEATURE my $f_;
177 my $self = shift;
178 my ($id, $context) = @_;
179  
180 DREQUIRE $id == NO_SESSION_ID, "session ID unused";
181 DREQUIRE defined $self->serializer, "already called set_serializer()";
182  
183 my $frozen = $self->serializer->serialize($context);
184  
185 require MIME::Base64;
186 require Crypt::CBC;
187 require Digest::MD5;
188  
189 #
190 # Compute MD5 checksum and encrypt context.
191 #
192 # XXX add logging support, when we have a log object:
193 # XXX
194 # XXX $self->log->debug("context size: $ls -> $le ($rate%)");
195 # XXX
196 # XXX With: my ($ls, $lc, $le) = (length $serialized, length $compressed,
197 # XXX length $encoded);
198 #
199  
200 my $md5 = Digest::MD5::md5_base64($frozen);
201 my $cipher = new Crypt::CBC($self->key, CRYPT_ALGO);
202 my $crypted = $cipher->encrypt($frozen);
203 my $encoded = MIME::Base64::encode($crypted);
204  
205 #
206 # Return the hidden parameters to generate in the HTML output.
207 #
208  
209 my $ret = {
210 &MX_MD5 => $md5,
211 &MX_CONTEXT => $encoded,
212 };
213  
214 return DVAL $ret;
215 }
216  
217 1;
218  
219 =head1 NAME
220  
221 CGI::MxScreen::Session::Medium::Browser - Browser session medium
222  
223 =head1 SYNOPSIS
224  
225 # Not meant to be used directly
226  
227 =head1 DESCRIPTION
228  
229 This saves the session within the browser, and therefore does not require
230 any storage on the server side, compared to other session media.
231  
232 The context is serialized within a hidden parameter, along with an MD5
233 checksum. The whole thing is encrypted with C<Crypt::CBC(Blowfish)> to
234 prevent accidental user peeking and/or tampering.
235  
236 The creation routine takes the following mandatory argument:
237  
238 =over 4
239  
240 =item C<-key> => I<string>
241  
242 The encryption key to protect the context.
243  
244 =back
245  
246 You can configure this session medium in the configuration file
247 by saying:
248  
249 $mx_medium = ["+Browser", -key => "your own protection key"];
250  
251 You can further say:
252  
253 $mx_serializer = ["+Storable", -compress => 1];
254  
255 to store sessions in compressed forms, which will reduce network traffic
256 at the cost of non-negligeable CPU overhead on the server. Your call.
257  
258 See L<CGI::MxScreen::Config> for details.
259  
260 =head1 AUTHOR
261  
262 Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
263  
264 =head1 SEE ALSO
265  
266 CGI::MxScreen::Session::Medium::File(3),
267 CGI::MxScreen::Session::Medium::Raw_File(3).
268  
269 =cut
270