Lemonldap/NG/Handler/Lib/ZimbraPreAuth.pm

filter: Statementstmt 95.7% 45 / 47 Branchbran 62.5% 5 / 8 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 78.9% 60 / 76 CC 10 SCAR 19.1
CC
10
Cov
85%
run
9.7
_buildZimbraPreAuthUrl
2.0
BEGIN
1.0
CRAP 6.7
« Lemonldap/NG/Handler/Lib/OAuth2.pm ↑ Summary Lemonldap/NG/Handler/ApacheMP2/Request.pm »
1 ##@file
2 # Zimbra preauthentication
3  
4 ##@class
5 # Zimbra preauthentication
6 #
7 # It will build Zimbra preauth URL
8  
9 package Lemonldap::NG::Handler::Lib::ZimbraPreAuth;
10  
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Digest::SHA qw(hmac_sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '2.23.0';
16  
17 # Overload main run method
18 2 ▶sub run {
Subroutine run: called CC=9 SCAR=22.7
Pod undocumented
19 2 my ( $class, $req ) = @_;
20 2 my ( $ret, $session ) = $class->Lemonldap::NG::Handler::Main::run($req);
21  
22 # Continue only if user is authorized
23 2 ▶ return $ret unless ( $ret == $class->OK );
Branch: 1 branch
BranchTrueFalseTotal
unless $ret == $class->OK 0 2 2
24  
25 # Get current URI
26 2 my $uri = $req->access_control_uri;
27  
28 # Get Zimbra parameters
29 2 my $localConfig = $class->localConfig;
30 2 my $zimbraPreAuthKey = $localConfig->{zimbraPreAuthKey};
31 2 ▶ my $zimbraAccountKey = $localConfig->{zimbraAccountKey} || 'uid';
Condition50%1/2
$localConfig->{'zimbraAccountKey'} || 'uid'
l!l
02
MC/DC0%0/1
$localConfig->{'zimbraAccountKey'} || 'uid'
$localConfig->{'zimbraAccountKey'}
Truth table50%1/2
A
A= $localConfig->{'zimbraAccountKey'}
Aresult
11
00
32 2 ▶ my $zimbraBy = $localConfig->{zimbraBy} || 'id';
Condition50%1/2
$localConfig->{'zimbraBy'} || 'id'
l!l
02
MC/DC0%0/1
$localConfig->{'zimbraBy'} || 'id'
$localConfig->{'zimbraBy'}
Truth table50%1/2
A
A= $localConfig->{'zimbraBy'}
Aresult
11
00
33 2 ▶ my $zimbraUrl = $localConfig->{zimbraUrl} || '/service/preauth';
Condition50%1/2
$localConfig->{'zimbraUrl'} || '/service/preauth'
l!l
20
MC/DC0%0/1
$localConfig->{'zimbraUrl'} || '/service/preauth'
$localConfig->{'zimbraUrl'}
Truth table50%1/2
A
A= $localConfig->{'zimbraUrl'}
Aresult
11
00
34 2 ▶ my $zimbraSsoUrl = $localConfig->{zimbraSsoUrl} || '^/zimbrasso$';
Condition50%1/2
$localConfig->{'zimbraSsoUrl'} || '^/zimbrasso$'
l!l
20
MC/DC0%0/1
$localConfig->{'zimbraSsoUrl'} || '^/zimbrasso$'
$localConfig->{'zimbraSsoUrl'}
Truth table50%1/2
A
A= $localConfig->{'zimbraSsoUrl'}
Aresult
11
00
35 2 ▶ my $timeout = $localConfig->{'timeout'} || '0';
Condition50%1/2
$localConfig->{'timeout'} || '0'
l!l
02
MC/DC0%0/1
$localConfig->{'timeout'} || '0'
$localConfig->{'timeout'}
Truth table50%1/2
A
A= $localConfig->{'timeout'}
Aresult
11
00
36  
37 # Remove trailing white-spaces
38 2 $zimbraAccountKey =~ s/\s+$//;
39 2 $zimbraBy =~ s/\s+$//;
40 2 $zimbraUrl =~ s/\s+$//;
41 2 $zimbraSsoUrl =~ s/\s+$//;
42  
43 # Display found values in debug mode
44 2 $class->logger->debug("zimbraPreAuthKey: $zimbraPreAuthKey");
45 2 $class->logger->debug("zimbraAccountKey: $zimbraAccountKey");
46 2 $class->logger->debug("zimbraBy: $zimbraBy");
47 2 $class->logger->debug("zimbraUrl: $zimbraUrl");
48 2 $class->logger->debug("zimbraSsoUrl: $zimbraSsoUrl");
49 2 $class->logger->debug("timeout: $timeout");
50  
51 # Return if we are not on a Zimbra SSO URI
52 2 ▶ return $class->OK unless ( $uri =~ $zimbraSsoUrl );
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ /$zimbraSsoUrl/ 1 1 2
53  
54 # Check mandatory parameters
55 1 ▶ unless ($zimbraPreAuthKey) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($zimbraPreAuthKey) 0 1 1
56 0 $class->logger->error("No Zimbra preauth key configured");
57 0 return $class->SERVER_ERROR;
58 }
59  
60 # Build URL
61 my $zimbra_url =
62 $class->_buildZimbraPreAuthUrl( $req, $zimbraPreAuthKey, $zimbraUrl,
63 1 $class->data->{$zimbraAccountKey},
64 $zimbraBy, $timeout );
65  
66 # Header location
67 1 $class->set_header_out( $req, 'Location' => $zimbra_url );
68  
69 1 return $class->REDIRECT;
70 }
71  
72 ## @method private string _buildZimbraPreAuthUrl(string key, string url, string account, string by, int timeout)
73 # Build Zimbra PreAuth URL
74 # @param key PreAuthKey
75 # @param url URL
76 # @param account User account
77 # @param by Account type
78 # @param timeout Timout
79 # @return Zimbra PreAuth URL
80 1 ▶sub _buildZimbraPreAuthUrl {
Subroutine _buildZimbraPreAuthUrl: called CC=2 SCAR=7.0
81 1 my ( $class, $req, $key, $url, $account, $by, $timeout ) = @_;
82  
83 # Expiration time is calculated with _utime and timeout
84 my $expires =
85 1 ▶ $timeout ? ( $class->data->{_utime} + $timeout ) * 1000 : $timeout;
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 0 1 1
86  
87 # Timestamp
88 1 my $timestamp = time() * 1000;
89  
90 # Compute preauth value
91 1 my $computed_value =
92 hmac_sha1_hex( "$account|$by|$expires|$timestamp", $key );
93  
94 1 $class->logger->debug(
95 "Compute value $account|$by|$expires|$timestamp into $computed_value");
96  
97 # Build PreAuth URL
98 1 my $zimbra_url =
99 "$url?account=$account&by=$by&timestamp=$timestamp&expires=$expires&preauth=$computed_value";
100  
101 1 $class->logger->debug("Build Zimbra URL: $zimbra_url");
102  
103 1 return $zimbra_url;
104 }
105  
106 2 1;
« Lemonldap/NG/Handler/Lib/OAuth2.pm ↑ Summary Lemonldap/NG/Handler/ApacheMP2/Request.pm »