| 1 | package Catalyst::Plugin::Starch::Cookie; | ||||||||||
| 2 | 1 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | our $VERSION = '0.08'; | |||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Catalyst::Plugin::Starch::Cookie - Track starch state in a cookie. | ||||||||||
| 9 | |||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||
| 11 | |||||||||||
| 12 | package MyApp; | ||||||||||
| 13 | |||||||||||
| 14 | use Catalyst qw( | ||||||||||
| 15 | Starch::Cookie | ||||||||||
| 16 | Starch | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||
| 20 | |||||||||||
| 21 | This plugin utilizes the L<Starch::Plugin::CookieArgs> plugin to add | ||||||||||
| 22 | a bunch of arguments to the Starch object, search the request cookies for | ||||||||||
| 23 | the session cookie, and write the session cookie at the end of the request. | ||||||||||
| 24 | |||||||||||
| 25 | See the L<Starch::Plugin::CookieArgs> documentation for a | ||||||||||
| 26 | list of arguments you can specify in the Catalyst configuration for | ||||||||||
| 27 | L<Catalyst::Plugin::Starch>. | ||||||||||
| 28 | |||||||||||
| 29 | =cut | ||||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | use Class::Method::Modifiers qw( fresh ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | =head1 COMPATIBILITY | ||||||||||
| 37 | |||||||||||
| 38 | Most of the methods documented in | ||||||||||
| 39 | L<Catalyst::Plugin::Session::Cookie/METHODS> are not | ||||||||||
| 40 | supported at this time: | ||||||||||
| 41 | |||||||||||
| 42 | =over | ||||||||||
| 43 | |||||||||||
| 44 | =item * | ||||||||||
| 45 | |||||||||||
| 46 | The C<make_session_cookie>, C<calc_expiry>, | ||||||||||
| 47 | C<calculate_session_cookie_expires>, C<cookie_is_rejecting>, | ||||||||||
| 48 | C<delete_session_id>, C<extend_session_id>, | ||||||||||
| 49 | C<get_session_id>, and C<set_session_id> methods are not currently | ||||||||||
| 50 | supported but could be if necessary. | ||||||||||
| 51 | |||||||||||
| 52 | =back | ||||||||||
| 53 | |||||||||||
| 54 | The above listed un-implemented methods and attributes will throw an exception | ||||||||||
| 55 | if called. | ||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | # These are already blocked by Catalyst::Plugin::Starch: | ||||||||||
| 60 | # delete_session_id extend_session_id | ||||||||||
| 61 | # get_session_id set_session_id | ||||||||||
| 62 | |||||||||||
| 63 | 1 | foreach my $method (qw( | |||||||||
| 64 | make_session_cookie calc_expiry | ||||||||||
| 65 | calculate_session_cookie_expires cookie_is_rejecting | ||||||||||
| 66 | )) { | ||||||||||
| 67 | 1 | ▶ | fresh $method => sub{ | ||||||||
|
Subroutine
make_session_cookie: called CC=1 SCAR=0.0
| |||||||||||
| 68 | 1 | Catalyst::Exception->throw( "The $method method is not implemented by Catalyst::Plugin::Starch::Cookie" ); | |||||||||
| 69 | 4 | }; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | =head1 METHODS | ||||||||||
| 73 | |||||||||||
| 74 | =head2 get_session_cookie | ||||||||||
| 75 | |||||||||||
| 76 | Returns the L<CGI::Simple::Cookie> object from L<Catalyst::Request> | ||||||||||
| 77 | for the session cookie, if there is one. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | 5 | ▶ | sub get_session_cookie { | ||||||||
|
Subroutine
get_session_cookie: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 82 | 5 | my ($c) = @_; | |||||||||
| 83 | |||||||||||
| 84 | 5 | my $cookie_name = $c->starch->cookie_name(); | |||||||||
| 85 | 5 | my $cookie = $c->req->cookies->{ $cookie_name }; | |||||||||
| 86 | |||||||||||
| 87 | 5 | return $cookie; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | =head2 update_session_cookie | ||||||||||
| 91 | |||||||||||
| 92 | This is called automatically by the C<finalize_headers> step in Catalyst. This method | ||||||||||
| 93 | is provided if you want to override the behavior. | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | 5 | ▶ | sub update_session_cookie { | ||||||||
|
Subroutine
update_session_cookie: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 98 | 5 | my ($c) = @_; | |||||||||
| 99 | 5 | ▶ | return if !$c->_has_sessionid(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 4 | my $cookie_name = $c->starch->cookie_name(); | |||||||||
| 101 | 4 | $c->res->cookies->{ $cookie_name } = $c->starch_state->cookie_args(); | |||||||||
| 102 | 4 | return; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 5 | ▶ | after prepare_cookies => sub{ | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 106 | 5 | my ($c) = @_; | |||||||||
| 107 | |||||||||||
| 108 | 5 | my $cookie = $c->get_session_cookie(); | |||||||||
| 109 | 5 | ▶ | return if !$cookie; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | |||||||||||
| 111 | 3 | my $id = $cookie->value(); | |||||||||
| 112 | 3 | ▶ | return if !$c->starch->state_id_type->check( $id ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | |||||||||||
| 114 | 3 | $c->_set_sessionid( $id ); | |||||||||
| 115 | 3 | return; | |||||||||
| 116 | 1 | }; | |||||||||
| 117 | |||||||||||
| 118 | 5 | ▶ | before finalize_headers => sub{ | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 119 | 5 | my ($c) = @_; | |||||||||
| 120 | 5 | $c->update_session_cookie(); | |||||||||
| 121 | 5 | return; | |||||||||
| 122 | 1 | }; | |||||||||
| 123 | |||||||||||
| 124 | 1 | ▶ | around default_starch_plugins => sub{ | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 125 | 1 | my $orig = shift; | |||||||||
| 126 | 1 | my $c = shift; | |||||||||
| 127 | |||||||||||
| 128 | return [ | ||||||||||
| 129 | 1 | @{ $c->$orig() }, | |||||||||
| 130 | '::CookieArgs', | ||||||||||
| 131 | ]; | ||||||||||
| 132 | 1 | }; | |||||||||
| 133 | |||||||||||
| 134 | 1 | 1; | |||||||||
| 135 | __END__ | ||||||||||