blib/lib/Lemonldap/NG/Portal/Plugins/SingleSession.pm

filter: Statementstmt 69.4% 82 / 118 Branchbran 54.1% 26 / 48 Conditioncond 69.5% 16 / 23 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 92.3% 12 / 13 Podpod 25.0% 1 / 4 totaltotal 65.3% 143 / 219 CC 36 SCAR 33.2
CC
36
Cov
66%
removeOther
72.0
run
28.8
init
4.1
CRAP 27.6
« blib/lib/Lemonldap/NG/Portal/Lib/Choice.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/DBI.pm »
1 package Lemonldap::NG::Portal::Plugins::SingleSession;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Lemonldap::NG::Common::Session 'id2storage';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 PE_OK
10 PE_ERROR
11 PE_NOTOKEN
12 PE_TOKENEXPIRED
13 1 );
14  
15 1 our $VERSION = '2.23.0';
16  
17 1 extends qw(
18 Lemonldap::NG::Portal::Main::Plugin
19 Lemonldap::NG::Portal::Lib::OtherSessions
20 );
21  
22 1 ▶use constant endAuth => 'run';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 has singleIPRule => ( is => 'rw' );
25 1 has singleSessionRule => ( is => 'rw' );
26 1 has singleUserByIPRule => ( is => 'rw' );
27 has ott => (
28 is => 'rw',
29 lazy => 1,
30 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 my $ott =
32 2 $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
33 2 $ott->timeout( $_[0]->conf->{formTimeout} );
34 2 return $ott;
35 }
36 1 );
37  
38 14 ▶sub storageId {
Subroutine storageId: called CC=2 SCAR=7.2
Pod undocumented
39 14 my ( $self, $id ) = @_;
40 14 ▶ return $self->conf->{hashedSessionStore} ? id2storage($id) : $id;
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'hashedSessionStore'} ? : 0 14 14
41 }
42  
43 13 ▶sub init {
Subroutine init: called CC=4 SCAR=14.2
Pod undocumented
44 13 my ($self) = @_;
45 13 $self->addAuthRoute( removeOther => 'removeOther', ['GET'] );
46  
47 # Build triggering rules from configuration
48 $self->singleIPRule(
49 13 $self->p->buildRule( $self->conf->{singleIP}, 'singleIP' ) );
50 13 ▶ return 0 unless $self->singleIPRule;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->singleIPRule 0 13 13
51  
52 $self->singleSessionRule(
53 13 $self->p->buildRule( $self->conf->{singleSession}, 'singleSession' ) );
54 13 ▶ return 0 unless $self->singleSessionRule;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->singleSessionRule 0 13 13
55  
56 $self->singleUserByIPRule(
57 13 $self->p->buildRule( $self->conf->{singleUserByIP}, 'singleUserByIP' )
58 );
59 13 ▶ return 0 unless $self->singleUserByIPRule;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->singleUserByIPRule 0 13 13
60  
61 13 return 1;
62 }
63  
64 12 ▶sub run {
Subroutine run: called CC=25 SCAR=33.6
Pod documented
65 12 my ( $self, $req ) = @_;
66 12 my ( $linkedSessionId, $token, $html ) = ( '', '', '' );
67 12 my $deleted = [];
68 12 my $otherSessions = [];
69 12 my @otherSessionsId = ();
70  
71 12 ▶ my $moduleOptions = $self->conf->{globalStorageOptions} || {};
Condition50%1/2
$self->conf->{'globalStorageOptions'} || {}
l!l
120
MC/DC0%0/1
$self->conf->{'globalStorageOptions'} || {}
$self->conf->{'globalStorageOptions'}
Truth table50%1/2
A
A= $self->conf->{'globalStorageOptions'}
Aresult
11
00
72 12 $moduleOptions->{backend} = $self->conf->{globalStorage};
73  
74 12 my $singleSessionRuleMatched =
75 $self->singleSessionRule->( $req, $req->sessionInfo );
76 12 my $singleIPRuleMatched = $self->singleIPRule->( $req, $req->sessionInfo );
77 12 my $singleUserByIPRuleMatched =
78 $self->singleUserByIPRule->( $req, $req->sessionInfo );
79  
80 12 ▶ if ( $singleSessionRuleMatched
Branch: 1 branch
BranchTrueFalseTotal
if ($singleSessionRuleMatched or $singleIPRuleMatched or $self->conf->{"notifyOther"}) 8 4 12
Condition83%5/6
$singleSessionRuleMatched or $singleIPRuleMatched or $self->conf->{"notifyOther"}
l!l&&r!l&&!r
804
$singleSessionRuleMatched or $singleIPRuleMatched
l!l&&r!l&&!r
354
MC/DC66%2/3
$singleSessionRuleMatched or $singleIPRuleMatched or $self->conf->{"notifyOther"}
$singleSessionRuleMatched $singleIPRuleMatched $self->conf->{"notifyOther"}
Truth table75%3/4
A or B or C
A= $singleSessionRuleMatchedB= $singleIPRuleMatchedC= $self->conf->{"notifyOther"}
ABCresult
1XX1
01X1
0011
0000
81 or $singleIPRuleMatched
82 or $self->conf->{notifyOther} )
83 {
84 my $sessions = $self->module->searchOn(
85 $moduleOptions,
86 $self->conf->{whatToTrace},
87 $req->{sessionInfo}->{ $self->conf->{whatToTrace} }
88 8 );
89  
90 8 ▶ if ( $self->conf->{securedCookie} == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"securedCookie"} == 2) 0 8 8
91 0 $self->logger->debug("Looking for double sessions...");
92  
93 # searchOn() returns sessions indexed by their storage ID, then
94 # it is required to use hashed ID
95 $linkedSessionId =
96 0 $sessions->{ $self->storageId( $req->id ) }->{_httpSession};
97 0 ▶ my $msg =
Branch: 1 branch
BranchTrueFalseTotal
$linkedSessionId ? : 0 0 0
98 $linkedSessionId
99 ? "Linked session found -> $linkedSessionId / " . $req->id
100 : "NO linked session found!";
101 0 $self->logger->debug($msg);
102 }
103  
104 # searchOn() returns sessions indexed by their storage ID, then
105 # it is required to use hashed ID
106 8 my $storageId = $self->storageId( $req->id );
107 8 foreach my $id ( keys %$sessions ) {
108 17 ▶ next if $id eq $storageId;
Branch: 1 branch
BranchTrueFalseTotal
if $id eq $storageId 8 9 17
109 next
110 9 ▶ if $linkedSessionId and $id eq $self->storageId($linkedSessionId);
Branch: 1 branch
BranchTrueFalseTotal
if $linkedSessionId and $id eq $self->storageId($linkedSessionId) 0 9 9
Condition33%1/3
$linkedSessionId and $id eq $self->storageId($linkedSessionId)
!ll&&!rl&&r
900
MC/DC0%0/2
$linkedSessionId and $id eq $self->storageId($linkedSessionId)
$linkedSessionId $id eq $self->storageId($linkedSessionId)
Truth table33%1/3
A and B
A= $linkedSessionIdB= $id eq $self->storageId($linkedSessionId)
ABresult
0X0
100
111
111 9 ▶ my $session = $self->p->getApacheSession( $id, hashStore => 0 )
Branch: 1 branch
BranchTrueFalseTotal
my $session = $self->p->getApacheSession($id, "hashStore", 0) or next 9 0 9
112 or next;
113 9 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->singleSessionRule->($req, $req->sessionInfo) or $self->singleIPRule->($req, $req->sessionInfo) and $req->{'sessionInfo'}{'ipAddr'} ne $session->data->{'ipAddr'}) { } 6 3 9
Condition83%5/6
$self->singleSessionRule->($req, $req->sessionInfo) or $self->singleIPRule->($req, $req->sessionInfo) and $req->{"sessionInfo"}{"ipAddr"} ne $session->data->{"ipAddr"}
l!l&&r!l&&!r
243
$self->singleIPRule->($req, $req->sessionInfo) and $req->{"sessionInfo"}{"ipAddr"} ne $session->data->{"ipAddr"}
!ll&&!rl&&r
034
MC/DC66%2/3
$self->singleSessionRule->($req, $req->sessionInfo) or $self->singleIPRule->($req, $req->sessionInfo) and $req->{"sessionInfo"}{"ipAddr"} ne $session->data->{"ipAddr"}
$self->singleSessionRule->($req, $req->sessionInfo) $self->singleIPRule->($req, $req->sessionInfo) $req->{"sessionInfo"}{"ipAddr"} ne $session->data->{"ipAddr"}
Truth table75%3/4
A or B and C
A= $self->singleSessionRule->($req, $req->sessionInfo)B= $self->singleIPRule->($req, $req->sessionInfo)C= $req->{"sessionInfo"}{"ipAddr"} ne $session->data->{"ipAddr"}
ABCresult
1XX1
0111
00X0
0100
114 $self->singleSessionRule->( $req, $req->sessionInfo )
115 or ( $self->singleIPRule->( $req, $req->sessionInfo )
116 and $req->{sessionInfo}->{ipAddr} ne
117 $session->data->{ipAddr} )
118 )
119 {
120 6 push @$deleted, $self->p->_sumUpSession( $session->data );
121  
122 # Trigger back-channel logout before deletion
123 6 $self->p->_triggerBackChannelLogout( $req, $session->data );
124  
125 6 $self->p->_deleteSession( $req, $session, 1 );
126 }
127 else {
128 3 push @$otherSessions, $self->p->_sumUpSession( $session->data );
129 3 push @otherSessionsId, $id;
130 }
131 }
132 }
133  
134 $token = $self->ott->createToken( {
135 user => $req->{sessionInfo}->{ $self->conf->{whatToTrace} },
136 12 ▶ sessions => to_json( \@otherSessionsId )
Branch: 1 branch
BranchTrueFalseTotal
if @otherSessionsId 3 9 12
137 }
138 ) if @otherSessionsId;
139  
140 12 ▶ if ($singleUserByIPRuleMatched) {
Branch: 1 branch
BranchTrueFalseTotal
if ($singleUserByIPRuleMatched) 4 8 12
141 my $sessions =
142 $self->module->searchOn( $moduleOptions, 'ipAddr',
143 4 $req->sessionInfo->{ipAddr} );
144  
145 4 foreach my $id ( keys %$sessions ) {
146 6 ▶ next if $self->storageId( $req->id ) eq $id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->storageId($req->id) eq $id 4 2 6
147  
148 # searchOn() returns sessions indexed by their storage ID, then
149 # it is required to set hashStore to 0
150 2 ▶ my $session = $self->p->getApacheSession( $id, hashStore => 0, )
Branch: 1 branch
BranchTrueFalseTotal
my $session = $self->p->getApacheSession($id, "hashStore", 0) or next 2 0 2
151 or next;
152 2 ▶ unless ( $req->{sessionInfo}->{ $self->conf->{whatToTrace} } eq
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->{"sessionInfo"}{$self->conf->{"whatToTrace"}} eq $session->data->{$self->conf->{"whatToTrace"}}) 1 1 2
153 $session->data->{ $self->conf->{whatToTrace} } )
154 {
155 1 push @$deleted, $self->p->_sumUpSession( $session->data );
156  
157 # Trigger back-channel logout before deletion
158 1 $self->p->_triggerBackChannelLogout( $req, $session->data );
159  
160 1 $self->p->_deleteSession( $req, $session, 1 );
161 }
162 }
163 }
164  
165 $html = $self->p->mkSessionArray( $req, $deleted, 'sessionsDeleted', 1 )
166 12 ▶ if ( $self->conf->{notifyDeleted} and @$deleted );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"notifyDeleted"} and @$deleted 5 7 12
Condition66%2/3
$self->conf->{"notifyDeleted"} and @$deleted
!ll&&!rl&&r
075
MC/DC50%1/2
$self->conf->{"notifyDeleted"} and @$deleted
$self->conf->{"notifyDeleted"} @$deleted
Truth table66%2/3
A and B
A= $self->conf->{"notifyDeleted"}B= @$deleted
ABresult
0X0
100
111
167 $html .=
168 $self->p->mkSessionArray( $req, $otherSessions, 'otherSessions', 1 )
169 . $self->_mkRemoveOtherLink( $req, $token )
170 12 ▶ if ( $self->conf->{notifyOther} and @$otherSessions );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"notifyOther"} and @$otherSessions 1 11 12
Condition66%2/3
$self->conf->{"notifyOther"} and @$otherSessions
!ll&&!rl&&r
1101
MC/DC50%1/2
$self->conf->{"notifyOther"} and @$otherSessions
$self->conf->{"notifyOther"} @$otherSessions
Truth table66%2/3
A and B
A= $self->conf->{"notifyOther"}B= @$otherSessions
ABresult
0X0
100
111
171  
172 12 $req->info($html);
173 12 return PE_OK;
174 }
175  
176 0 ▶sub removeOther {
Subroutine removeOther: not called CC=8 SCAR=42.8
Pod undocumented
177 0 my ( $self, $req ) = @_;
178 0 my $res = PE_OK;
179 0 my $count = 0;
180 0 $req->{urldc} = decode_base64( $req->param('url') );
181  
182 0 ▶ if ( my $token = $req->param('token') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $req->param('token')) { } 0 0 0
183 0 ▶ if ( $token = $self->ott->getToken($token) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $self->ott->getToken($token)) { } 0 0 0
184  
185 # Read sessions from token
186 0 my $sessions = eval { from_json( $token->{sessions} ) };
187 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
188 0 $self->logger->error("Bad encoding in OTT: $@");
189 0 $res = PE_ERROR;
190 }
191 0 my $as;
192 0 foreach (@$sessions) {
193  
194 # searchOn() returns sessions indexed by their storage ID, then
195 # it is required to set hashStore to 0
196 0 ▶ unless ( $as =
Branch: 1 branch
BranchTrueFalseTotal
unless ($as = $self->p->getApacheSession($_, "hashStore", 0)) 0 0 0
197 $self->p->getApacheSession( $_, hashStore => 0, ) )
198 {
199 0 $self->userLogger->info(
200 "SingleSession: session $_ expired");
201 0 next;
202 }
203 0 my $user = $token->{user};
204 0 ▶ if ( $req->userData->{ $self->{conf}->{whatToTrace} } eq $user )
Branch: 1 branch
BranchTrueFalseTotal
if ($req->userData->{$self->{'conf'}{'whatToTrace'}} eq $user) { } 0 0 0
205 {
206 0 $self->userLogger->info("Remove \"$user\" session: $_");
207  
208 # Trigger back-channel logout before deletion
209 0 $self->p->_triggerBackChannelLogout( $req, $as->data );
210  
211 0 $self->p->_deleteSession( $req, $as, 1 );
212 0 $count++;
213 }
214 else {
215 0 $self->userLogger->warn(
216 "SingleSession called with an invalid token");
217 0 $res = PE_TOKENEXPIRED;
218 }
219 }
220 }
221 else {
222 0 $self->userLogger->error(
223 "SingleSession called with an expired token");
224 0 $res = PE_TOKENEXPIRED;
225 }
226 }
227 else {
228 0 $self->userLogger->error('SingleSession called without token');
229 0 $res = PE_NOTOKEN;
230 }
231  
232 0 ▶ return $self->p->doPE( $req, $res ) if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
233 0 $self->userLogger->info("$count remaining session(s) removed");
234 0 $req->mustRedirect(1);
235 0 return $self->p->autoRedirect($req);
236 }
237  
238 # Build the removeOther link
239 # Last part of URL is built trough javascript
240 # @return removeOther link in HTML code
241 1 ▶sub _mkRemoveOtherLink {
Subroutine _mkRemoveOtherLink: called CC=1 SCAR=0.0
242 1 my ( $self, $req, $token ) = @_;
243  
244 1 return $self->loadTemplate(
245 $req,
246 'removeOther',
247 params => {
248 link => $self->p->buildUrl(
249 $req->portal, "removeOther", { token => $token }
250 ),
251 }
252 );
253 }
254  
255 1 1;
« blib/lib/Lemonldap/NG/Portal/Lib/Choice.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/DBI.pm »