Authen-Simple

 view release on metacpan or  search on metacpan

lib/Authen/Simple/Adapter.pm  view on Meta::CPAN

        my ( $self, $username, $password ) = @_;
        
        if ( $username eq 'larry' && $password eq $self->secret ) {
            
            $self->log->debug( qq/Successfully authenticated user '$username'./ )
              if $self->log;
            
            return 1;
        }
        
        $self->log->debug( qq/Failed to authenticate user '$username'. Reason: 'Invalid credentials'/ )
          if $self->log;
        
        return 0;
    }
    
    1;

=head1 DESCRIPTION

Adapter class for implementations.

t/05adapter.t  view on Meta::CPAN


use strict;
use lib 't/lib';

use MyAdapter;
use MyCache;
use MyLog;

use Test::More tests => 5;

my $credentials  = {
    user => 'password'
};

my $adapter = MyAdapter->new(
    credentials => $credentials,
    log         => MyLog->new
);

ok( $adapter );
ok( $adapter->authenticate( 'user', 'password' ) );
ok( !$adapter->authenticate( 'john', 'password' ) );
like( $adapter->log->messages->[0], qr/Successfully authenticated user 'user'/ );
like( $adapter->log->messages->[1], qr/Failed to authenticate user 'john'/ );

t/06cache.t  view on Meta::CPAN


use strict;
use lib 't/lib';

use MyAdapter;
use MyCache;
use MyLog;

use Test::More tests => 8;

my $credentials  = {
    user => 'password'
};

my $adapter = MyAdapter->new(
    credentials => $credentials,
    cache       => MyCache->new,
    log         => MyLog->new
);

ok( $adapter );
ok( $adapter->authenticate( 'user', 'password' ) );
ok( !$adapter->authenticate( 'john', 'password' ) );

like( $adapter->log->messages->[-2], qr/Caching successful authentication status '1' for user 'user'/ );

is_deeply( scalar $adapter->cache->hash, { 'user:password' => 1 } );

$adapter->credentials( {} );

ok( $adapter->authenticate( 'user', 'password' ) );

like( $adapter->log->messages->[-1], qr/Successfully authenticated user 'user' from cache/ );

$adapter->cache->clear;

ok( !$adapter->authenticate( 'user', 'password' ) );

t/07callback.t  view on Meta::CPAN


use strict;
use lib 't/lib';

use MyAdapter;
use MyCache;
use MyLog;

use Test::More tests => 7;

my $credentials  = {
    user => 'password'
};

my $adapter = MyAdapter->new(
    credentials => $credentials,
    log         => MyLog->new
);

ok( $adapter );

$adapter->callback( sub { undef } );

ok( $adapter->authenticate( 'user', 'password' ) );
like( $adapter->log->messages->[-1], qr/Successfully authenticated user 'user'/ );

t/08simple.t  view on Meta::CPAN


use Authen::Simple;
use MyAdapter;
use MyCache;
use MyLog;

use Test::More tests => 7;

my $log    = MyLog->new;
my $simple = Authen::Simple->new(
    MyAdapter->new( credentials => {}, log => $log ),
    MyAdapter->new( credentials => { user => 'password' }, log => $log ),
);

ok( $simple );
ok( $simple->authenticate( 'user', 'password' ) );
ok( !$simple->authenticate( 'john', 'password' ) );

like( $log->messages->[-4], qr/Failed to authenticate user 'user'/ );
like( $log->messages->[-3], qr/Successfully authenticated user 'user'/ );
like( $log->messages->[-2], qr/Failed to authenticate user 'john'/ );
like( $log->messages->[-1], qr/Failed to authenticate user 'john'/ );

t/lib/MyAdapter.pm  view on Meta::CPAN

package MyAdapter;

use strict;
use warnings;
use base 'Authen::Simple::Adapter';

__PACKAGE__->options({
    credentials => {
        type     => Params::Validate::HASHREF,
        default  => { },
        optional => 1
    }
});

sub check {
    my ( $self, $username, $password ) = @_;

    if ( exists $self->credentials->{$username} && $password eq $self->credentials->{$username} ) {

        $self->log->debug( qq/Successfully authenticated user '$username'./ )
          if $self->log;

        return 1;
    }

    $self->log->debug( qq/Failed to authenticate user '$username'. Reason: 'Invalid credentials'/ )
      if $self->log;

    return 0;
}

1;



( run in 0.294 second using v1.01-cache-2.11-cpan-a5abf4f5562 )