| 1 | package Gantry::Plugins::Session; | ||||||||||||
| 2 | 2 | ▶ | use strict; use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | 2 | ▶ | use Gantry; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use Gantry::Utils::Crypt; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | ▶ | use base 'Exporter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | our @EXPORT = qw( | |||||||||||
| 9 | session_id | ||||||||||||
| 10 | session_init | ||||||||||||
| 11 | session_lock | ||||||||||||
| 12 | session_store | ||||||||||||
| 13 | session_remove | ||||||||||||
| 14 | session_inited | ||||||||||||
| 15 | session_update | ||||||||||||
| 16 | session_unlock | ||||||||||||
| 17 | session_retrieve | ||||||||||||
| 18 | do_cookiecheck | ||||||||||||
| 19 | ); | ||||||||||||
| 20 | |||||||||||||
| 21 | 2 | our $VERSION = '0.04'; | |||||||||||
| 22 | 2 | my %registered_callbacks; | |||||||||||
| 23 | 2 | my $lock = '_LOCK_'; | |||||||||||
| 24 | |||||||||||||
| 25 | #----------------------------------------------------------- | ||||||||||||
| 26 | # $class->get_callbacks( $namespace ) | ||||||||||||
| 27 | #----------------------------------------------------------- | ||||||||||||
| 28 | 0 | ▶ | sub get_callbacks { | ||||||||||
|
Subroutine
get_callbacks: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 29 | 0 | my ( $class, $namespace ) = @_; | |||||||||||
| 30 | |||||||||||||
| 31 | 0 | ▶ | return if ( $registered_callbacks{ $namespace }++ ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | |||||||||||||
| 33 | 0 | ▶ | warn "Your app needs a 'namespace' method which doesn't return 'Gantry'" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | if ( $namespace eq 'Gantry' ); | ||||||||||||
| 35 | return ( | ||||||||||||
| 36 | 0 | { phase => 'init', callback => \&initialize } | |||||||||||
| 37 | ); | ||||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | #----------------------------------------------------------- | ||||||||||||
| 41 | # initialize | ||||||||||||
| 42 | #----------------------------------------------------------- | ||||||||||||
| 43 | 0 | ▶ | sub initialize { | ||||||||||
|
Subroutine
initialize: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 44 | 0 | my ($gobj) = @_; | |||||||||||
| 45 | |||||||||||||
| 46 | 0 | $gobj->session_init(); | |||||||||||
| 47 | |||||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | 0 | ▶ | sub session_init { | ||||||||||
|
Subroutine
session_init: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 51 | 0 | my ($gobj) = @_; | |||||||||||
| 52 | |||||||||||||
| 53 | 0 | my $cache; | |||||||||||
| 54 | my $cookie; | ||||||||||||
| 55 | 0 | my $session; | |||||||||||
| 56 | 0 | ▶ | my $app_rootp = $gobj->app_rootp || ""; | ||||||||||
|
Condition0%0/2
$gobj->app_rootp || ''
MC/DC0%0/1
$gobj->app_rootp || ''
$gobj->app_rootp
Truth table0%0/2
A
A= $gobj->app_rootp
| |||||||||||||
| 57 | 0 | my $cookiecheck = $app_rootp . '/cookiecheck'; | |||||||||||
| 58 | 0 | my $regex = qr/^${app_rootp}\/(cookiecheck).*/; | |||||||||||
| 59 | 0 | ▶ | my $secret = $gobj->fish_config('session_secret') || 'w3s3cR7'; | ||||||||||
|
Condition0%0/2
$gobj->fish_config('session_secret') || 'w3s3cR7'
MC/DC0%0/1
$gobj->fish_config('session_secret') || 'w3s3cR7'
$gobj->fish_config('session_secret')
Truth table0%0/2
A
A= $gobj->fish_config('session_secret')
| |||||||||||||
| 60 | 0 | my $crypt = Gantry::Utils::Crypt->new({'secret' => $secret}); | |||||||||||
| 61 | |||||||||||||
| 62 | 0 | ▶ | return if ($gobj->fish_uri =~ /^$regex/); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | |||||||||||||
| 64 | # check to see if a previous session is active | ||||||||||||
| 65 | |||||||||||||
| 66 | 0 | ▶ | if ($session = $gobj->get_cookies('_session_id_')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | |||||||||||||
| 68 | # OK, store the session id | ||||||||||||
| 69 | |||||||||||||
| 70 | 0 | $gobj->session_id($session); | |||||||||||
| 71 | |||||||||||||
| 72 | } else { | ||||||||||||
| 73 | |||||||||||||
| 74 | # set a cookie and see if it works | ||||||||||||
| 75 | |||||||||||||
| 76 | 0 | $cookie = $crypt->encrypt(time, {}, rand(), $$); | |||||||||||
| 77 | |||||||||||||
| 78 | 0 | $gobj->set_cookie( | |||||||||||
| 79 | { | ||||||||||||
| 80 | name => '_session_id_', | ||||||||||||
| 81 | value => $cookie, | ||||||||||||
| 82 | path => '/' | ||||||||||||
| 83 | } | ||||||||||||
| 84 | ); | ||||||||||||
| 85 | |||||||||||||
| 86 | 0 | $gobj->relocate($cookiecheck); | |||||||||||
| 87 | |||||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | 0 | ▶ | sub do_cookiecheck { | ||||||||||
|
Subroutine
do_cookiecheck: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||
| 93 | 0 | my $gobj = shift; | |||||||||||
| 94 | |||||||||||||
| 95 | 0 | my $session; | |||||||||||
| 96 | 0 | ▶ | my $app_rootp = $gobj->app_rootp || "/"; | ||||||||||
|
Condition0%0/2
$gobj->app_rootp || '/'
MC/DC0%0/1
$gobj->app_rootp || '/'
$gobj->app_rootp
Truth table0%0/2
A
A= $gobj->app_rootp
| |||||||||||||
| 97 | |||||||||||||
| 98 | # if cookies are enabled they should be returned on the redirect | ||||||||||||
| 99 | |||||||||||||
| 100 | 0 | ▶ | if ($session = $gobj->get_cookies('_session_id_')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | |||||||||||||
| 102 | # Ok, redirect them back to the applicaion | ||||||||||||
| 103 | |||||||||||||
| 104 | 0 | $gobj->session_inited(1); | |||||||||||
| 105 | 0 | $gobj->session_id($session); | |||||||||||
| 106 | 0 | $gobj->session_store($lock, '0'); | |||||||||||
| 107 | 0 | $gobj->relocate($app_rootp); | |||||||||||
| 108 | |||||||||||||
| 109 | } else { | ||||||||||||
| 110 | |||||||||||||
| 111 | # Hmmm, OK, lets give them a nudge | ||||||||||||
| 112 | |||||||||||||
| 113 | 0 | ▶ | my $session_title = $gobj->fish_config('session_title') || 'Missing Cookies'; | ||||||||||
|
Condition0%0/2
$gobj->fish_config('session_title') || 'Missing Cookies'
MC/DC0%0/1
$gobj->fish_config('session_title') || 'Missing Cookies'
$gobj->fish_config('session_title')
Truth table0%0/2
A
A= $gobj->fish_config('session_title')
| |||||||||||||
| 114 | 0 | ▶ | my $session_wrapper = $gobj->fish_config('session_wrapper') || 'default.tt'; | ||||||||||
|
Condition0%0/2
$gobj->fish_config('session_wrapper') || 'default.tt'
MC/DC0%0/1
$gobj->fish_config('session_wrapper') || 'default.tt'
$gobj->fish_config('session_wrapper')
Truth table0%0/2
A
A= $gobj->fish_config('session_wrapper')
| |||||||||||||
| 115 | 0 | ▶ | my $session_template = $gobj->fish_config('session_template') || 'session.tt'; | ||||||||||
|
Condition0%0/2
$gobj->fish_config('session_template') || 'session.tt'
MC/DC0%0/1
$gobj->fish_config('session_template') || 'session.tt'
$gobj->fish_config('session_template')
Truth table0%0/2
A
A= $gobj->fish_config('session_template')
| |||||||||||||
| 116 | |||||||||||||
| 117 | 0 | $gobj->template_wrapper($session_wrapper); | |||||||||||
| 118 | 0 | $gobj->stash->view->title($session_title); | |||||||||||
| 119 | 0 | $gobj->stash->view->template($session_template); | |||||||||||
| 120 | |||||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | #----------------------------------------------------------- | ||||||||||||
| 126 | # session_store | ||||||||||||
| 127 | #----------------------------------------------------------- | ||||||||||||
| 128 | 0 | ▶ | sub session_store { | ||||||||||
|
Subroutine
session_store: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 129 | 0 | my ($gobj, $key, $value) = (shift, shift, shift); | |||||||||||
| 130 | |||||||||||||
| 131 | 0 | my $session = $gobj->session_id(); | |||||||||||
| 132 | |||||||||||||
| 133 | 0 | $gobj->cache_namespace($session); | |||||||||||
| 134 | 0 | $gobj->cache_set($key, $value); | |||||||||||
| 135 | |||||||||||||
| 136 | } | ||||||||||||
| 137 | |||||||||||||
| 138 | #----------------------------------------------------------- | ||||||||||||
| 139 | # session_retrieve | ||||||||||||
| 140 | #----------------------------------------------------------- | ||||||||||||
| 141 | 0 | ▶ | sub session_retrieve { | ||||||||||
|
Subroutine
session_retrieve: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 142 | 0 | my ($gobj, $key) = (shift, shift); | |||||||||||
| 143 | |||||||||||||
| 144 | 0 | my $data; | |||||||||||
| 145 | 0 | my $session = $gobj->session_id(); | |||||||||||
| 146 | |||||||||||||
| 147 | 0 | $gobj->cache_namespace($session); | |||||||||||
| 148 | 0 | $data = $gobj->cache_get($key); | |||||||||||
| 149 | |||||||||||||
| 150 | 0 | return $data; | |||||||||||
| 151 | |||||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | #----------------------------------------------------------- | ||||||||||||
| 155 | # session_remove | ||||||||||||
| 156 | #----------------------------------------------------------- | ||||||||||||
| 157 | 0 | ▶ | sub session_remove { | ||||||||||
|
Subroutine
session_remove: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 158 | 0 | my ($gobj, $key) = (shift, shift); | |||||||||||
| 159 | |||||||||||||
| 160 | 0 | my $session = $gobj->session_id(); | |||||||||||
| 161 | |||||||||||||
| 162 | 0 | $gobj->cache_namespace($session); | |||||||||||
| 163 | 0 | $gobj->cache_del($key); | |||||||||||
| 164 | |||||||||||||
| 165 | } | ||||||||||||
| 166 | |||||||||||||
| 167 | #----------------------------------------------------------- | ||||||||||||
| 168 | # session_update | ||||||||||||
| 169 | #----------------------------------------------------------- | ||||||||||||
| 170 | 0 | ▶ | sub session_update { | ||||||||||
|
Subroutine
session_update: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 171 | 0 | my ($gobj, $key, $value) = (shift, shift, shift); | |||||||||||
| 172 | |||||||||||||
| 173 | 0 | my $session = $gobj->session_id(); | |||||||||||
| 174 | |||||||||||||
| 175 | 0 | $gobj->cache_namespace($session); | |||||||||||
| 176 | 0 | $gobj->cache_del($key); | |||||||||||
| 177 | 0 | $gobj->cache_set($key, $value); | |||||||||||
| 178 | |||||||||||||
| 179 | } | ||||||||||||
| 180 | |||||||||||||
| 181 | #----------------------------------------------------------- | ||||||||||||
| 182 | # session_id | ||||||||||||
| 183 | #----------------------------------------------------------- | ||||||||||||
| 184 | 0 | ▶ | sub session_id { | ||||||||||
|
Subroutine
session_id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 185 | 0 | my ($gobj, $p) = (shift, shift); | |||||||||||
| 186 | |||||||||||||
| 187 | 0 | ▶ | $$gobj{__SESSION_ID__} = $p if defined $p; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | 0 | return($$gobj{__SESSION_ID__}); | |||||||||||
| 189 | |||||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | #----------------------------------------------------------- | ||||||||||||
| 193 | # session_inited | ||||||||||||
| 194 | #----------------------------------------------------------- | ||||||||||||
| 195 | 0 | ▶ | sub session_inited { | ||||||||||
|
Subroutine
session_inited: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 196 | 0 | my ($gobj, $p) = @_; | |||||||||||
| 197 | |||||||||||||
| 198 | 0 | ▶ | $$gobj{__SESSION_INITED__} = $p if defined $p; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 199 | 0 | return($$gobj{__SESSION_INITED__}); | |||||||||||
| 200 | |||||||||||||
| 201 | } | ||||||||||||
| 202 | |||||||||||||
| 203 | #----------------------------------------------------------- | ||||||||||||
| 204 | # session_lock | ||||||||||||
| 205 | #----------------------------------------------------------- | ||||||||||||
| 206 | 0 | ▶ | sub session_lock { | ||||||||||
|
Subroutine
session_lock: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 207 | 0 | my ($gobj, $attempts) = (shift, shift); | |||||||||||
| 208 | |||||||||||||
| 209 | 0 | my $value; | |||||||||||
| 210 | 0 | my $stat = 0; | |||||||||||
| 211 | 0 | my $session = $gobj->session_id(); | |||||||||||
| 212 | |||||||||||||
| 213 | |||||||||||||
| 214 | 0 | ▶ | $attempts = 30 if (!defined($attempts)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 215 | 0 | $gobj->cache_namespace($session); | |||||||||||
| 216 | |||||||||||||
| 217 | 0 | ▶ | for (my $x = 0; $x < $attempts; $x++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 218 | |||||||||||||
| 219 | 0 | ▶ | $value = $gobj->cache_get($lock) || 0; | ||||||||||
|
Condition0%0/2
$gobj->cache_get($lock) || 0
MC/DC0%0/1
$gobj->cache_get($lock) || 0
$gobj->cache_get($lock)
Truth table0%0/2
A
A= $gobj->cache_get($lock)
| |||||||||||||
| 220 | 0 | ▶ | if ($value eq '1') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 221 | |||||||||||||
| 222 | 0 | sleep(1); | |||||||||||
| 223 | |||||||||||||
| 224 | } else { | ||||||||||||
| 225 | |||||||||||||
| 226 | 0 | $gobj->cache_set($lock, '1'); | |||||||||||
| 227 | 0 | $stat = 1; | |||||||||||
| 228 | 0 | last; | |||||||||||
| 229 | |||||||||||||
| 230 | } | ||||||||||||
| 231 | |||||||||||||
| 232 | } | ||||||||||||
| 233 | |||||||||||||
| 234 | 0 | return $stat; | |||||||||||
| 235 | |||||||||||||
| 236 | } | ||||||||||||
| 237 | |||||||||||||
| 238 | #----------------------------------------------------------- | ||||||||||||
| 239 | # session_unlock | ||||||||||||
| 240 | #----------------------------------------------------------- | ||||||||||||
| 241 | 0 | ▶ | sub session_unlock { | ||||||||||
|
Subroutine
session_unlock: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 242 | 0 | my ($gobj) = (shift); | |||||||||||
| 243 | |||||||||||||
| 244 | 0 | my $session = $gobj->session_id(); | |||||||||||
| 245 | |||||||||||||
| 246 | 0 | $gobj->cache_namespace($session); | |||||||||||
| 247 | 0 | $gobj->cache_set($lock, '0'); | |||||||||||
| 248 | |||||||||||||
| 249 | } | ||||||||||||
| 250 | |||||||||||||
| 251 | 2 | 1; | |||||||||||
| 252 | |||||||||||||
| 253 | __END__ | ||||||||||||