Apache2_4-AuthCookieMultiDBI
view release on metacpan or search on metacpan
# Optional, to share tickets between servers.
PerlSetVar WhatEverDomain .domain.com
# These must be set
PerlSetVar WhatEverDBI_DSN "DBI:mysql:database=test"
PerlSetVar WhatEverDBI_SecretKey "489e5eaad8b3208f9ad8792ef4afca73598ae666b0206a9c92ac877e73ce835c"
# These are optional, the module sets sensible defaults.
PerlSetVar WhatEverDBI_User "nobody"
PerlSetVar WhatEverDBI_Password "password"
PerlSetVar WhatEverDBI_UsersTable "users"
PerlSetVar WhatEverDBI_UserField "user"
PerlSetVar WhatEverDBI_PasswordField "password"
PerlSetVar WhatEverDBI_UserActiveField "" # Default is skip this feature
PerlSetVar WhatEverDBI_CryptType "none"
PerlSetVar WhatEverDBI_GroupsTable "groups"
PerlSetVar WhatEverDBI_GroupField "grp"
PerlSetVar WhatEverDBI_GroupUserField "user"
PerlSetVar WhatEverDBI_EncryptionType "none"
PerlSetVar WhatEverDBI_SessionLifetime 00-24-00-00
perlSetVar WhatEverDBI_URIRegx "^/(.+)/(.+)$" # if have uri pattran like /client_id/file_name.pl
perlSetVar WhatEverDBI_URIClientPos 0 # client_id position in uri
perlSetVar WhatEverDBI_LoadClientDB 1 # do you have seperate database for each cleint
# If the directopry you are protecting is the DocumentRoot directory
# then uncomment the following directive:
#Satisfy any
</Files>
=head1 DESCRIPTION
This module is an authentication handler that uses the basic mechanism provided
by Apache2_4::AuthCookie with a DBI database for ticket-based protection. Actually
it is modified version of L<Apache2::AuthCookieDBI> for apache 2.4. 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 DBI database. The supplied password is checked against the password
in the database; the password in the database can be plaintext, or a crypt()
or md5_hex() checksum of the password. 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
lib/Apache2_4/AuthCookieMultiDBI.pm view on Meta::CPAN
use constant THIRTY_TWO_CHARACTER_HEX_STRING_REGEX => qr/ \A [0-9a-fA-F]{32} \z /mx;
use constant TRUE => 1;
my %CONFIG_DEFAULT = (
DBI_DSN => undef,
DBI_SecretKey => undef,
DBI_User => undef,
DBI_Password => undef,
DBI_UsersTable => 'users',
DBI_UserField => 'user',
DBI_PasswordField => 'password',
DBI_UserActiveField => EMPTY_STRING, # Default is don't use this feature
DBI_CryptType => 'none',
DBI_GroupsTable => 'groups',
DBI_GroupField => 'grp',
DBI_GroupUserField => 'user',
DBI_EncryptionType => 'none',
DBI_SessionLifetime => '00-24-00-00',
DBI_sessionmodule => 'none',
DBI_URIRegx => EMPTY_STRING, # Default is don't use this feature
DBI_LoadClientDB => 0, # 1 to set
lib/Apache2_4/AuthCookieMultiDBI.pm view on Meta::CPAN
# Optional, to share tickets between servers.
PerlSetVar WhatEverDomain .domain.com
# These must be set
PerlSetVar WhatEverDBI_DSN "DBI:mysql:database=test"
PerlSetVar WhatEverDBI_SecretKey "489e5eaad8b3208f9ad8792ef4afca73598ae666b0206a9c92ac877e73ce835c"
# These are optional, the module sets sensible defaults.
PerlSetVar WhatEverDBI_User "nobody"
PerlSetVar WhatEverDBI_Password "password"
PerlSetVar WhatEverDBI_UsersTable "users"
PerlSetVar WhatEverDBI_UserField "user"
PerlSetVar WhatEverDBI_PasswordField "password"
PerlSetVar WhatEverDBI_UserActiveField "" # Default is skip this feature
PerlSetVar WhatEverDBI_CryptType "none"
PerlSetVar WhatEverDBI_GroupsTable "groups"
PerlSetVar WhatEverDBI_GroupField "grp"
PerlSetVar WhatEverDBI_GroupUserField "user"
PerlSetVar WhatEverDBI_EncryptionType "none"
PerlSetVar WhatEverDBI_SessionLifetime 00-24-00-00
perlSetVar WhatEverDBI_URIRegx "^/(.+)/(.+)$" # if have uri pattran like /client_id/file_name.pl
perlSetVar WhatEverDBI_URIClientPos 0 # client_id position in uri
perlSetVar WhatEverDBI_LoadClientDB 1 # do you have seperate database for each cleint
lib/Apache2_4/AuthCookieMultiDBI.pm view on Meta::CPAN
# If the directopry you are protecting is the DocumentRoot directory
# then uncomment the following directive:
#Satisfy any
</Files>
=head1 DESCRIPTION
This module is an authentication handler that uses the basic mechanism provided
by Apache2_4::AuthCookie with a DBI database for ticket-based protection. Actually
it is modified version of L<Apache2::AuthCookieDBI> for apache 2.4. 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 DBI database. The supplied password is checked against the password
in the database; the password in the database can be plaintext, or a crypt()
or md5_hex() checksum of the password. 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
lib/Apache2_4/AuthCookieMultiDBI.pm view on Meta::CPAN
if ( !defined $c{$variable} ) {
$self->_log_not_set( $r, $variable );
}
}
# If we used encryption we need to pull in Crypt::CBC.
if ( $c{'DBI_EncryptionType'} ne 'none' ) {
require Crypt::CBC;
}
# Compile module for password encryption, if needed.
if ( $c{'DBI_CryptType'} =~ '^sha') {
require Digest::SHA;
}
return %c;
}
#-------------------------------------------------------------------------------
# _get_cipher_for_type - Get the cipher from the cache, or create a new one if the
# cached cipher hasn't been created.
lib/Apache2_4/AuthCookieMultiDBI.pm view on Meta::CPAN
Carp::confess('Failed to pass Apache request object') if not $r;
my ( $pkg, $file, $line, $sub ) = caller(1);
my $info_message = "${self} -> _dbi_connect called in $sub at line $line";
$r->server->log_error( $info_message );
my %c = $self->_dbi_config_vars($r);
my $auth_name = $r->auth_name;
# get the crypted password from the users database for this user.
my $dbh = DBI->connect_cached( $c{'DBI_DSN'}, $c{'DBI_User'}, $c{'DBI_Password'} );
if ( !defined $dbh ) {
my $error_message = "${self} => couldn't connect to $c{'DBI_DSN'} for auth realm $auth_name";
$r->server->log_error( $error_message );
return;
}
if($c{'DBI_LoadClientDB'}) {
my $client = $self->get_client_name($r);
lib/Apache2_4/AuthCookieMultiDBI.pm view on Meta::CPAN
my $error_message = "${self} => couldn't connect to $c{'DBI_DSN'} for auth realm $auth_name";
$r->server->log_error( $error_message );
return;
}
return $dbh;
}
#-------------------------------------------------------------------------------
# _get_crypted_password -- Get the users' password from the database
#
sub _get_crypted_password ($$\@) {
my $self = shift;
my $r = shift;
my $user = shift;
my $dbh = $self->_dbi_connect($r) || return;
my %c = $self->_dbi_config_vars($r);
my $auth_name = $r->auth_name;
if ( !$self->user_is_active( $r, $user ) ) {
my $message
= "${self}\tUser '$user' is not active for auth realm $auth_name.";
$r->server->log_error( $message );
return;
}
my $crypted_password = EMPTY_STRING;
my $sql_query = <<"SQL";
SELECT `$c{'DBI_PasswordField'}`
FROM `$c{'DBI_UsersTable'}`
WHERE `$c{'DBI_UserField'}` = ?
AND (`$c{'DBI_PasswordField'}` != ''
AND `$c{'DBI_PasswordField'}` IS NOT NULL)
SQL
my $sth = $dbh->prepare_cached($sql_query);
$sth->execute($user);
($crypted_password) = $sth->fetchrow_array();
$sth->finish();
if ( _is_empty($crypted_password) ) {
my $message
= "${self}\tCould not select password using SQL query '$sql_query'";
$r->server->log_error( $message );
return;
}
return $crypted_password;
}
#-------------------------------------------------------------------------------
# _now_year_month_day_hour_minute_second -- Return a string with the time in
# this order separated by dashes.
#
sub _now_year_month_day_hour_minute_second {
return sprintf '%04d-%02d-%02d-%02d-%02d-%02d', Today_and_Now;
}
#-------------------------------------------------------------------------------
# _check_password -- password checking
#
sub _check_password {
my ( $self, $password, $crypted_password, $crypt_type ) = @_;
return
if not $crypted_password
; # https://rt.cpan.org/Public/Bug/Display.html?id=62470
my %password_checker = (
'none' => sub { return $password eq $crypted_password; },
'crypt' => sub {
$self->_crypt_digest( $password, $crypted_password ) eq
$crypted_password;
},
'md5' => sub { return md5_hex($password) eq $crypted_password; },
'sha256' => sub {
return Digest::SHA::sha256_hex($password) eq $crypted_password;
},
'sha384' => sub {
return Digest::SHA::sha384_hex($password) eq $crypted_password;
},
'sha512' => sub {
return Digest::SHA::sha512_hex($password) eq $crypted_password;
},
);
return $password_checker{$crypt_type}->();
}
#-------------------------------------------------------------------------------
# _get_expire_time -- calculating expire time
#
sub _get_expire_time {
my $session_lifetime = shift;
$session_lifetime = lc $session_lifetime;
my $expire_time = EMPTY_STRING;
lib/Apache2_4/AuthCookieMultiDBI.pm view on Meta::CPAN
}
#-------------------------------------------------------------------------------
# authen_cred -- Overrid authen_cred method from Apache2_4::AuthCookie
#
sub authen_cred ($$\@) {
my $self = shift;
my $r = shift;
my $user = shift;
my $password = shift;
my @extra_data = @_;
my $auth_name = $r->auth_name;
( $user, $password ) = _defined_or_empty( $user, $password );
$user = trim($user);
if ( !length $user ) {
$r->server->log_error( "${self} no username supplied for auth realm $auth_name" );
return;
}
if ( !length $password ) {
$r->server->log_error( "${self} no password supplied for auth realm $auth_name" );
return;
}
if ( !$self->user_is_active( $r, $user ) ) {
my $message
= "${self}\tUser '$user' is not active for auth realm $auth_name.";
$r->server->log_error( $message );
return;
}
# get the configuration information.
my %c = $self->_dbi_config_vars($r);
# get the crypted password from the users database for this user.
my $crypted_password = $self->_get_crypted_password( $r, $user, \%c );
# now return unless the passwords match.
my $crypt_type = lc $c{'DBI_CryptType'};
if ( !$self->_check_password( $password, $crypted_password, $crypt_type ) )
{
my $message = "${self} crypt_type: '$crypt_type' - passwords didn't match for user '$user' for auth realm $auth_name";
$r->server->log_error( $message );
return;
}
# Successful login
my $message = "${self} Successful login for $user for auth realm $auth_name";
$r->server->log_error( $message );
# Create the expire time for the ticket.
my $expire_time = _get_expire_time( $c{'DBI_SessionLifetime'} );
lib/Apache2_4/AuthCookieMultiDBI.pm view on Meta::CPAN
$r->pnotes( $auth_name, $session );
$session_id = $session->{_session_id};
}
# OK, now we stick the username and the current time and the expire
# time and the session id (if any) together to make the public part
# of the session key:
my $current_time = _now_year_month_day_hour_minute_second;
my $public_part = "$enc_user:$current_time:$expire_time:$session_id";
$public_part
.= $self->extra_session_info( $r, $user, $password, @extra_data );
# Now we calculate the hash of this and the secret key and then
# calculate the hash of *that* and the secret key again.
my $secretkey = $c{'DBI_SecretKey'};
if ( !defined $secretkey ) {
my $message = "${self} -> didn't have the secret key for auth realm $auth_name";
$r->server->log_error( $message );
return;
}
my $hash = md5_hex( join q{:}, $secretkey,
lib/Apache2_4/AuthCookieMultiDBI.pm view on Meta::CPAN
# my $auth_name = $r->auth_name;
# my $client = $self->get_client_name($r);
# return $r->dir_config("${auth_name}Path") . "$client/";
# }
sub extra_session_info {
my ( $self, $r, $user, $password, @extra_data ) = @_;
return EMPTY_STRING;
}
=head1 EXPORTS
None.
=head1 REVISIONS
( run in 0.809 second using v1.01-cache-2.11-cpan-49f99fa48dc )