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