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