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
|
|
|
|
|
|
|
# This specific handler is intended to be called directly by Apache
|
|
|
|
|
2014-05-23 12:23:51 +02:00
|
|
|
package Lemonldap::NG::Handler::Specific::ZimbraPreAuth;
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
use strict;
|
2016-03-17 23:19:44 +01:00
|
|
|
use Lemonldap::NG::Handler::ApacheMP2 qw(:all);
|
|
|
|
use base qw(Lemonldap::NG::Handler::ApacheMP2);
|
2014-03-13 16:07:36 +01:00
|
|
|
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
|
|
|
|
2016-02-25 08:26:31 +01:00
|
|
|
sub handler {
|
|
|
|
my ( $class, $request ) = ( __PACKAGE__, shift );
|
|
|
|
$class->run($request);
|
|
|
|
}
|
|
|
|
|
2014-03-13 16:07:36 +01:00
|
|
|
## @rmethod Apache2::Const run(Apache2::RequestRec r)
|
|
|
|
# Overload main run method
|
|
|
|
# @param r Current request
|
2016-03-17 23:19:44 +01:00
|
|
|
# @return Apache2::Const value ($class->OK, $class->FORBIDDEN, $class->REDIRECT or $class->SERVER_ERROR)
|
2014-03-13 16:07:36 +01:00
|
|
|
sub run {
|
|
|
|
my $class = shift;
|
|
|
|
my $r = $_[0];
|
2016-02-26 14:23:15 +01:00
|
|
|
my $ret = $class->SUPER::run();
|
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
|
2016-03-17 23:19:44 +01:00
|
|
|
my $uri = $class->uri_with_args($r);
|
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
|
2016-04-29 09:27:26 +02:00
|
|
|
$class->lmLog( "zimbraPreAuthKey: $zimbraPreAuthKey", 'debug' );
|
|
|
|
$class->lmLog( "zimbraAccountKey: $zimbraAccountKey", 'debug' );
|
|
|
|
$class->lmLog( "zimbraBy: $zimbraBy", 'debug' );
|
|
|
|
$class->lmLog( "zimbraUrl: $zimbraUrl", 'debug' );
|
|
|
|
$class->lmLog( "zimbraSsoUrl: $zimbraSsoUrl", 'debug' );
|
|
|
|
$class->lmLog( "timeout: $timeout", 'debug' );
|
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) {
|
2016-04-29 09:27:26 +02:00
|
|
|
$class->lmLog( "No Zimbra preauth key configured", 'error' );
|
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 =
|
|
|
|
$class->_buildZimbraPreAuthUrl( $zimbraPreAuthKey, $zimbraUrl,
|
2016-03-17 23:19:44 +01:00
|
|
|
$class->datas->{$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 {
|
2016-02-26 14:23:15 +01:00
|
|
|
my ( $class, $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 =
|
|
|
|
$timeout ? ( $class->datas->{_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 );
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
$class->lmLog(
|
2014-03-13 16:07:36 +01:00
|
|
|
"Compute value $account|$by|$expires|$timestamp into $computed_value",
|
|
|
|
'debug' );
|
|
|
|
|
|
|
|
# Build PreAuth URL
|
|
|
|
my $zimbra_url =
|
|
|
|
"$url?account=$account&by=$by×tamp=$timestamp&expires=$expires&preauth=$computed_value";
|
|
|
|
|
2016-04-29 09:27:26 +02:00
|
|
|
$class->lmLog( "Build Zimbra URL: $zimbra_url", 'debug' );
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
return $zimbra_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
__PACKAGE__->init( {} );
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
=encoding utf8
|
|
|
|
|
|
|
|
Lemonldap::NG::Handler::ZimbraPreAuth - Perl extension to generate Zimbra preauth URL
|
|
|
|
for users authenticated by Lemonldap::NG
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
Edit you Zimbra vhost configuration like this:
|
|
|
|
|
2016-02-26 14:23:15 +01:00
|
|
|
PerlModule Lemonldap::NG::Handler::Specific::ZimbraPreAuth
|
2014-03-13 16:07:36 +01:00
|
|
|
<VirtualHost *>
|
|
|
|
ServerName zimbra.example.com
|
|
|
|
|
|
|
|
# Load Zimbra Handler
|
2016-02-26 14:23:15 +01:00
|
|
|
PerlHeaderParserHandler Lemonldap::NG::Handler::Specific::ZimbraPreAuth
|
2014-03-13 16:07:36 +01:00
|
|
|
|
|
|
|
</VirtualHost>
|
|
|
|
|
|
|
|
=head2 EXPORT
|
|
|
|
|
|
|
|
See L<Lemonldap::NG::Handler>
|
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
L<http://wiki.zimbra.com/wiki/Preauth>
|
|
|
|
L<Lemonldap::NG::Handler>
|
|
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
|
|
=item Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>
|
|
|
|
|
|
|
|
=item Xavier Guimard, E<lt>x.guimard@free.frE<gt>
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
=head1 BUG REPORT
|
|
|
|
|
|
|
|
Use OW2 system to report bug or ask for features:
|
|
|
|
L<http://jira.ow2.org>
|
|
|
|
|
|
|
|
=head1 DOWNLOAD
|
|
|
|
|
|
|
|
Lemonldap::NG is available at
|
|
|
|
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>
|
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
|
|
=item Copyright (C) 2010 by Xavier Guimard, E<lt>x.guimard@free.frE<gt>
|
|
|
|
|
|
|
|
=item Copyright (C) 2010, 2012 by Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see L<http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
=cut
|