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;
|
|
|
|
use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex);
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
our $VERSION = '2.0.0';
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
# Overload main run method
|
|
|
|
sub run {
|
2017-02-15 07:41:50 +01:00
|
|
|
my ( $class, $req ) = @_;
|
2018-06-01 17:22:27 +02:00
|
|
|
my $ret = $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';
|
|
|
|
my $zimbraBy = $localConfig->{zimbraBy} || 'id';
|
|
|
|
my $zimbraUrl = $localConfig->{zimbraUrl} || '/service/preauth';
|
|
|
|
my $zimbraSsoUrl = $localConfig->{zimbraSsoUrl} || '^/zimbrasso$';
|
|
|
|
my $timeout = $localConfig->{'timeout'} || '0';
|
|
|
|
|
|
|
|
# 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
|
2016-02-26 14:23:15 +01:00
|
|
|
Lemonldap::NG::Handler::API->set_header_out( 'Location' => $zimbra_url );
|
2014-03-13 16:07:36 +01:00
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
# Return $class->REDIRECT
|
|
|
|
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;
|