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

filter: Statementstmt 32.1% 37 / 115 Branchbran 6.2% 3 / 48 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 61.5% 8 / 13 Podpod 25.0% 1 / 4 totaltotal 22.6% 49 / 216 CC 36 SCAR 58.6
CC
36
Cov
22%
run
650.0
removeOther
72.0
storageId
6.0
CRAP 351.4
« blib/lib/Lemonldap/NG/Portal/Plugins/Register.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Password/Base.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(
9 PE_OK
10 PE_ERROR
11 PE_NOTOKEN
12 PE_TOKENEXPIRED
13 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '2.22.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 default => sub {
31 my $ott =
32 0 ▶ $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 $ott->timeout( $_[0]->conf->{formTimeout} );
34 0 return $ott;
35 }
36 1 );
37  
38 sub storageId {
39 0 ▶ my ( $self, $id ) = @_;
Subroutine storageId: not called CC=2 SCAR=17.9
Pod undocumented
40 0 ▶ return $self->conf->{hashedSessionStore} ? id2storage($id) : $id;
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'hashedSessionStore'} ? : 0 0 0
41 }
42  
43 sub init {
44 13 ▶ my ($self) = @_;
Subroutine init: called CC=4 SCAR=14.2
Pod undocumented
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 sub run {
65 0 ▶ my ( $self, $req ) = @_;
Subroutine run: not called CC=25 SCAR=64.8
Pod documented
66 0 my ( $linkedSessionId, $token, $html ) = ( '', '', '' );
67 0 my $deleted = [];
68 0 my $otherSessions = [];
69 0 my @otherSessionsId = ();
70  
71 0 ▶ my $moduleOptions = $self->conf->{globalStorageOptions} || {};
Condition0%0/2
$self->conf->{'globalStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'globalStorageOptions'} || {}
$self->conf->{'globalStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'globalStorageOptions'}
Aresult
11
00
72 0 $moduleOptions->{backend} = $self->conf->{globalStorage};
73  
74 0 my $singleSessionRuleMatched =
75 $self->singleSessionRule->( $req, $req->sessionInfo );
76 0 my $singleIPRuleMatched = $self->singleIPRule->( $req, $req->sessionInfo );
77 0 my $singleUserByIPRuleMatched =
78 $self->singleUserByIPRule->( $req, $req->sessionInfo );
79  
80 0 ▶ if ( $singleSessionRuleMatched
Branch: 1 branch
BranchTrueFalseTotal
if ($singleSessionRuleMatched or $singleIPRuleMatched or $self->conf->{"notifyOther"}) 0 0 0
Condition0%0/6
$singleSessionRuleMatched or $singleIPRuleMatched or $self->conf->{"notifyOther"}
l!l&&r!l&&!r
000
$singleSessionRuleMatched or $singleIPRuleMatched
l!l&&r!l&&!r
000
MC/DC0%0/3
$singleSessionRuleMatched or $singleIPRuleMatched or $self->conf->{"notifyOther"}
$singleSessionRuleMatched $singleIPRuleMatched $self->conf->{"notifyOther"}
Truth table0%0/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 0 );
89  
90 0 ▶ if ( $self->conf->{securedCookie} == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"securedCookie"} == 2) 0 0 0
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 0 my $storageId = $self->storageId( $req->id );
107 0 foreach my $id ( keys %$sessions ) {
108 0 ▶ next if $id eq $storageId;
Branch: 1 branch
BranchTrueFalseTotal
if $id eq $storageId 0 0 0
109 next
110 0 ▶ if $linkedSessionId and $id eq $self->storageId($linkedSessionId);
Branch: 1 branch
BranchTrueFalseTotal
if $linkedSessionId and $id eq $self->storageId($linkedSessionId) 0 0 0
Condition0%0/3
$linkedSessionId and $id eq $self->storageId($linkedSessionId)
!ll&&!rl&&r
000
MC/DC0%0/2
$linkedSessionId and $id eq $self->storageId($linkedSessionId)
$linkedSessionId $id eq $self->storageId($linkedSessionId)
Truth table0%0/3
A and B
A= $linkedSessionIdB= $id eq $self->storageId($linkedSessionId)
ABresult
0X0
100
111
111 0 ▶ my $session = $self->p->getApacheSession( $id, hashStore => 0 )
Branch: 1 branch
BranchTrueFalseTotal
my $session = $self->p->getApacheSession($id, "hashStore", 0) or next 0 0 0
112 or next;
113 0 ▶ 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'}) { } 0 0 0
Condition0%0/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
000
$self->singleIPRule->($req, $req->sessionInfo) and $req->{"sessionInfo"}{"ipAddr"} ne $session->data->{"ipAddr"}
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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 0 push @$deleted, $self->p->_sumUpSession( $session->data );
121 0 $self->p->_deleteSession( $req, $session, 1 );
122 }
123 else {
124 0 push @$otherSessions, $self->p->_sumUpSession( $session->data );
125 0 push @otherSessionsId, $id;
126 }
127 }
128 }
129  
130 $token = $self->ott->createToken( {
131 user => $req->{sessionInfo}->{ $self->conf->{whatToTrace} },
132 0 ▶ sessions => to_json( \@otherSessionsId )
Branch: 1 branch
BranchTrueFalseTotal
if @otherSessionsId 0 0 0
133 }
134 ) if @otherSessionsId;
135  
136 0 ▶ if ($singleUserByIPRuleMatched) {
Branch: 1 branch
BranchTrueFalseTotal
if ($singleUserByIPRuleMatched) 0 0 0
137 my $sessions =
138 $self->module->searchOn( $moduleOptions, 'ipAddr',
139 0 $req->sessionInfo->{ipAddr} );
140  
141 0 foreach my $id ( keys %$sessions ) {
142 0 ▶ next if $self->storageId( $req->id ) eq $id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->storageId($req->id) eq $id 0 0 0
143  
144 # searchOn() returns sessions indexed by their storage ID, then
145 # it is required to set hashStore to 0
146 0 ▶ my $session = $self->p->getApacheSession( $id, hashStore => 0, )
Branch: 1 branch
BranchTrueFalseTotal
my $session = $self->p->getApacheSession($id, "hashStore", 0) or next 0 0 0
147 or next;
148 0 ▶ unless ( $req->{sessionInfo}->{ $self->conf->{whatToTrace} } eq
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->{"sessionInfo"}{$self->conf->{"whatToTrace"}} eq $session->data->{$self->conf->{"whatToTrace"}}) 0 0 0
149 $session->data->{ $self->conf->{whatToTrace} } )
150 {
151 0 push @$deleted, $self->p->_sumUpSession( $session->data );
152 0 $self->p->_deleteSession( $req, $session, 1 );
153 }
154 }
155 }
156  
157 $html = $self->p->mkSessionArray( $req, $deleted, 'sessionsDeleted', 1 )
158 0 ▶ if ( $self->conf->{notifyDeleted} and @$deleted );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"notifyDeleted"} and @$deleted 0 0 0
Condition0%0/3
$self->conf->{"notifyDeleted"} and @$deleted
!ll&&!rl&&r
000
MC/DC0%0/2
$self->conf->{"notifyDeleted"} and @$deleted
$self->conf->{"notifyDeleted"} @$deleted
Truth table0%0/3
A and B
A= $self->conf->{"notifyDeleted"}B= @$deleted
ABresult
0X0
100
111
159 $html .=
160 $self->p->mkSessionArray( $req, $otherSessions, 'otherSessions', 1 )
161 . $self->_mkRemoveOtherLink( $req, $token )
162 0 ▶ if ( $self->conf->{notifyOther} and @$otherSessions );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"notifyOther"} and @$otherSessions 0 0 0
Condition0%0/3
$self->conf->{"notifyOther"} and @$otherSessions
!ll&&!rl&&r
000
MC/DC0%0/2
$self->conf->{"notifyOther"} and @$otherSessions
$self->conf->{"notifyOther"} @$otherSessions
Truth table0%0/3
A and B
A= $self->conf->{"notifyOther"}B= @$otherSessions
ABresult
0X0
100
111
163  
164 0 $req->info($html);
165 0 return PE_OK;
166 }
167  
168 sub removeOther {
169 0 ▶ my ( $self, $req ) = @_;
Subroutine removeOther: not called CC=8 SCAR=42.8
Pod undocumented
170 0 my $res = PE_OK;
171 0 my $count = 0;
172 0 $req->{urldc} = decode_base64( $req->param('url') );
173  
174 0 ▶ if ( my $token = $req->param('token') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $req->param('token')) { } 0 0 0
175 0 ▶ if ( $token = $self->ott->getToken($token) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $self->ott->getToken($token)) { } 0 0 0
176  
177 # Read sessions from token
178 0 my $sessions = eval { from_json( $token->{sessions} ) };
179 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
180 0 $self->logger->error("Bad encoding in OTT: $@");
181 0 $res = PE_ERROR;
182 }
183 0 my $as;
184 0 foreach (@$sessions) {
185  
186 # searchOn() returns sessions indexed by their storage ID, then
187 # it is required to set hashStore to 0
188 0 ▶ unless ( $as =
Branch: 1 branch
BranchTrueFalseTotal
unless ($as = $self->p->getApacheSession($_, "hashStore", 0)) 0 0 0
189 $self->p->getApacheSession( $_, hashStore => 0, ) )
190 {
191 0 $self->userLogger->info(
192 "SingleSession: session $_ expired");
193 0 next;
194 }
195 0 my $user = $token->{user};
196 0 ▶ if ( $req->{userData}->{ $self->{conf}->{whatToTrace} } eq
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{'userData'}{$self->{'conf'}{'whatToTrace'}} eq $user) { } 0 0 0
197 $user )
198 {
199 0 $self->userLogger->info("Remove \"$user\" session: $_");
200 0 $self->p->_deleteSession( $req, $as, 1 );
201 0 $count++;
202 }
203 else {
204 0 $self->userLogger->warn(
205 "SingleSession called with an invalid token");
206 0 $res = PE_TOKENEXPIRED;
207 }
208 }
209 }
210 else {
211 0 $self->userLogger->error(
212 "SingleSession called with an expired token");
213 0 $res = PE_TOKENEXPIRED;
214 }
215 }
216 else {
217 0 $self->userLogger->error('SingleSession called without token');
218 0 $res = PE_NOTOKEN;
219 }
220  
221 0 ▶ return $self->p->doPE( $req, $res ) if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
222 0 $self->userLogger->info("$count remaining session(s) removed");
223 0 $req->mustRedirect(1);
224 0 return $self->p->autoRedirect($req);
225 }
226  
227 # Build the removeOther link
228 # Last part of URL is built trough javascript
229 # @return removeOther link in HTML code
230 sub _mkRemoveOtherLink {
231 0 ▶ my ( $self, $req, $token ) = @_;
Subroutine _mkRemoveOtherLink: not called CC=1 SCAR=6.9
232  
233 0 return $self->loadTemplate(
234 $req,
235 'removeOther',
236 params => {
237 link => $self->p->buildUrl(
238 $req->portal, "removeOther", { token => $token }
239 ),
240 }
241 );
242 }
243  
244 1 1;
« blib/lib/Lemonldap/NG/Portal/Plugins/Register.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Password/Base.pm »