Apache-AuthCookieLDAP

 view release on metacpan or  search on metacpan

AuthCookieLDAP.pm  view on Meta::CPAN

# S E R V E R   S T A R T   I N I T I A L I Z A T I O N
#===============================================================================

BEGIN {
	my @keyfile_vars = grep {
		$_ =~ /LDAP_SecretKeyFile$/
	} keys %{ Apache->server->dir_config() };
	foreach my $keyfile_var ( @keyfile_vars ) {
		my $keyfile = Apache->server->dir_config( $keyfile_var );
		my $auth_name = $keyfile_var;
		$auth_name =~ s/LDAP_SecretKeyFile$//;
		unless ( open( KEY, "<$keyfile" ) ) {
			Apache::log_error( "Could not open keyfile for $auth_name in file $keyfile" );
		} else {
			$SECRET_KEYS{ $auth_name } = <KEY>;
			close KEY;
		}
	}
}

#===============================================================================
# P E R L D O C
#===============================================================================

=head1 NAME

Apache::AuthCookieLDAP - An AuthCookie module backed by a LDAP database.

=head1 VERSION

	$Revision: 0.02 $

=head1 SYNOPSIS

Not correct!!!


	# In httpd.conf or .htaccess
	PerlModule Apache::AuthCookieLDAP
	PerlSetVar WhatEverPath /
	PerlSetVar WhatEverLoginScript /login.pl

	# Optional, to share tickets between servers.
	PerlSetVar WhatEverDomain .domain.com
	
	# These must be set
	PerlSetVar WhatEverLDAP_DN "o=foo.com"
	PerlSetVar WhatEverLDAP_SecretKeyFile /etc/httpd/acme.com.key
	PerlSetVar WhatEverLDAP_User uid


	# These are optional, the module sets sensible defaults.

	PerlSetVar WhatEverLDAP_filter F=on
	PerlSetVar WhatEverDBI_GroupsTable "groups"
	PerlSetVar WhatEverDBI_GroupField "grp"
	PerlSetVar WhatEverDBI_GroupUserField "user"

	PerlSetVar WhatEverLDAP_host ldap.bank.com
	PerlSetVar WhatEverLDAP_EncryptionType "none"
	PerlSetVar WhatEverLDAP_SessionLifetime 00-24-00-00

	# Protected by AuthCookieLDAP.
	<Directory /www/domain.com/authcookieldap>
		AuthType Apache::AuthCookieLDAP
		AuthName WhatEver
		PerlAuthenHandler Apache::AuthCookieLDAP->authenticate
		PerlAuthzHandler Apache::AuthCookieLDAP->authorize
		require valid-user
		# or you can require users:
		require user jacob

		# You can optionally require groups.
		require group system
	</Directory>

	# Login location.  *** DEBUG *** I still think this is screwy
	<Files LOGIN>
		AuthType Apache::AuthCookieLDAP
		AuthName WhatEver
		SetHandler perl-script
		PerlHandler Apache::AuthCookieLDAP->login
	</Files>

=head1 DESCRIPTION

This module is an authentication handler that uses the basic mechanism provided
by Apache::AuthCookie with a LDAP database for ticket-based protection.  It
is based on two tokens being provided, a username and password, which can
be any strings (there are no illegal characters for either).  The username is
used to set the remote user as if Basic Authentication was used.

On an attempt to access a protected location without a valid cookie being
provided, the module prints an HTML login form (produced by a CGI or any
other handler; this can be a static file if you want to always send people
to the same entry page when they log in).  This login form has fields for
username and password.  On submitting it, the username and password are looked
up in the LDAP database. If this succeeds, the user is issued
a ticket.  This ticket contains the username, an issue time, an expire time,
and an MD5 checksum of those and a secret key for the server.  It can
optionally be encrypted before returning it to the client in the cookie;
encryption is only useful for preventing the client from seeing the expire
time.  If you wish to protect passwords in transport, use an SSL-encrypted
connection.  The ticket is given in a cookie that the browser stores.

After a login the user is redirected to the location they originally wished
to view (or to a fixed page if the login "script" was really a static file).

