2014-03-13 16:07:36 +01:00
|
|
|
##@file
|
|
|
|
# Zimbra preauthentication
|
|
|
|
|
|
|
|
##@class
|
|
|
|
# Zimbra preauthentication
|
|
|
|
#
|
|
|
|
# It will build Zimbra preauth URL
|
2014-03-20 19:40:26 +01:00
|
|
|
|
2017-02-11 08:47:22 +01:00
|
|
|
package Lemonldap::NG::Handler::Lib::ZimbraPreAuth;
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
use strict;
|
2020-12-16 22:52:28 +01:00
|
|
|
use URI::Escape;
|
2019-09-20 22:47:48 +02:00
|
|
|
use Digest::HMAC_SHA1 qw(hmac_sha1_hex);
|
2014-03-13 16:07:36 +01:00
|
|
|
|
2019-09-27 22:23:54 +02:00
|
|
|
our $VERSION = '2.0.7';
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Overload main run method
|
|
|
|
sub run {
|
2019-09-27 22:23:54 +02:00
|
|
|
my ( $class, $req ) = @_;
|
|
|
|
my ( $ret, $session ) = $class->Lemonldap::NG::Handler::Main::run($req);
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Continue only if user is authorized
|
2016-03-17 23:19:44 +01:00
|
|
|
return $ret unless ( $ret == $class->OK );
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Get current URI
|
2017-03-28 23:07:49 +02:00
|
|
|
my $uri = $req->{env}->{REQUEST_URI};
|
2014-03-13 16:07:36 +01:00
|
|
|
|
2016-02-26 14:23:15 +01:00
|
|
|
# Get Zimbra parameters
|
2016-03-17 23:19:44 +01:00
|
|
|
my $localConfig = $class->localConfig;
|
2016-02-26 14:23:15 +01:00
|
|
|
my $zimbraPreAuthKey = $localConfig->{zimbraPreAuthKey};
|
|
|
|
my $zimbraAccountKey = $localConfig->{zimbraAccountKey} || 'uid';
|
2022-02-16 17:43:29 +01:00
|
|
|
my $zimbraBy = $localConfig->{zimbraBy} || 'id';
|
|
|
|
my $zimbraUrl = $localConfig->{zimbraUrl} || '/service/preauth';
|
2016-02-26 14:23:15 +01:00
|
|
|
my $zimbraSsoUrl = $localConfig->{zimbraSsoUrl} || '^/zimbrasso$';
|
2022-02-16 17:43:29 +01:00
|
|
|
my $timeout = $localConfig->{'timeout'} || '0';
|
2016-02-26 14:23:15 +01:00
|
|
|
|
2020-12-16 22:52:28 +01:00
|
|
|
# Remove trailing white-spaces
|
|
|
|
$zimbraAccountKey =~ s/\s+$//;
|
|
|
|
$zimbraBy =~ s/\s+$//;
|
|
|
|
$zimbraUrl =~ s/\s+$//;
|
|
|
|
$zimbraSsoUrl =~ s/\s+$//;
|
|
|
|
|
2016-02-26 14:23:15 +01:00
|
|
|
# Display found values in debug mode
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("zimbraPreAuthKey: $zimbraPreAuthKey");
|
|
|
|
$class->logger->debug("zimbraAccountKey: $zimbraAccountKey");
|
|
|
|
$class->logger->debug("zimbraBy: $zimbraBy");
|
|
|
|
$class->logger->debug("zimbraUrl: $zimbraUrl");
|
|
|
|
$class->logger->debug("zimbraSsoUrl: $zimbraSsoUrl");
|
|
|
|
$class->logger->debug("timeout: $timeout");
|
2016-02-26 14:23:15 +01:00
|
|
|
|
2014-03-13 16:07:36 +01:00
|
|
|
# Return if we are not on a Zimbra SSO URI
|
2016-03-17 23:19:44 +01:00
|
|
|
return $class->OK unless ( $uri =~ $zimbraSsoUrl );
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Check mandatory parameters
|
2016-02-26 14:23:15 +01:00
|
|
|
unless ($zimbraPreAuthKey) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->error("No Zimbra preauth key configured");
|
2016-03-17 23:19:44 +01:00
|
|
|
return $class->SERVER_ERROR;
|
2016-02-26 14:23:15 +01:00
|
|
|
}
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Build URL
|
2016-02-26 14:23:15 +01:00
|
|
|
my $zimbra_url =
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->_buildZimbraPreAuthUrl( $req, $zimbraPreAuthKey, $zimbraUrl,
|
2018-07-05 22:56:16 +02:00
|
|
|
$class->data->{$zimbraAccountKey},
|
2016-02-26 14:23:15 +01:00
|
|
|
$zimbraBy, $timeout );
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Header location
|
2019-09-27 22:09:04 +02:00
|
|
|
$class->set_header_out( $req, 'Location' => $zimbra_url );
|
2014-03-13 16:07:36 +01:00
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
return $class->REDIRECT;
|
2014-03-13 16:07:36 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 14:23:15 +01:00
|
|
|
## @method private string _buildZimbraPreAuthUrl(string key, string url, string account, string by, int timeout)
|
2014-03-13 16:07:36 +01:00
|
|
|
# Build Zimbra PreAuth URL
|
|
|
|
# @param key PreAuthKey
|
|
|
|
# @param url URL
|
|
|
|
# @param account User account
|
|
|
|
# @param by Account type
|
2016-02-26 14:23:15 +01:00
|
|
|
# @param timeout Timout
|
2014-03-13 16:07:36 +01:00
|
|
|
# @return Zimbra PreAuth URL
|
|
|
|
sub _buildZimbraPreAuthUrl {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $key, $url, $account, $by, $timeout ) = @_;
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Expiration time is calculated with _utime and timeout
|
2016-04-29 09:27:26 +02:00
|
|
|
my $expires =
|
2018-07-05 22:56:16 +02:00
|
|
|
$timeout ? ( $class->data->{_utime} + $timeout ) * 1000 : $timeout;
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Timestamp
|
|
|
|
my $timestamp = time() * 1000;
|
|
|
|
|
|
|
|
# Compute preauth value
|
|
|
|
my $computed_value =
|
|
|
|
hmac_sha1_hex( "$account|$by|$expires|$timestamp", $key );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug(
|
|
|
|
"Compute value $account|$by|$expires|$timestamp into $computed_value");
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Build PreAuth URL
|
|
|
|
my $zimbra_url =
|
|
|
|
"$url?account=$account&by=$by×tamp=$timestamp&expires=$expires&preauth=$computed_value";
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Build Zimbra URL: $zimbra_url");
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
return $zimbra_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|