Apache2-AuthCookieLDAP

 view release on metacpan or  search on metacpan

lib/Apache2/AuthCookieLDAP.pm  view on Meta::CPAN

package Apache2::AuthCookieLDAP;

# Apache2::AuthCookieLDAP
#
# An Apache2::AuthCookie backend for LDAP based authentication
#
# Author: Kirill Solomko <ksolomko@cpan.org>

use strict;
use warnings;
use 5.010_000;
our $VERSION = '1.15';

use Apache2::AuthCookie;
use base qw(Apache2::AuthCookie);

use Apache2::Connection;
use Apache2::RequestRec;
use Apache2::Log;
use Apache2::Const -compile => qw(:log);
use APR::Const -compile     => qw(:error ENOTIME SUCCESS);

use English qw(-no_match_vars);

use Digest::MD5 qw(md5_hex);
use Crypt::CBC;
use Crypt::DES;

use Net::LDAP;

use constant CIPHER_TYPES => qw(DES IDEA Blowfish Blowfish_PP);
use constant LOG_LEVELS   => {
    emerg  => Apache2::Const::LOG_EMERG,
    alert  => Apache2::Const::LOG_ALERT,
    crit   => Apache2::Const::LOG_CRIT,
    err    => Apache2::Const::LOG_ERR,
    warn   => Apache2::Const::LOG_WARNING,
    notice => Apache2::Const::LOG_NOTICE,
    info   => Apache2::Const::LOG_INFO,
    debug  => Apache2::Const::LOG_DEBUG
};

use constant NULL               => q{};
use constant C_SECRET_KEY       => '_SecretKey';
use constant C_SESSION_LIFETIME => '_SessionLifetime';
use constant C_LDAPURI          => '_LDAPURI';
use constant C_BASE             => '_Base';
use constant C_BINDDN           => '_BindDN';
use constant C_BINDPW           => '_BindPW';
use constant C_FILTER           => '_Filter';
use constant C_CIPHER           => '_Cipher';
use constant C_DEBUG            => '_Debug';
use constant C_DEBUG_LOGLEVEL   => '_DebugLogLevel';
use constant C_ERROR_LOGLEVEL   => '_ErrorLogLevel';

my %CONFIG_DEFAULT = (
    C_SECRET_KEY,     undef,          C_SESSION_LIFETIME, '00-24-00-00',
    C_LDAPURI,        undef,          C_BASE,             undef,
    C_BINDDN,         undef,          C_BINDPW,           undef,
    C_FILTER,         '(uid=%USER%)', C_CIPHER,           'des',
    C_DEBUG_LOGLEVEL, 'alert',        C_ERROR_LOGLEVEL,   'err',
    C_DEBUG,          0,
);

my $ldap_handler;
my %config_data;
my %ciphers;
my $DEBUG = C_DEBUG;

#----------------------------------------------------------------------
sub cipher {
    my ( $self, $r ) = @_;

    my $auth_name  = $r->auth_name;
    my $cipher     = $self->config( $r, C_CIPHER );
    my $cipher_key = $auth_name . ':' . lc($cipher);

    unless ( exists $ciphers{$cipher_key} ) {
        my $secret_key = $self->config( $r, C_SECRET_KEY );
        foreach my $cipher_type (CIPHER_TYPES) {
            next unless lc($cipher_type) eq $cipher;
            $ciphers{$cipher_key} = Crypt::CBC->new(
                -key    => $secret_key,
                -cipher => $cipher_type
            );
        }
    }
    exists $ciphers{$cipher_key}
      ? return $ciphers{$cipher_key}
      : $self->fatal( $r, "Wrong cipher $cipher" );
    return NULL;
}

sub config {
    my ( $self, $r, $req_key ) = @_;

    return unless defined $req_key;

    if ( keys %config_data ) {
        exists $config_data{$req_key}
          ? return $config_data{$req_key}
          : return NULL;
    }

    my $auth_name = $r->auth_name;
    foreach my $key ( keys %CONFIG_DEFAULT ) {
        my $default = $CONFIG_DEFAULT{$key};
        my $var     = $r->dir_config( $auth_name . $key );
        $config_data{$key} = defined $var ? $var : $default;
        if ( $key eq C_DEBUG ) {
            $DEBUG = $config_data{$key};
        }
    }

    foreach my $key ( ( C_DEBUG_LOGLEVEL, C_ERROR_LOGLEVEL ) ) {
        my $value = $config_data{$key};
        my $replace = $key eq C_DEBUG_LOGLEVEL ? 'alert' : 'err';

lib/Apache2/AuthCookieLDAP.pm  view on Meta::CPAN

          && $self->rlog( $r, "Expiration time has passed for user '$user'" );
        return;
    }

    my $session_data = $enc_user . ':' . $session_time;
    unless ( $hash eq $self->create_hash( $r, $session_data ) ) {
        $DEBUG
          && $self->rlog( $r, "Session hash does not match for user '$user'" );
        return;
    }

    return $user;
}

1;

=pod

=head1 NAME

Apache2::AuthCookieLDAP - An Apache2::AuthCookie backend for LDAP based authentication

=head1 VERSION

Version 1.15

=head1 COMPATIBILITY

The version is compatible with Apache2 and mod_perl2

=head1 SYNOPSIS

1. Make sure that your LDAP server is configured and you have access to it 

2.  In httpd.conf or .htaccess

Apache2::AuthCookie config (check L<Apache2::AuthCookie> documentation for the additional info)

    PerlSetVar MyAuthPath /
    PerlSetVar MyAuthLoginScript /
    PerlSetVar MyAuthLogoutURL http://127.0.0.1
    PerlSetVar MyAuthSecure 1

