Make psgix.session a hash reference and plack.session an (optional)
[catagits/Web-Session.git] / lib / Plack / Session / State / Cookie.pm
CommitLineData
bd992981 1package Plack::Session::State::Cookie;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
bd992981 8use parent 'Plack::Session::State';
9
ac4892f4 10use Plack::Util::Accessor qw[
11 path
12 domain
13 expires
14 secure
15];
bd992981 16
4a0cb5a0 17sub get_session_id {
bd992981 18 my ($self, $request) = @_;
ae35574f 19 ( $request->cookie( $self->session_key ) || return )->value;
bd992981 20}
21
caf3bd90 22sub expire_session_id {
23 my ($self, $id, $response) = @_;
24 $response->cookies->{ $self->session_key } = +{
25 value => $id,
26 path => ($self->path || '/'),
27 expires => 0,
28 ( defined $self->domain ? ( domain => $self->domain ) : () ),
29 ( defined $self->secure ? ( secure => $self->secure ) : () ),
30 };
31}
32
bd992981 33sub finalize {
4ff41723 34 my ($self, $id, $response, $options) = @_;
bd992981 35 $response->cookies->{ $self->session_key } = +{
36 value => $id,
37 path => ($self->path || '/'),
05b5f99d 38 ( defined $self->domain ? ( domain => $self->domain ) : () ),
39 ( defined $self->expires ? ( expires => $self->expires ) : () ),
40 ( defined $self->secure ? ( secure => $self->secure ) : () ),
bd992981 41 };
42}
43
fe1bfe7d 441;
ac4892f4 45
46__END__
47
48=pod
49
50=head1 NAME
51
52Plack::Session::State::Cookie - Basic cookie-based session state
53
3d92cf47 54=head1 SYNOPSIS
55
56 use Plack::Builder;
57 use Plack::Middleware::Session;
58
59 my $app = sub {
60 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
61 };
62
63 builder {
64 enable 'Session'; # Cookie is the default state
65 $app;
66 };
67
ac4892f4 68=head1 DESCRIPTION
69
43f34c01 70This is a subclass of L<Plack::Session::State> and implements it's
3d92cf47 71full interface. This is the default state used in
72L<Plack::Middleware::Session>.
43f34c01 73
ac4892f4 74=head1 METHODS
75
76=over 4
77
78=item B<new ( %params )>
79
3d92cf47 80The C<%params> can include I<path>, I<domain>, I<expires> and
81I<secure> options, as well as all the options accepted by
82L<Plack::Session::Store>.
83
ac4892f4 84=item B<path>
85
3d92cf47 86Path of the cookie, this defaults to "/";
87
ac4892f4 88=item B<domain>
89
3d92cf47 90Domain of the cookie, if nothing is supplied then it will not
91be included in the cookie.
92
ac4892f4 93=item B<expires>
94
3d92cf47 95Expiration time of the cookie, if nothing is supplied then it will
96not be included in the cookie.
97
ac4892f4 98=item B<secure>
99
3d92cf47 100Secure flag for the cookie, if nothing is supplied then it will not
101be included in the cookie.
102
ac4892f4 103=back
104
ac4892f4 105=head1 BUGS
106
107All complex software has bugs lurking in it, and this module is no
108exception. If you find a bug please either email me, or add the bug
109to cpan-RT.
110
111=head1 AUTHOR
112
113Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
114
115=head1 COPYRIGHT AND LICENSE
116
000c696e 117Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 118
119L<http://www.iinteractive.com>
120
121This library is free software; you can redistribute it and/or modify
122it under the same terms as Perl itself.
123
124=cut
125
126