Lemonldap/NG/Handler/Lib/ServiceToken.pm

filter: Statementstmt 96.2% 51 / 53 Branchbran 81.8% 18 / 22 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 3 / 3 Podpod 0.0% 0 / 2 totaltotal 87.2% 82 / 94 CC 15 SCAR 26.1
CC
15
Cov
90%
fetchId
15.3
BEGIN
1.0
hideCookie
1.0
CRAP 13.6
« Lemonldap/NG/Handler/Main/Run.pm ↑ Summary Lemonldap/NG/Handler/Lib/DevOps.pm »
1 package Lemonldap::NG::Handler::Lib::ServiceToken;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '2.23.0';
6  
7 16 ▶sub fetchId {
Subroutine fetchId: called CC=15 SCAR=27.3
Pod undocumented
8 16 my ( $class, $req ) = @_;
9 16 my $token = $req->{env}->{HTTP_X_LLNG_TOKEN};
10 16 ▶ return $class->Lemonldap::NG::Handler::Main::fetchId($req)
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /\w+/ 0 16 16
11 unless ( $token =~ /\w+/ );
12 16 $class->logger->debug("Found token: $token");
13  
14 # Decrypt token
15 16 my $s = $class->tsv->{cipher}->decrypt($token);
16  
17 # Token format:
18 # time:_session_id:vhost1:vhost2:serviceHeader1=value1:serviceHeader2=value2,...
19 16 my ( $t, $_session_id, @vhosts ) = split /:/, $s;
20 16 $class->logger->debug("Found epoch: $t");
21 16 $class->logger->debug("Found _session_id: $_session_id");
22  
23 # Looking for service headers
24 16 my $vhost = $class->resolveAlias($req);
25 16 my ( %serviceHeaders, @vhostRegexp );
26 @vhosts = grep {
27 16 ▶ if (/^([\w\-]+)=(.+)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^([\w\-]+)=(.+)$/) { } 36 38 74
elsif (m[^/(.+)?/$]) { } 8 30 38
elsif (/^[\w.*%-]+$/) { } 30 0 30
28 36 $serviceHeaders{$1} = $2;
29 36 $class->logger->debug("Found service header: $1 => $2");
30 36 0;
31 }
32 elsif (m#^/(.+)?/$#) {
33 8 push @vhostRegexp, qr/$1/;
34 8 $class->logger->debug("Found VHost regexp: $1");
35 8 0;
36 }
37 elsif (/^[\w.*%-]+$/) {
38 30 $class->logger->debug("Found VHost: $_");
39 30 1;
40 }
41 else {
42 0 $class->logger->warn("Found a non valid VHost: $_");
43 0 0;
44 }
45 } @vhosts;
46  
47 # $_session_id and at least one vhost or RegExp
48 16 ▶ unless ( $_session_id and ( @vhosts or @vhostRegexp ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_session_id and @vhosts || @vhostRegexp) 2 14 16
Condition83%5/6
$_session_id and @vhosts || @vhostRegexp
!ll&&!rl&&r
0214
@vhosts or @vhostRegexp
l!l&&r!l&&!r
1222
MC/DC66%2/3
$_session_id and @vhosts || @vhostRegexp
$_session_id @vhosts @vhostRegexp
Truth table75%3/4
A and B or C
A= $_session_idB= @vhostsC= @vhostRegexp
ABCresult
0XX0
1000
11X1
1011
49 2 $class->auditLog(
50 $req,
51 message => 'Bad service token',
52 code => "INVALID_SERVICE_TOKEN",
53 );
54 2 ▶ $class->logger->debug(
Branch: 1 branch
BranchTrueFalseTotal
$_session_id ? : 2 0 2
55 $_session_id ? 'No VH or RegExp found' : 'No _session_id found' );
56 2 return 0;
57 }
58  
59 # Is vhost listed in token ?
60 14 ▶ if ( grep { $_ eq $vhost } @vhosts ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_ eq $vhost;} @vhosts) { } 8 6 14
elsif (grep {$vhost =~ /$_/;} @vhostRegexp) { } 3 3 6
61 8 $class->logger->debug( "$vhost found in VHosts list: " . join ', ',
62 @vhosts );
63 }
64 5 elsif ( grep { $vhost =~ $_ } @vhostRegexp ) {
65 3 $class->logger->debug( "$vhost matches a VHost regexp: " . join ', ',
66 @vhostRegexp );
67 }
68 else {
69 3 $class->auditLog(
70 $req,
71 message => (
72 "$vhost not allowed in token scope ("
73 . join( ', ', ( @vhostRegexp, @vhosts ) ) . ')'
74 ),
75 code => "INVALID_SERVICE_TOKEN_SCOPE",
76 vhostRegexp => \@vhostRegexp,
77 vhosts => \@vhosts,
78 );
79 3 return 0;
80 }
81  
82 # Is token in good interval ?
83 my $ttl =
84 ( $class->tsv->{serviceTokenTTL}->{$vhost} > 0 )
85 ? $class->tsv->{serviceTokenTTL}->{$vhost}
86 11 ▶ : $class->tsv->{handlerServiceTokenTTL};
Branch: 1 branch
BranchTrueFalseTotal
$class->tsv->{'serviceTokenTTL'}{$vhost} > 0 ? : 11 0 11
87 11 my $now = time;
88 11 ▶ unless ( $t <= $now and $t > $now - $ttl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t <= $now and $t > $now - $ttl) 4 7 11
Condition66%2/3
$t <= $now and $t > $now - $ttl
!ll&&!rl&&r
047
MC/DC50%1/2
$t <= $now and $t > $now - $ttl
$t <= $now $t > $now - $ttl
Truth table66%2/3
A and B
A= $t <= $nowB= $t > $now - $ttl
ABresult
0X0
100
111
89 4 $class->auditLog(
90 $req,
91 message => 'Expired service token',
92 code => "EXPIRED_SERVICE_TOKEN",
93 );
94 4 $class->logger->debug("VH: $vhost with ServiceTokenTTL: $ttl");
95 4 $class->logger->debug("TokenTime: $t / Time: $now");
96 4 return 0;
97 }
98  
99 # Send service headers to protected application if exist
100 7 ▶ if (%serviceHeaders) {
Branch: 1 branch
BranchTrueFalseTotal
if (%serviceHeaders) 6 1 7
101 6 $class->logger->info("Append service header(s)...");
102 6 $class->set_header_in( $req, %serviceHeaders );
103 }
104  
105 7 return $_session_id;
106 }
107  
108 # Hide ServiceToken to the protected application
109 7 ▶sub hideCookie {
Subroutine hideCookie: called CC=1 SCAR=0.0
Pod undocumented
110 7 my ( $class, $req ) = @_;
111 7 $class->logger->debug('Removing X_LLNG_TOKEN header');
112 7 $class->unset_header_in( $req, 'X_LLNG_TOKEN' );
113 7 $class->Lemonldap::NG::Handler::Main::hideCookie($req);
114 }
115  
116 3 1;
« Lemonldap/NG/Handler/Main/Run.pm ↑ Summary Lemonldap/NG/Handler/Lib/DevOps.pm »