| 1 | package Jifty::Web::Session::ClientSide; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Jifty::Web::Session::ClientSide - Session handler for client-side sessions | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | In your F<etc/config.yml>: | ||
| 10 | |||
| 11 | framework: | ||
| 12 | Web: | ||
| 13 | SessionClass: Jifty::Web::Session::ClientSide | ||
| 14 | SessionSecret: secret_passphrase | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use base 'Jifty::Web::Session'; | ||
| 21 | use Jifty::Model::Session(); | ||
| 22 | use Storable (); | ||
| 23 | use Compress::Zlib (); | ||
| 24 | use Crypt::CBC (); | ||
| 25 | use Crypt::Rijndael (); | ||
| 26 | use CGI::Cookie; | ||
| 27 | use CGI::Cookie::Splitter (); | ||
| 28 | use MIME::Base64; | ||
| 29 | |||
| 30 | my $session_key; | ||
| 31 | my $splitter = CGI::Cookie::Splitter->new; | ||
| 32 | |||
| 33 | =head2 new | ||
| 34 | |||
| 35 | Returns a new, empty session handler, subclassing L<Jifty::Web::Session>. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub new { | ||
| 40 | my $class = shift; | ||
| 41 | my $cookie_name = Jifty->config->framework('Web')->{'SessionCookieName'}; | ||
| 42 | my $session_key = Jifty->config->framework('Web')->{'SessionSecret'} | ||
| 43 | or die "Please set SessionSecret in your framework/Web settings"; | ||
| 44 | my $cipher = Crypt::CBC->new( | ||
| 45 | -key => $session_key, | ||
| 46 | -cipher => 'Rijndael', | ||
| 47 | ); | ||
| 48 | bless { _cookie_name => $cookie_name, _cipher => $cipher, _session => undef }, $class; | ||
| 49 | } | ||
| 50 | |||
| 51 | =head2 _cipher | ||
| 52 | |||
| 53 | Accessor to the underlying L<Crypt::CBC> object that encapsulates the | ||
| 54 | server-side secret. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub _cipher { | ||
| 59 | my $self = shift; | ||
| 60 | $self->{'_cipher'} = shift if (@_); | ||
| 61 | return ( $self->{'_cipher'} ); | ||
| 62 | } | ||
| 63 | |||
| 64 | =head2 id | ||
| 65 | |||
| 66 | Returns the session's id if it has been loaded, or C<undef> otherwise. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub id { | ||
| 71 | my $self = shift; | ||
| 72 | return $self->loaded ? $self->_session->{session_id} : undef; | ||
| 73 | } | ||
| 74 | |||
| 75 | =head2 create | ||
| 76 | |||
| 77 | Since there is no server-side storage, this simply clears the object's | ||
| 78 | local state. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub create { | ||
| 83 | my $self = shift; | ||
| 84 | $self->_session({ | ||
| 85 | session_id => Jifty::Model::Session->new_session_id, | ||
| 86 | continuation => {}, | ||
| 87 | metadata => {}, | ||
| 88 | key => {}, | ||
| 89 | }); | ||
| 90 | } | ||
| 91 | |||
| 92 | =head2 load [ID] | ||
| 93 | |||
| 94 | Load up the current session from the given C<ID>, or the appropriate | ||
| 95 | cookie (see L<Jifty::Web::Session/cookie_name>) otherwise. | ||
| 96 | |||
| 97 | If both of those fail, creates a session in memory. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub load { | ||
| 102 | my $self = shift; | ||
| 103 | my $session_id = shift; | ||
| 104 | my %cookies = %{ Jifty->web->request->cookies }; | ||
| 105 | |||
| 106 | unless ($session_id) { | ||
| 107 | my $cookie_name = $self->cookie_name; | ||
| 108 | $session_id = $cookies{$cookie_name} if $cookies{$cookie_name}; | ||
| 109 | $session_id ||= Jifty::Model::Session->new_session_id; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | my $data; | ||
| 114 | |||
| 115 | { | ||
| 116 | local $@; | ||
| 117 | eval { | ||
| 118 | ($data) | ||
| 119 | = grep { $_->name eq "JIFTY_DAT_$session_id" } | ||
| 120 | $splitter->join( | ||
| 121 | map { | ||
| 122 | CGI::Cookie->new( -name => $_, -value => $cookies{$_} ) | ||
| 123 | } keys %cookies | ||
| 124 | ); | ||
| 125 | }; | ||
| 126 | |||
| 127 | if ($@) { | ||
| 128 | |||
| 129 | # Reassembly of cookie failed -- start a new session | ||
| 130 | $session_id = Jifty::Model::Session->new_session_id; | ||
| 131 | warn $@; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | if ($data) { | ||
| 136 | local $@; | ||
| 137 | eval { | ||
| 138 | local $Storable::Eval = 0; # Just to be on the safe side... | ||
| 139 | |||
| 140 | if (my $session = Storable::thaw( | ||
| 141 | Compress::Zlib::uncompress( | ||
| 142 | $self->_cipher->decrypt( | ||
| 143 | decode_base64( | ||
| 144 | $data->value | ||
| 145 | ) | ||
| 146 | ) | ||
| 147 | ) | ||
| 148 | )) { | ||
| 149 | $self->_session($session); | ||
| 150 | die "Session id mismatch" | ||
| 151 | unless $self->_session->{session_id} eq $session_id; | ||
| 152 | } | ||
| 153 | 1; | ||
| 154 | } and return; | ||
| 155 | warn $@ if $@; | ||
| 156 | } | ||
| 157 | |||
| 158 | $self->_session({ | ||
| 159 | session_id => $session_id, | ||
| 160 | continuation => {}, | ||
| 161 | metadata => {}, | ||
| 162 | key => {}, | ||
| 163 | }); | ||
| 164 | } | ||
| 165 | |||
| 166 | =head2 get KEY [TYPE] | ||
| 167 | |||
| 168 | See L<Jifty::Web::Session/get>. | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | sub get { | ||
| 173 | my $self = shift; | ||
| 174 | my $key = shift; | ||
| 175 | my $key_type = shift || "key"; | ||
| 176 | |||
| 177 | return undef unless $self->loaded; | ||
| 178 | return $self->_session->{$key_type}{$key}; | ||
| 179 | } | ||
| 180 | |||
| 181 | =head2 set KEY => VALUE, [TYPE] | ||
| 182 | |||
| 183 | See L<Jifty::Web::Session/set>. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | sub set { | ||
| 188 | my $self = shift; | ||
| 189 | my $key = shift; | ||
| 190 | my $value = shift; | ||
| 191 | my $key_type = shift || "key"; | ||
| 192 | |||
| 193 | return undef unless $self->loaded; | ||
| 194 | $self->_session->{$key_type}{$key} = $value; | ||
| 195 | |||
| 196 | # XXX - delay until the very last moment? | ||
| 197 | $self->flush; | ||
| 198 | } | ||
| 199 | |||
| 200 | =head2 remove KEY, [TYPE] | ||
| 201 | |||
| 202 | See L<Jifty::Web::Session/remove>. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | sub remove { | ||
| 207 | my $self = shift; | ||
| 208 | my $key = shift; | ||
| 209 | my $key_type = shift || "key"; | ||
| 210 | |||
| 211 | return undef unless $self->loaded; | ||
| 212 | delete $self->_session->{$key_type}{$key}; | ||
| 213 | } | ||
| 214 | |||
| 215 | =head2 continuations | ||
| 216 | |||
| 217 | See L<Jifty::Web::Session/continuations>. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | sub continuations { | ||
| 222 | my $self = shift; | ||
| 223 | return () unless $self->loaded; | ||
| 224 | return %{ $self->_session->{continuation} }; | ||
| 225 | } | ||
| 226 | |||
| 227 | =head2 unload | ||
| 228 | |||
| 229 | See L<Jifty::Web::Session/unload>. | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | sub unload { | ||
| 234 | my $self = shift; | ||
| 235 | $self->flush; | ||
| 236 | $self->_session(undef); | ||
| 237 | } | ||
| 238 | |||
| 239 | =head2 flush | ||
| 240 | |||
| 241 | Outputs the client-side session as one or more cookies. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub flush { | ||
| 246 | my $self = shift; | ||
| 247 | my $session_id = $self->id or return; | ||
| 248 | |||
| 249 | my $data_cookie = CGI::Cookie->new( | ||
| 250 | -name => "JIFTY_DAT_$session_id", | ||
| 251 | -expires => $self->expires, | ||
| 252 | -value => encode_base64( | ||
| 253 | $self->_cipher->encrypt( | ||
| 254 | Compress::Zlib::compress( | ||
| 255 | Storable::nfreeze( | ||
| 256 | $self->_session | ||
| 257 | ) | ||
| 258 | ) | ||
| 259 | ) | ||
| 260 | ) | ||
| 261 | ); | ||
| 262 | |||
| 263 | foreach my $cookie ($splitter->split( $data_cookie )) { | ||
| 264 | Jifty->web->response->cookies->{$cookie->name} = $cookie; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | 1; |