Lemonldap/NG/Handler/Lib/CDA.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 74 CC 8 SCAR 35.3
CC
8
Cov
0%
run
56.0
getCDAInfos
6.0
BEGIN
2.0
CRAP 34.2
1 package Lemonldap::NG::Handler::Lib::CDA;
2  
3 use strict;
4 use URI;
5 use URI::QueryParam;
6  
7 our $VERSION = '2.19.0';
8  
9 sub run {
10 my ( $class, $req, $rule, $protection ) = @_;
11  
12 # Use the routed URI: an encoded '?' would hide the CDA parameter here
13 my $uri = $req->access_control_uri;
14 my $cn = $class->tsv->{cookieName};
15 my ( $id, $session );
16 if ( $uri =~ m/[\?&;]${cn}cda=(\w+)/i ) {
17 if ( $id = $class->fetchId($req)
18 and $session = $class->retrieveSession( $req, $id ) )
19 {
20 $class->logger->info(
21 'CDA asked for an already available session, skipping');
22 }
23 else {
24 # Extract CDA code from URI
25 my $u = URI->new( $req->uri );
26 my $cdaid = $u->query_param("${cn}cda");
27  
28 # Remove CDA param from URI
29 $u->query_param_delete("${cn}cda");
30  
31 $class->logger->debug("CDA request with id $cdaid");
32  
33 my $cdaInfos = $class->getCDAInfos( $req, $cdaid );
34 unless ( $cdaInfos->{cookie_value} and $cdaInfos->{cookie_name} ) {
35 $class->logger->error("CDA request for id $cdaid is not valid");
36 return $class->FORBIDDEN;
37 }
38  
39 my $redirectUrl = $class->_buildUrl( $req, $u->path_query );
40 my $redirectHttps = ( $redirectUrl =~ m/^https/ );
41 $class->set_header_out(
42 $req,
43 'Location' => $redirectUrl,
44 'Set-Cookie' => $cdaInfos->{cookie_name} . "=" . 'c:'
45 . $class->tsv->{cipher}->encrypt(
46 $cdaInfos->{cookie_value} . ' ' . $class->resolveAlias($req)
47 )
48 . "; path=/"
49 . ( $redirectHttps ? "; secure" : "" )
50 . ( $class->tsv->{httpOnly} ? "; HttpOnly" : "" )
51 . (
52 $class->tsv->{cookieExpiration}
53 ? "; max-age=" . $class->tsv->{cookieExpiration}
54 : ""
55 )
56 );
57 $req->data->{'noTry'} = 1;
58 return $class->REDIRECT;
59 }
60 }
61 return $class->Lemonldap::NG::Handler::Main::run( $req, $rule,
62 $protection );
63 }
64  
65 ## @rmethod protected hash getCDAInfos(id)
66 # Tries to retrieve the CDA session, get infos and delete session
67 # @return CDA session infos
68 sub getCDAInfos {
69 my ( $class, $req, $id ) = @_;
70 my $infos = {};
71  
72 # Get the session
73 my $cdaSession = Lemonldap::NG::Common::Session->new( {
74 hashStore => $class->tsv->{hashedSessionStore},
75 storageModule => $class->tsv->{sessionStorageModule},
76 storageModuleOptions => $class->tsv->{sessionStorageOptions},
77 cacheModule => $class->tsv->{sessionCacheModule},
78 cacheModuleOptions => $class->tsv->{sessionCacheOptions},
79 id => $id,
80 kind => "CDA",
81 }
82 );
83  
84 unless ( $cdaSession->error ) {
85 $class->logger->debug("Get CDA session $id");
86  
87 $infos->{cookie_value} = $cdaSession->data->{cookie_value};
88 $infos->{cookie_name} = $cdaSession->data->{cookie_name};
89  
90 $cdaSession->remove;
91 }
92 else {
93 $class->logger->info("CDA Session $id can't be retrieved");
94 $class->logger->info( $cdaSession->error );
95 }
96  
97 return $infos;
98 }
99  
100 1;