On this access and any subsequent attempt to access a protected document, the
browser returns the ticket to the server.  The server unencrypts it if
encrypted tickets are enabled, then extracts the username, issue time, expire
time and checksum.  A new checksum is calculated of the username, issue time,
expire time and the secret key again; if it agrees with the checksum that
the client supplied, we know that the data has not been tampered with.  We
next check that the expire time has not passed.  If not, the ticket is still
good, so we set the username.

Authorization checks then check that any "require valid-user" or "require
user jacob" settings are passed. If all these
checks pass, the document requested is displayed.

AuthCookieLDAP.pm  view on Meta::CPAN




=item C<WhatEverLDAP_filter>
An extra filter for the search for the user. Is not required


=cut

	$c{ LDAP_filter       } = _dir_config_var( $r, 'LDAP_filter') || "";



=item C<WhatEverLDAP_SecretKeyFile>

The file that contains the secret key (on the first line of the file).  This
is required and has no default value.  This key should be owned and only
readable by root.  It is read at server startup time.
The key should be long and fairly random.  If you want, you
can change it and restart the server, (maybe daily), which will invalidate
all prior-issued tickets.

=cut

	unless (
	   $c{ LDAP_secretkeyfile } = _dir_config_var $r, 'LDAP_SecretKeyFile'
	) {
		_log_not_set $r, 'LDAP_SecretKeyFile';
		return undef;
	}

=item C<WhatEverLDAP_EncryptionType>

What kind of encryption to use to prevent the user from looking at the fields
in the ticket we give them.  This is almost completely useless, so don't
switch it on unless you really know you need it.  It does not provide any
protection of the password in transport; use SSL for that.  It can be 'none',
'des', 'idea', 'blowfish', or 'blowfish_pp'.

This is not required and defaults to 'none'.

=cut

	$c{ LDAP_encryptiontype } = _dir_config_var( $r, 'LDAP_EncryptionType' )
	            || 'none';
	# If we used encryption we need to pull in Crypt::CBC.
	if ( $c{ LDAP_encryptiontype } ne 'none' ) {
		require Crypt::CBC;
	}

=item C<WhatEverLDAP_SessionLifetime>

How long tickets are good for after being issued.  Note that presently
Apache::AuthCookie does not set a client-side expire time, which means that
most clients will only keep the cookie until the user quits the browser.
However, if you wish to force people to log in again sooner than that, set
this value.  This can be 'forever' or a life time specified as:

	DD-hh-mm-ss -- Days, hours, minute and seconds to live.

This is not required and defaults to '00-24-00-00' or 24 hours.

=cut

	$c{ LDAP_sessionlifetime }
	   = _dir_config_var( $r, 'LDAP_SessionLifetime' ) || '00-24-00-00';






## This is for some leftover DBI code:

=item C<WhatEverDBI_DSN>

Specifies the DSN for DBI for the database you wish to connect to retrieve
user information.  This is required and has no default value.

=cut

        unless ( $c{ DBI_DSN } = _dir_config_var $r, 'DBI_DSN' ) {
                _log_not_set $r, 'DBI_DSN';
                return undef;
        }

=item C<WhatEverDBI_User>

The user to log into the database as.  This is not required and
defaults to undef.

=cut

        $c{ DBI_user           } = _dir_config_var( $r, 'DBI_User')
                    || undef;

=item C<WhatEverDBI_Password>

The password to use to access the database.  This is not required
and defaults to undef.

=cut

        $c{ DBI_password       } = _dir_config_var( $r, 'DBI_Password' )
                    || undef;



=item C<WhatEverDBI_GroupsTable>

The table that has the user / group information.  This is not required and
defaults to 'groups'.

=cut

        $c{ DBI_groupstable    } = _dir_config_var( $r, 'DBI_GroupsTable')
                    || 'groups';

=item C<WhatEverDBI_GroupField>

The field in the above table that has the group name.  This is not required
and defaults to 'grp' (to prevent conflicts with the SQL reserved word
'group').

=cut



( run in 1.435 second using v1.01-cache-2.11-cpan-39bf76dae61 )