To make "LogoutURL" working you can subsclass Apache2::ApacheCookieLDAP and provide it with:

    sub logout {
        my ( $self, $r ) = @_;
        $self->SUPER::logout($r);
        my $logout_url = $r->dir_config( $r->auth_name . 'LogoutURL' );
        if ($logout_url) {
            $r->headers_out->set( Location => $logout_url );
            $r->status(Apache2::Const::REDIRECT);
        }

        return Apache2::Const::REDIRECT;
    }
  
Apache2::AuthCookieLDAP config

    PerlSetVar MyAuth_SecretKey OGheSWkT1ixd4V0DydSarLVevF77sSibMIoUaIYuQUqp2zvZIwbS4lyWhRTFUcHE
    PerlSetVar MyAuth_SessionLifetime 00-24-00-00
    PerlSetVar MyAuth_LDAPURI ldap://127.0.0.1
    PerlSetVar MyAuth_Base uid=%USER%,ou=staff,dc=company,dc=com
    PerlSetVar MyAuth_BindDN cn=ldap,dc=company,dc=com
    PerlSetVar MyAuth_BindPW somepassword
    PerlSetVar MyAuth_Filter (uid=%USER%)

    <Directory /var/www/mysite/protected>
        AuthType Apache2::AuthCookieLDAP
        AuthName MyAuth
        PerlAuthenHandler Apache2::AuthCookieLDAP->authenticate
        PerlAuthzHandler Apache2::AuthCookieLDAP->authorize
        require valid-user
    </Directory>

    <Location /login>
        SetHandler perl-script
        AuthType Apache2::AuthCookieLDAP
        AuthName MyAuth
        PerlResponseHandler MyAuthCookieLDAP->login
    </Location>

    <Location /logout>
        SetHandler perl-script
        AuthType Apache2::AuthCookieLDAP
        AuthName MyAuth
        PerlResponseHandler Apache2::AuthCookieLDAP->logout
    </Location>

=head1 DESCRIPTION

This module acts as an authentication handler under Apache2 environment. 
It uses Apache2::AuthCookie as the base class and serves as a backend to 
provide user authentication against an LDAP server.

Make sure that you have got a reachable LDAP server and credentials to access it 
(ldapuri, base, binddn/bindpw or anonymous bind).

When there is an attempt to access a "protected" directory or location
that has 'require valid-user' option included Apache2::AuthCookieLDAP is used 
as the authentication and the authorization handler. It takes a pair of
provided username/password and tries to search the username in the LDAP directory 
(it also uses the filter MyAuth_Filter, for puropses where you want to restrict access
to the resource to only a specific group). If the user is found then it tries 
to bind with the provided username/password.  Once authorized a session key 
is generated by taking into account the provided username, authorization time 
and a hash generated by including a specific logic plus the user's IP address. 
Upon completion the session data is encrypted with the secret key (MyAuth_SecretKey) 
and the according cookie is generated by Apache2::AuthCookie.  
All the following requests to the protected resource take the cookie (if exists)
and the encrypted session key is validated (decrypted, the user is checked, 
the session time is checked for expiration and the hash is regenerated 
and compared with the provided one).
Upon success the user is authorized to access the protected resource.

Should you require any additional information how the cookies logic works 
please check L<Apache2::AuthCookie> documentation.

=head1 APACHE CONFIGURATION DIRECTIVES

All the configuration directives as used in the following format:

    PerlSetVar "AuthName""DirectiveName"

So if your have:
    
    <Directory /var/www/mysite/protected>
        AuthType Apache2::AuthCookieLDAP
        AuthName WhateverAuthName
    ...

Then the directive name for you will be (for instance):

    PerlSetVar WhatEverAuthName_SecretKey

=over 4

=item C<MyAuth_SecretKey> 

Use your own secret key !!!DONT USE THE ONE FROM THE EXAMPLE!!!

=item C<MyAuth_SessionLifetime> [optional, default: 00-24-00-00]

Format is: days-hours-minutes-seconds or 'forever' for endless sessions

=item C<MyAuth_LDAPURI>

Your LDAP server URI

Format: ldap://127.0.0.1 or ldap://myldaphost

Use ldaps:// for secure connections (if your LDAP server supports it)

=item C<MyAuth_Base> 

LDAP Base. Please note that '%USER%' macro is substituted in the request
with a username that is being authenticated.

Example: uid=%USER%,ou=staff,dc=company,dc=com

=item C<MyAuth_BindDN> [optional]

Use the option if your LDAP does not accept anonymous bind 
for search.

Example: cn=ldap,dc=company,dc=com

=item C<MyAuth_BindPW> [optional]

If you  BindDN then you most likely want to specify
a password here to bind with.

=item C<MyAuth_Cipher> [optinal, default: 'des']

An encryption method used for the session key.

Supported methods: 'des', 'idea', 'blowfish', 'blowfish_pp'

=item C<MyAuth_Filter> [optinal, default: '(uid=%USER%)']

You can additionally check if a user belongs to a specific group or has 
specific LDAP attributes. Where '%USER%' macro is substituted in the request
with a username that is being authenticated.

For instance: (&(uid=%USER%)(objectClass=posixAccount))

perldoc Net::LDAP::Filter for additional info

=item C<MyAuth_DebugLogLevel> [optional, default: 'alert']

A log level that will be used to send debug messages into your 
Apache log file.

Supported levels: 'emerg', 'alert', 'crit', 'err', 'warn', 'notice', 'info', 'debug'

=item C<MyAuth_ErrorLogLevel> [optional, default: 'err']

A log level that will be used to send error messages into your 
Apache log file.

Supported levels: 'emerg', 'alert', 'crit', 'err', 'warn', 'notice', 'info', 'debug'



( run in 2.490 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )