package Authen::Simple::Adapter;
use strict;
use warnings;
use base qw[Class::Accessor::Fast Class::Data::Inheritable];
use Authen::Simple::Log qw[];
use Authen::Simple::Password qw[];
use Carp qw[];
use Params::Validate qw[];
__PACKAGE__->mk_classdata( _options => { } );
__PACKAGE__->mk_accessors( qw[ cache callback log ] );
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $params = Params::Validate::validate_with(
params => \@_,
spec => $class->options,
called => "$class\::new"
);
return $class->SUPER::new->init($params);
}
sub init {
my ( $self, $params ) = @_;
while ( my ( $method, $value ) = each( %{ $params } ) ) {
$self->$method($value);
}
return $self;
}
sub authenticate {
my $self = shift;
my $class = ref($self) || $self;
my ( $username, $password ) = Params::Validate::validate_with(
params => \@_,
spec => [
{
type => Params::Validate::SCALAR
},
{
type => Params::Validate::SCALAR
}
],
called => "$class\::authenticate"
);
my $status;
if ( $self->callback ) {
$status = $self->callback->( \$username, \$password );
if ( defined $status ) {
my $boolean = $status ? 'true' : 'false';
$self->log->debug( qq/Callback returned a $boolean value '$status' for user '$username'./ )
if $self->log;
return $status;
}
}
if ( $self->cache ) {
$status = $self->cache->get("$username:$password");
if ( defined $status ) {
$self->log->debug( qq/Successfully authenticated user '$username' from cache./ )
if $self->log;
return $status;
}
}
$status = $self->check( $username, $password );
if ( $self->cache && $status ) {
$self->cache->set( "$username:$password" => $status );
$self->log->debug( qq/Caching successful authentication status '$status' for user '$username'./ )
if $self->log;
}
return $status;
}
sub check {
Carp::croak( __PACKAGE__ . qq/->check is an abstract method/ );
}
=1= |