| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$_session_id and @vhosts || @vhostRegexp
@vhosts or @vhostRegexp
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 11 | my $now = time; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 11 | ▶ | unless ( $t <= $now and $t > $now - $ttl ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$t <= $now and $t > $now - $ttl
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||