blib/lib/Lemonldap/NG/Portal/Main/Issuer.pm

filter: Statementstmt 74.1% 129 / 174 Branchbran 55.5% 30 / 54 Conditioncond 50.0% 13 / 26 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 85.0% 17 / 20 Podpod 0.0% 0 / 9 totaltotal 63.3% 190 / 300 CC 41 SCAR 21.4
CC
41
Cov
68%
_forAuthUser
22.3
cleanAllPdata
12.0
cleanPdata
11.0
CRAP 8.5
« blib/lib/Lemonldap/NG/Portal/Plugins/History.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Register/LDAP.pm »
1 # Base package for simple issuers plugins
2 #
3 # Issuer should just implement a run() method that will be called only for
4 # authenticated users when PATH_INFO starts with issuerDBXXPath
5 #
6 # run() should just return a Lemonldap::NG::Portal::Main::Constants value. It
7 # is called using process() method (Lemonldap::NG::Portal::Main::Process)
8 package Lemonldap::NG::Portal::Main::Issuer;
9  
10 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 35 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 35 ▶use Lemonldap::NG::Common::FormEncode;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 35 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
18 PE_OK
19 PE_RENEWSESSION
20 PE_UPGRADESESSION
21 PE_ISSUERTIMEOUT
22 35 );
23  
24 35 extends 'Lemonldap::NG::Portal::Main::Plugin';
25  
26 35 our $VERSION = '2.23.0';
27  
28 # PROPERTIES
29  
30 35 has type => ( is => 'rw' );
31 35 has path => ( is => 'rw' );
32 35 has ipath => ( is => 'rw' );
33 has _ott => (
34 is => 'rw',
35 lazy => 1,
36 10 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
37 10 my $ott = $_[0]->{p}->loadModule('::Lib::OneTimeToken');
38 my $timeout = $_[0]->{conf}->{issuersTimeout}
39 10 ▶ // $_[0]->{conf}->{formTimeout};
Condition33%1/3
$_[0]{"conf"}{"issuersTimeout"} // $_[0]{"conf"}{"formTimeout"}
l!l&&r!l&&!r
1000
MC/DC0%0/2
$_[0]{"conf"}{"issuersTimeout"} // $_[0]{"conf"}{"formTimeout"}
$_[0]{"conf"}{"issuersTimeout"} $_[0]{"conf"}{"formTimeout"}
Truth table33%1/3
A // B
A= $_[0]{"conf"}{"issuersTimeout"}B= $_[0]{"conf"}{"formTimeout"}
ABresult
1X1
011
000
40 10 $ott->timeout($timeout);
41 10 return $ott;
42 }
43 35 );
44  
45 # INTERFACE
46  
47 # Only logout is called in normal use. Issuer that inherits from this
48 # package are called only by their path
49 112 ▶sub beforeLogout { 'logout' }
Subroutine beforeLogout: called CC=1 SCAR=0.0
Pod undocumented
50  
51 # INITIALIZATION
52  
53 112 ▶sub init {
Subroutine init: called CC=3 SCAR=11.6
Pod undocumented
54 112 my ($self) = @_;
55 112 ▶ if ( $self->conf->{forceGlobalStorageIssuerOTT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"forceGlobalStorageIssuerOTT"}) 0 112 112
56 0 $self->logger->debug(
57 "-> Issuer tokens will be stored into global storage");
58 0 $self->_ott->cache(undef);
59 }
60  
61 112 my $type = ref( $_[0] );
62 112 $type =~ s/.*:://;
63 112 $self->type($type);
64 112 ▶ if ( my $path = $self->conf->{"issuerDB${type}Path"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $self->conf->{"issuerDB${type}Path"}) { } 112 0 112
65 112 $path =~ s/^.*?(\w+).*?$/$1/;
66 112 $self->path($path);
67 112 $self->addUnauthRoute(
68 $path => { '*' => '_redirect' },
69 [ 'GET', 'POST' ]
70 );
71 112 $self->addAuthRoute(
72 $path => { '*' => "_forAuthUser" },
73 [ 'GET', 'POST' ]
74 );
75 }
76 else {
77 0 $self->logger->debug("No path declared for issuer $type. Skipping");
78 }
79 112 $self->ipath( 'issuerRequest' . $self->path );
80 }
81  
82 # RUNNING METHODS
83  
84 # Case 1: Unauthentified users are redirected to the main portal
85  
86 20 ▶sub _redirect {
Subroutine _redirect: called CC=7 SCAR=19.9
87 20 my ( $self, $req, @path ) = @_;
88 20 ▶ if ( $req->pdata->{issuerTs}
Branch: 1 branch
BranchTrueFalseTotal
if ($req->pdata->{"issuerTs"} and $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()) 0 20 20
Condition66%2/3
$req->pdata->{"issuerTs"} and $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()
!ll&&!rl&&r
1730
MC/DC0%0/2
$req->pdata->{"issuerTs"} and $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()
$req->pdata->{"issuerTs"} $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()
Truth table66%2/3
A and B
A= $req->pdata->{"issuerTs"}B= $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()
ABresult
0X0
100
111
89 and $req->pdata->{issuerTs} + $self->conf->{issuersTimeout} < time )
90 {
91 0 $self->cleanAllPdata($req);
92 }
93 20 my $restore;
94 my $ir;
95 20 ▶ unless ( $self->can('ssoMatch') and not $self->ssoMatch($req) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('ssoMatch') and not $self->ssoMatch($req)) { } 0 20 20
Condition66%2/3
$self->can("ssoMatch") and not $self->ssoMatch($req)
!ll&&!rl&&r
1370
MC/DC0%0/2
$self->can("ssoMatch") and not $self->ssoMatch($req)
$self->can("ssoMatch") not $self->ssoMatch($req)
Truth table66%2/3
A and not(B)
A= $self->can("ssoMatch")B= not $self->ssoMatch($req)
ABresult
0X0
100
111
96 20 $self->logger->debug("Unauth request to $self->{path} issuer");
97 20 $restore = 1;
98 20 $self->logger->debug('Processing _redirect');
99 20 ▶ if ( !$req->param("inProgress") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->param("inProgress")) 17 3 20
100 17 $req->pdata->{ $self->ipath } = $self->storeRequest($req);
101 }
102 20 $self->p->setHiddenFormValue( $req, "inProgress", 1, '', 0 );
103 20 $ir = $req->pdata->{ $self->ipath };
104 20 $req->pdata->{ $self->ipath . 'Path' } = \@path;
105 20 $self->logger->debug(
106 'Add ' . $self->ipath . ', ' . $self->ipath . 'Path in keepPdata' );
107 20 push @{ $req->pdata->{keepPdata} }, $self->ipath, $self->ipath . 'Path';
108 20 $req->{urldc} = $self->p->buildUrl( $req->portal, $self->path );
109 20 $req->pdata->{_url} = encode_base64( $req->urldc, '' );
110 20 $req->pdata->{issuerTs} = time;
111 }
112 else {
113 0 $self->logger->debug('Not seen as Issuer request, skipping');
114 }
115  
116 # TODO: launch normal process with 'run' at the end
117 return $self->p->do(
118 $req,
119 [
120 'controlUrl',
121 20 @{ $self->p->beforeAuth },
122 $self->p->authProcess,
123 20 @{ $self->p->betweenAuthAndData },
124 $self->p->sessionData,
125 20 @{ $self->p->afterData },
126 $self->p->validSession,
127 20 ▶ @{ $self->p->endAuth },
Branch: 1 branch
BranchTrueFalseTotal
$restore ? : 20 0 20
128 (
129 $restore
130 ? (
131 # Restore urldc if auth doesn't need to dial with browser
132 [ ref($self) . "::resumeProcess", $ir, @path ],
133 )
134 : ()
135 )
136 ]
137 );
138 }
139  
140 6 ▶sub resumeProcess {
Subroutine resumeProcess: called CC=2 SCAR=7.8
Pod undocumented
141 6 my ( $self, $req, $ir, @path ) = @_;
142  
143 6 $self->p->clearHiddenFormValue( $req, ['inProgress'] );
144 6 $self->cleanPdata($req);
145 6 ▶ if ($ir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ir) { } 6 0 6
146 6 $self->restoreRequest( $req, $ir );
147 6 return $self->run( $req, @path );
148 }
149 else {
150 0 $self->logger->error(
151 "Could not restore issuer context, redirecting to portal");
152 0 $req->menuError(PE_ISSUERTIMEOUT);
153 0 return PE_ISSUERTIMEOUT;
154 }
155 }
156  
157 # Case 3: authentified user, launch
158 48 ▶sub _forAuthUser {
Subroutine _forAuthUser: called CC=9 SCAR=31.0
159 48 my ( $self, $req, @path ) = @_;
160  
161 48 my $restore_failed;
162 48 ▶ if ( $req->pdata->{issuerTs}
Branch: 1 branch
BranchTrueFalseTotal
if ($req->pdata->{"issuerTs"} and $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()) 0 48 48
Condition33%1/3
$req->pdata->{"issuerTs"} and $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()
!ll&&!rl&&r
4800
MC/DC0%0/2
$req->pdata->{"issuerTs"} and $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()
$req->pdata->{"issuerTs"} $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()
Truth table33%1/3
A and B
A= $req->pdata->{"issuerTs"}B= $req->pdata->{"issuerTs"} + $self->conf->{"issuersTimeout"} < &CORE::GLOBAL::time()
ABresult
0X0
100
111
163 and $req->pdata->{issuerTs} + $self->conf->{issuersTimeout} < time )
164 {
165 0 $restore_failed = 1;
166 0 $self->cleanAllPdata($req);
167 }
168  
169 48 $self->logger->debug('Processing _forAuthUser');
170 48 ▶ if ( my $r = $req->pdata->{ $self->ipath } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $req->pdata->{$self->ipath}) 0 48 48
171 0 $self->logger->debug("Restoring request to $self->{path} issuer");
172 0 ▶ if ( $self->restoreRequest( $req, $r ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->restoreRequest($req, $r)) { } 0 0 0
173 0 @path = @{ $req->pdata->{ $self->ipath . 'Path' } }
174 0 ▶ if ( $req->pdata->{ $self->ipath . 'Path' } );
Branch: 1 branch
BranchTrueFalseTotal
if $req->pdata->{$self->ipath . 'Path'} 0 0 0
175  
176 # In case a confirm form is shown, we need it to POST on the
177 # current Path
178 0 $req->data->{confirmFormAction} = URI->new( $req->uri )->path;
179 }
180 else {
181 0 $restore_failed = 1;
182 0 $self->cleanAllPdata($req);
183 }
184 }
185  
186 48 ▶ if ( $restore_failed and !@path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restore_failed and not @path) 0 48 48
Condition33%1/3
$restore_failed and not @path
!ll&&!rl&&r
4800
MC/DC0%0/2
$restore_failed and not @path
$restore_failed not @path
Truth table33%1/3
A and not(B)
A= $restore_failedB= not @path
ABresult
0X0
100
111
187 0 $self->logger->error(
188 "Could not restore issuer context, redirecting to portal");
189 0 $req->menuError(PE_ISSUERTIMEOUT);
190 0 return $self->p->doPE( $req, PE_ISSUERTIMEOUT );
191 }
192  
193 # Clean pdata: keepPdata has been set, so pdata must be cleaned here
194 48 $self->logger->debug('Cleaning pdata');
195 48 $self->cleanPdata($req);
196  
197 48 ▶ $req->maybeNotBase64(1) if ( ref($self) =~ /::CAS$/ );
Branch: 1 branch
BranchTrueFalseTotal
if ref($self) =~ /::CAS$/ 30 18 48
198 48 $req->mustRedirect(1);
199 return $self->p->do(
200 $req,
201 [
202 'importHandlerData', 'controlUrl',
203 48 @{ $self->p->forAuthUser }, [ ref($self) . "::run", @path ],
204 ]
205 );
206 }
207  
208 0 ▶sub cleanAllPdata {
Subroutine cleanAllPdata: not called CC=3 SCAR=24.8
Pod undocumented
209 0 my ( $self, $req ) = @_;
210 0 foreach my $k ( keys %{ $req->pdata } ) {
211 0 ▶ if ( exists $req->pdata->{ $k . 'Path' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $req->pdata->{$k . 'Path'}) 0 0 0
212 0 $self->cleanPdata( $req, $k );
213 }
214 }
215 }
216  
217 54 ▶sub cleanPdata {
Subroutine cleanPdata: called CC=10 SCAR=24.0
Pod undocumented
218 54 my ( $self, $req, $path ) = @_;
219 54 ▶ $path ||= $self->ipath;
Condition33%1/3
$path ||= $self->ipath
l!l&&r!l&&!r
0540
MC/DC0%0/2
$path ||= $self->ipath
$path $self->ipath
Truth table33%1/3
A ||= B
A= $pathB= $self->ipath
ABresult
1X1
011
000
220 54 for my $s ( $path, $path . 'Path' ) {
221 108 ▶ if ( $req->pdata->{$s} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->pdata->{$s}) 12 96 108
222 12 $self->logger->debug("Removing $s key from pdata");
223 12 delete $req->pdata->{$s};
224 }
225 }
226 54 ▶ if ( $req->pdata->{keepPdata} and ref $req->pdata->{keepPdata} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->pdata->{"keepPdata"} and ref $req->pdata->{"keepPdata"}) 6 48 54
Condition66%2/3
$req->pdata->{"keepPdata"} and ref $req->pdata->{"keepPdata"}
!ll&&!rl&&r
4806
MC/DC50%1/2
$req->pdata->{"keepPdata"} and ref $req->pdata->{"keepPdata"}
$req->pdata->{"keepPdata"} ref $req->pdata->{"keepPdata"}
Truth table66%2/3
A and B
A= $req->pdata->{"keepPdata"}B= ref $req->pdata->{"keepPdata"}
ABresult
0X0
100
111
227 6 @{ $req->pdata->{keepPdata} } =
228 grep {
229 18 ▶ $_ ne $path
Branch: 1 branch
BranchTrueFalseTotal
$_ ne $path . 'Path' ? : 0 9 9
Condition60%3/5
$_ ne $path && ($_ ne $path . 'Path' ? 1 : $self->logger->debug("Removing $_ from keepPdata") && 0)
!ll&&!rl&&r
990
$self->logger->debug("Removing $_ from keepPdata") and 0
!ll
09
MC/DC0%0/2
$_ ne $path && ($_ ne $path . 'Path' ? 1 : $self->logger->debug("Removing $_ from keepPdata") && 0)
$_ ne $path ($_ ne $path . 'Path' ? 1 : $self->logger->debug("Removing $_ from keepPdata") && 0)
MC/DC0%0/1
$self->logger->debug("Removing $_ from keepPdata") and 0
$self->logger->debug("Removing $_ from keepPdata")
Truth table66%2/3
A && B
A= $_ ne $pathB= ($_ ne $path . 'Path' ? 1 : $self->logger->debug("Removing $_ from keepPdata") && 0)
ABresult
0X0
100
111
Truth table50%1/2
A
A= $self->logger->debug("Removing $_ from keepPdata")
Aresult
00
11
230 and $_ ne $path . 'Path'
231 ? 1
232 : ( $self->logger->debug("Removing $_ from keepPdata") and 0 )
233 6 } @{ $req->pdata->{keepPdata} };
234 delete $req->pdata->{keepPdata}
235 6 ▶ unless ( @{ $req->pdata->{keepPdata} } );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$req->pdata->{"keepPdata"};} 6 0 6
236 }
237 }
238  
239 17 ▶sub storeRequest {
Subroutine storeRequest: called CC=5 SCAR=16.1
Pod undocumented
240 17 my ( $self, $req ) = @_;
241 17 my $info = {};
242 17 $info->{content} = $req->content;
243 17 foreach ( keys %{ $req->env } ) {
244 344 ▶ next if $_ eq "psgi.errors";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "psgi.errors" 0 344 344
245 344 ▶ next if $_ eq "psgi.input";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "psgi.input" 3 341 344
246 341 ▶ $info->{$_} = $req->env->{$_} unless ( ref $req->env->{$_} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $req->env->{$_} 269 72 341
247 }
248 17 my $token = $self->_ott->createToken($info);
249 17 $self->logger->debug("Store issuer request as $token");
250 17 return $token;
251 }
252  
253 6 ▶sub restoreRequest {
Subroutine restoreRequest: called CC=6 SCAR=17.9
Pod undocumented
254 6 my ( $self, $req, $token ) = @_;
255 6 $self->logger->debug("Restore issuer request from $token");
256  
257 6 my $result = 0;
258 6 my $env = $self->_ott->getToken($token);
259 6 ▶ if ($env) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env) 6 0 6
260 6 $result = 1;
261 6 $self->logger->debug("Restoring request from $token");
262 6 ▶ if ( my $c = delete $env->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = delete $env->{"content"}) 3 3 6
263 3 $env->{'psgix.input.buffered'} = 0;
264 3 $env->{'psgi.input'} = IO::String->new($c);
265 }
266 6 $req->{env} = {};
267 6 foreach ( keys %$env ) {
268 $self->logger->debug(
269 123 ▶ "Restore $_" . ( ref $env->{$_} ? '' : "\t" . $env->{$_} ) );
Branch: 1 branch
BranchTrueFalseTotal
ref $env->{$_} ? : 3 120 123
270 123 ▶ $req->env->{$_} = $env->{$_} unless /^plack/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^plack/ 123 0 123
271 }
272 }
273 6 $req->{uri} = uri_unescape( $req->env->{REQUEST_URI} );
274 6 $req->{uri} =~ s|^//+|/|g;
275 6 return $result;
276 }
277  
278 0 ▶sub reAuth {
Subroutine reAuth: not called CC=2 SCAR=17.9
Pod undocumented
279 0 my ( $self, $req ) = @_;
280 0 my $cacheTag = $self->p->cacheTag;
281 $req->data->{customScript} =
282 qq'<script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/autoRenew.min.js?v=$cacheTag"></script>'
283 0 ▶ if ( $self->conf->{skipRenewConfirmation} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"skipRenewConfirmation"} 0 0 0
284  
285 0 my $portal_without_slash = $req->portal =~ s/\/+$//r;
286 $req->data->{_url} =
287 0 encode_base64( $portal_without_slash . $req->path_info, '' );
288 0 $req->pdata->{ $self->ipath } = $self->storeRequest($req);
289 0 push @{ $req->pdata->{keepPdata} }, $self->ipath, $self->ipath . 'Path';
290 0 $req->pdata->{issuerTs} = time;
291 0 return PE_RENEWSESSION;
292 }
293  
294 0 ▶sub upgradeAuth {
Subroutine upgradeAuth: not called CC=2 SCAR=17.9
Pod undocumented
295 0 my ( $self, $req ) = @_;
296 0 my $cacheTag = $self->p->cacheTag;
297 $req->data->{customScript} =
298 qq'<script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/autoRenew.min.js?v=$cacheTag"></script>'
299 0 ▶ if ( $self->conf->{skipUpgradeConfirmation} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"skipUpgradeConfirmation"} 0 0 0
300 0 my $portal_without_slash = $req->portal =~ s/\/+$//r;
301 $req->data->{_url} =
302 0 encode_base64( $portal_without_slash . $req->path_info, '' );
303 0 $req->pdata->{ $self->ipath } = $self->storeRequest($req);
304 0 push @{ $req->pdata->{keepPdata} }, $self->ipath, $self->ipath . 'Path';
305 0 $req->pdata->{issuerTs} = time;
306 0 return PE_UPGRADESESSION;
307 }
308  
309 35 1;
310 __END__
« blib/lib/Lemonldap/NG/Portal/Plugins/History.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Register/LDAP.pm »