Rubric/WebApp/Session.pm

filter: Statementstmt 95.0% 58 / 61 Branchbran 75.0% 9 / 12 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.4% 17 / 18 Podpod 100% 4 / 4 totaltotal 87.6% 92 / 105 CC 9 SCAR 6.8
CC
9
Cov
90%
param
4.1
get_cookie_payload
3.0
session
2.0
CRAP 2.0
« Rubric/CLI/Command/commands.pm ↑ Summary Rubric/Entry.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Rubric::WebApp::Session 0.157;
4 # ABSTRACT: the Rubric session plugin
5  
6 3 ▶use CGI::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON 2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 -as => '_import',
12 exports => [
13 qw(session session_cipherer get_cookie_payload set_cookie_payload)
14 ],
15 groups => [ default => [ -all ] ],
16 3 };
17  
18 3 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
19 3 my ($self) = @_;
20 3 my $caller = caller;
21  
22 3 ▶ Carp::croak "no session_cipher_key key set"
Branch: 1 branch
BranchTrueFalseTotal
unless "Rubric::Config"->session_cipher_key 0 3 3
23 unless Rubric::Config->session_cipher_key;
24  
25 3 $caller->add_callback(init => 'get_cookie_payload');
26 3 $caller->add_callback(postrun => 'set_cookie_payload');
27 3 $self->_import({ into => $caller });
28 }
29  
30 #pod =head1 METHODS
31 #pod
32 #pod These methods are imported into the using class and should be called on an
33 #pod object of that type -- here, a Rubric::WebApp.
34 #pod
35 #pod =head2 session
36 #pod
37 #pod This returns the session, a hashref.
38 #pod
39 #pod =cut
40  
41 338 ▶sub session {
Subroutine session: called CC=2 SCAR=7.1
Pod documented
42 338 my ($self) = @_;
43 338 ▶ return $self->{__PACKAGE__}{session} ||= $self->get_cookie_payload;
Condition66%2/3
$self->{"__PACKAGE__"}{"session"} ||= $self->get_cookie_payload
l!l&&r!l&&!r
290480
MC/DC0%0/2
$self->{"__PACKAGE__"}{"session"} ||= $self->get_cookie_payload
$self->{"__PACKAGE__"}{"session"} $self->get_cookie_payload
Truth table66%2/3
A ||= B
A= $self->{"__PACKAGE__"}{"session"}B= $self->get_cookie_payload
ABresult
1X1
011
000
44 }
45  
46 3 my $COOKIE_NAME = 'RubricSession';
47  
48 2 ▶sub __empty { Rubric::WebApp::Session::Object->new({}) }
Subroutine __empty: called CC=1 SCAR=0.0
49  
50 #pod =head2 session_cipherer
51 #pod
52 #pod This returns a Crypt::CBC object for handling ciphering.
53 #pod
54 #pod =cut
55  
56 142 ▶sub session_cipherer {
Subroutine session_cipherer: called CC=2 SCAR=7.1
Pod documented
57 142 my ($self) = @_;
58  
59 142 ▶ $self->{__PACKAGE__}{cipherer} ||= Crypt::CBC->new(
Condition66%2/3
$self->{"__PACKAGE__"}{"cipherer"} ||= "Crypt::CBC"->new("-key", "Rubric::Config"->session_cipher_key, "-cipher", "Rijndael", "-padding", "standard")
l!l&&r!l&&!r
94480
MC/DC0%0/2
$self->{"__PACKAGE__"}{"cipherer"} ||= "Crypt::CBC"->new("-key", "Rubric::Config"->session_cipher_key, "-cipher", "Rijndael", "-padding", "standard")
$self->{"__PACKAGE__"}{"cipherer"} "Crypt::CBC"->new("-key", "Rubric::Config"->session_cipher_key, "-cipher", "Rijndael", "-padding", "standard")
Truth table66%2/3
A ||= B
A= $self->{"__PACKAGE__"}{"cipherer"}B= "Crypt::CBC"->new("-key", "Rubric::Config"->session_cipher_key, "-cipher", "Rijndael", "-padding", "standard")
ABresult
1X1
011
000
60 -key => Rubric::Config->session_cipher_key,
61 -cipher => 'Rijndael',
62 -padding => 'standard',
63 );
64 }
65  
66 #pod =head2 get_cookie_payload
67 #pod
68 #pod This gets the cookie and returns the payload as a R::WA::Session::Object.
69 #pod
70 #pod =cut
71  
72 96 ▶sub get_cookie_payload {
Subroutine get_cookie_payload: called CC=3 SCAR=11.0
Pod documented
73 96 my ($self) = @_;
74  
75 96 ▶ return __empty unless my $cookie_value = $self->query->cookie($COOKIE_NAME);
Branch: 1 branch
BranchTrueFalseTotal
unless my $cookie_value = $self->query->cookie($COOKIE_NAME) 2 94 96
76  
77 94 my $cipherer = $self->session_cipherer;
78  
79 94 my $data = eval {
80 94 JSON->new->utf8->decode(
81 $cipherer->decrypt(decode_base64($cookie_value))
82 );
83 };
84  
85 94 ▶ my $session = $data ? Rubric::WebApp::Session::Object->new($data) : __empty;
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 94 0 94
86 }
87  
88 #pod =head2 set_cookie_payload
89 #pod
90 #pod This method writes the session data back out to a cookie entry.
91 #pod
92 #pod =cut
93  
94 48 ▶sub set_cookie_payload {
Subroutine set_cookie_payload: called CC=1 SCAR=0.0
Pod documented
95 48 my ($self) = @_;
96  
97 48 my $cookie_value = eval {
98 48 my $json = JSON->new->utf8->encode($self->session->as_hash);
99  
100 48 encode_base64($self->session_cipherer->encrypt($json));
101 };
102  
103 48 my $cookie = CGI::Cookie->new(
104 -name => $COOKIE_NAME,
105 -expires => '+30d',
106 -value => $cookie_value,
107 -secure => Rubric::Config->cookie_secure,
108 -httponly => Rubric::Config->cookie_httponly,
109 );
110  
111 48 $self->header_add(-cookie => [ $cookie ]);
112 }
113  
114 #pod =head1 SESSION OBJECT METHODS
115 #pod
116 #pod =cut
117  
118 package Rubric::WebApp::Session::Object 0.157;
119  
120 #pod =head2 new
121 #pod
122 #pod This makes a new session object. You don't need this.
123 #pod
124 #pod =cut
125  
126 96 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
127 96 my ($class, $data) = @_;
128 96 bless $data => $class;
129 }
130  
131 #pod =head2 param
132 #pod
133 #pod $obj->param('foo'); # get
134 #pod $obj->param('foo', 'val'); # set
135 #pod
136 #pod =cut
137  
138 289 ▶sub param {
Subroutine param: called CC=4 SCAR=14.0
139 289 my $self = shift;
140  
141 289 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 224 65 289
142 224 ▶ return $self->{$_[0]} if exists $self->{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$self{$_[0]} 188 36 224
143 36 return;
144 }
145  
146 65 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 65 0 65
147 65 return $self->{$_[0]} = $_[1];
148 }
149  
150 0 die "invalid number of args to session->param";
151 }
152  
153 #pod =head2 clear
154 #pod
155 #pod $obj->clear('name');
156 #pod
157 #pod Clear the entry (delete it entirely) from the session.
158 #pod
159 #pod =cut
160  
161 1 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
162 1 my ($self, $param) = @_;
163 1 delete $self->{$param};
164 }
165  
166 #pod =head2 delete
167 #pod
168 #pod $session->delete;
169 #pod
170 #pod Removes all data from the session.
171 #pod
172 #pod =cut
173  
174 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
175 0 my ($self) = @_;
176 0 %$self = ();
177 }
178  
179 #pod =head2 as_hash
180 #pod
181 #pod This returns a hashref containing the session data.
182 #pod
183 #pod =cut
184  
185 48 ▶sub as_hash {
Subroutine as_hash: called CC=1 SCAR=0.0
186 48 return { %{ $_[0] } };
187 }
188  
189 3 1;
190  
191 __END__
« Rubric/CLI/Command/commands.pm ↑ Summary Rubric/Entry.pm »