Refactored Middleware::Session to call ->state and ->store inside more
[catagits/Web-Session.git] / lib / Plack / Middleware / Session / Cookie.pm
CommitLineData
d326e755 1package Plack::Middleware::Session::Cookie;
2use strict;
3use parent qw(Plack::Middleware::Session);
4
5use Plack::Util::Accessor qw(secret session_key domain expires path secure);
6
7use Digest::HMAC_SHA1;
8use MIME::Base64 ();
9use Storable ();
10use Time::HiRes;
11use Plack::Util;
12
13use Plack::Session::State::Cookie;
14
15sub prepare_app {
16 my $self = shift;
17
4ff41723 18 Plack::Util::load_class($self->session_class) if $self->session_class;
d326e755 19 $self->session_key("plack_session") unless $self->session_key;
20
dc556d28 21 $self->state( Plack::Session::State::Cookie->new );
d326e755 22 for my $attr (qw(session_key path domain expires secure)) {
dc556d28 23 $self->state->$attr($self->$attr);
d326e755 24 }
dc556d28 25}
26
27sub get_session {
28 my($self, $request) = @_;
29
30 my $cookie = $self->state->get_session_id($request) or return;
d326e755 31
dc556d28 32 my($time, $b64, $sig) = split /:/, $cookie, 3;
33 $self->sig($b64) eq $sig or return;
34
35 my $session = Storable::thaw(MIME::Base64::decode($b64));
36 return ($time, $session);
d326e755 37}
38
dc556d28 39sub generate_id {
40 my $self = shift;
41 return Time::HiRes::gettimeofday;
42}
4ff41723 43
dc556d28 44sub commit { }
45
46sub save_state {
47 my($self, $id, $res, $session, $options) = @_;
48
49 my $cookie = $self->_serialize($id, $session);
50 $self->state->finalize($cookie, $res, $options);
4ff41723 51}
52
d326e755 53sub _serialize {
dc556d28 54 my($self, $id, $session) = @_;
d326e755 55
d326e755 56 my $b64 = MIME::Base64::encode( Storable::freeze($session), '' );
dc556d28 57 join ":", $id, $b64, $self->sig($b64);
d326e755 58}
59
60sub sig {
61 my($self, $b64) = @_;
62 return '.' unless $self->secret;
63 Digest::HMAC_SHA1::hmac_sha1_hex($b64, $self->secret);
64}
65
661;
67
68__END__
69
70=head1 NAME
71
72Plack::Middleware::Session::Cookie - Session middleware that saves session data in the cookie
73
74=head1 SYNOPSIS
75
76 enable "Session::Cookie";
77
78=head1 DESCRIPTION
79
80This middleware component allows you to use the cookie as a sole
81cookie state and store, without any server side storage to do the
82session management. This middleware utilizes its own state and store
83automatically for you, so you can't override the objects.
84
85=head1 CONFIGURATIONS
86
87This middleware is a subclass of L<Plack::Middleware::Session> and
88accepts most configuration of the parent class. In addition, following
89options are accepted.
90
91=over 4
92
93=item secret
94
95Server side secret to sign the session data using HMAC SHA1. Defaults
96to nothing (i.e. do not sign) but B<strongly recommended> to set your
97own secret string.
98
99=item session_key, domain, expires, path, secure
100
101Accessors for the cookie attribuets. See
102L<Plack::Session::State::Cookie> for these options.
103
104=back
105
106=head1 AUTHOR
107
108Tatsuhiko Miyagawa
109
110=head1 SEE ALSO
111
112Rack::Session::Cookie L<Dancer::Session::Cookie>
113
114=cut
115