1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
package Plack::Session::State::Cookie;
use strict;
use warnings;
our $VERSION = '0.36';
our $AUTHORITY = 'cpan:STEVAN';
use parent 'Plack::Session::State';
use Cookie::Baker;
use Plack::Util;
use Plack::Util::Accessor qw[
path
domain
expires
secure
httponly
samesite
partitioned
];
sub get_session_id {
my ($self, $env) = @_;
crush_cookie($env->{HTTP_COOKIE})->{$self->session_key};
}
sub merge_options {
my($self, %options) = @_;
delete $options{id};
$options{path} = $self->path || '/' if !exists $options{path};
$options{domain} = $self->domain if !exists $options{domain} && defined $self->domain;
$options{secure} = $self->secure if !exists $options{secure} && defined $self->secure;
$options{httponly} = $self->httponly if !exists $options{httponly} && defined $self->httponly;
$options{samesite} = $self->samesite if !exists $options{samesite} && defined $self->samesite;
# https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies
$options{partitioned} = $self->partitioned if !exists $options{partitioned} && defined $self->partitioned;
if (!exists $options{expires} && defined $self->expires) {
$options{expires} = time + $self->expires;
}
if ($options{partitioned}) {
$options{secure} = 1;
$options{samesite} = 'None';
}
return %options;
}
sub expire_session_id {
my ($self, $id, $res, $options) = @_;
my %opts = $self->merge_options(%$options, expires => time);
$self->_set_cookie($id, $res, %opts);
}
sub finalize {
my ($self, $id, $res, $options) = @_;
my %opts = $self->merge_options(%$options);
$self->_set_cookie($id, $res, %opts);
}
sub _set_cookie {
my($self, $id, $res, %options) = @_;
my $cookie = bake_cookie(
$self->session_key, {
value => $id,
%options,
}
);
Plack::Util::header_push($res->[1], 'Set-Cookie', $cookie);
}
1;
__END__
=pod
=head1 NAME
Plack::Session::State::Cookie - Basic cookie-based session state
=head1 SYNOPSIS
use Plack::Builder;
use Plack::Middleware::Session;
my $app = sub {
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
};
builder {
enable 'Session'; # Cookie is the default state
$app;
};
=head1 DESCRIPTION
This is a subclass of L<Plack::Session::State> and implements its
full interface. This is the default state used in
L<Plack::Middleware::Session>.
=head1 METHODS
=over 4
=item B<new ( %params )>
The C<%params> can include I<path>, I<domain>, I<expires>, I<secure>,
and I<httponly> options, as well as all the options accepted by
L<Plack::Session::State>.
=item B<path>
Path of the cookie, this defaults to "/";
=item B<domain>
Domain of the cookie, if nothing is supplied then it will not
be included in the cookie.
=item B<expires>
Expiration time of the cookie in seconds, if nothing is supplied then
it will not be included in the cookie, which means the session expires
per browser session.
=item B<secure>
Secure flag for the cookie, if nothing is supplied then it will not
be included in the cookie.
=item B<httponly>
HttpOnly flag for the cookie, if nothing is supplied then it will not
be included in the cookie.
=item B<samesite>
SameSite flag for the cookie, if nothing is supplied then it will not
be included in the cookie.
=back
=head1 BUGS
All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.
=head1 AUTHOR
Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2009, 2010 Infinity Interactive, Inc.
L<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|