View | Details | Raw Unified | Return to bug 20340
Collapse All | Expand All

(-)a/C4/Auth.pm (+14 lines)
Lines 40-45 use Koha::Library::Groups; Link Here
40
use Koha::Libraries;
40
use Koha::Libraries;
41
use Koha::Patrons;
41
use Koha::Patrons;
42
use Koha::Patron::Consents;
42
use Koha::Patron::Consents;
43
use Koha::Plugins;
43
use POSIX qw/strftime/;
44
use POSIX qw/strftime/;
44
use List::MoreUtils qw/ any /;
45
use List::MoreUtils qw/ any /;
45
use Encode qw( encode is_utf8);
46
use Encode qw( encode is_utf8);
Lines 1781-1786 sub checkpw { Link Here
1781
    my ( $dbh, $userid, $password, $query, $type, $no_set_userenv ) = @_;
1782
    my ( $dbh, $userid, $password, $query, $type, $no_set_userenv ) = @_;
1782
    $type = 'opac' unless $type;
1783
    $type = 'opac' unless $type;
1783
1784
1785
    if ( C4::Context->preference('UseKohaPlugins') && C4::Context->config("enable_plugins") ) {
1786
        my @plugins = Koha::Plugins->new()->GetPlugins({
1787
            method => 'checkpw',
1788
        });
1789
1790
        @plugins = sort { $a->retrieve_data('priority') <=> $b->retrieve_data('priority') } @plugins;
1791
        foreach my $plugin ( @plugins ) {
1792
            my ( $retval, $retcard, $retuserid ) = $plugin->checkpw(@_);
1793
            if ( $retval == 1 ) {
1794
                return ( $retval, $retcard, $retuserid );
1795
            }
1796
        }
1797
    }
1784
    # Get shibboleth login attribute
1798
    # Get shibboleth login attribute
1785
    my $shib = C4::Context->config('useshibboleth') && shib_ok();
1799
    my $shib = C4::Context->config('useshibboleth') && shib_ok();
1786
    my $shib_login = $shib ? get_login_shib() : undef;
1800
    my $shib_login = $shib ? get_login_shib() : undef;
(-)a/t/Koha/Plugin/TestAuth.pm (+75 lines)
Line 0 Link Here
1
package Koha::Plugin::TestAuth;
2
3
use Modern::Perl;
4
5
use base 'Koha::Plugins::Base';
6
7
our $VERSION = '1.0';
8
our $metadata = {
9
    name => 'Authentication test plugin',
10
    author => 'Alex Arnaud <alex.arnaud@biblibre.com>',
11
    description => 'Plugin for testing authentication plugin API',
12
    date_authored => '2018-02-28',
13
    date_updated => '2018-02-28',
14
    minimum_version => '17.05',
15
    maximum_version => undef,
16
    type => 'authentication',
17
    version => $VERSION,
18
};
19
20
=head1 METHODS
21
22
=head2 new
23
24
    Koha::Plugin::TestAuth->new();
25
26
    Create new object
27
28
=cut
29
30
sub new {
31
    my ($class, $args) = @_;
32
33
    $args->{metadata} = $metadata;
34
    $args->{metadata}->{class} = $class;
35
36
    my $self = $class->SUPER::new($args);
37
38
    return $self;
39
}
40
41
=head2 retrieve_data
42
43
Just return priority. It overrides the one in Koha::Plugin::Base for the test.
44
45
=cut
46
47
sub retrieve_data {
48
    my ( $self, $key ) = @_;
49
50
    if ( $key eq 'priority' ) {
51
        return 1;
52
    }
53
54
}
55
56
=head2 checkpw
57
58
  ($result, cardnumber, $userid) =
59
    checkpw($self, $dbh, $userid, $password, $query, $type, $no_set_userenv);
60
61
  Authenticate a patron using login/password.
62
63
=cut
64
65
sub checkpw {
66
    my ( $self, $dbh, $userid, $password, $query, $type, $no_set_userenv ) = @_;
67
68
    if ( $userid = 'test' && $password eq 'test' ) {
69
        return (1, 'test', 'test');
70
    }
71
72
    return (0, '', '');
73
}
74
75
1;
(-)a/t/db_dependent/Auth.t (-9 / +24 lines)
Lines 10-16 use CGI qw ( -utf8 ); Link Here
10
use Test::MockObject;
10
use Test::MockObject;
11
use Test::MockModule;
11
use Test::MockModule;
12
use List::MoreUtils qw/all any none/;
12
use List::MoreUtils qw/all any none/;
13
use Test::More tests => 22;
13
use Test::More;
14
use Test::Warn;
14
use Test::Warn;
15
use t::lib::Mocks;
15
use t::lib::Mocks;
16
use t::lib::TestBuilder;
16
use t::lib::TestBuilder;
Lines 20-27 use C4::Members; Link Here
20
use Koha::AuthUtils qw/hash_password/;
20
use Koha::AuthUtils qw/hash_password/;
21
use Koha::Database;
21
use Koha::Database;
22
use Koha::Patrons;
22
use Koha::Patrons;
23
use File::Basename;
23
24
24
BEGIN {
25
BEGIN {
26
    push( @INC, dirname(__FILE__) . '/..' );
27
25
    use_ok('C4::Auth');
28
    use_ok('C4::Auth');
26
}
29
}
27
30
Lines 33-38 my $dbh = C4::Context->dbh; Link Here
33
# handling
36
# handling
34
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
37
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
35
t::lib::Mocks::mock_preference( 'GDPR_Policy', '' ); # Disabled
38
t::lib::Mocks::mock_preference( 'GDPR_Policy', '' ); # Disabled
39
t::lib::Mocks::mock_preference( 'UseKohaPlugins', 0 );
36
40
37
$schema->storage->txn_begin;
41
$schema->storage->txn_begin;
38
42
Lines 130-142 subtest 'no_set_userenv parameter tests' => sub { Link Here
130
    ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
134
    ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
131
    is( C4::Context->userenv, undef, 'Userenv should be undef as required' );
135
    is( C4::Context->userenv, undef, 'Userenv should be undef as required' );
132
    C4::Context->_new_userenv('DUMMY SESSION');
136
    C4::Context->_new_userenv('DUMMY SESSION');
133
    C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Library 1', 0, '', '');
137
134
    is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv gives correct branch' );
138
    C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Library 1', 0, '', '');
135
    ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
139
    is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv gives correct branch' );
136
    is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is preserved if no_set_userenv is true' );
140
    ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
137
    ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 0 ), 'checkpw still returns true' );
141
    is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' );
138
    isnt( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is overwritten if no_set_userenv is false' );
142
    ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' );
139
};
143
    isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' );
144
145
    t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
146
    my @result = checkpw( $dbh, $patron->{userid}, 'wrong_password', undef, undef, 1 );
147
    is( $result[0], 0, 'With TestAuth plugin, checkpw returns 0' );
148
    is( $result[1], undef, 'With TestAuth plugin, checkpw returns empty cardnumber' );
149
    is( $result[2], undef, 'With TestAuth plugin, checkpw returns empty userid' );
150
    @result = checkpw( $dbh, 'test', 'test', undef, undef, 1 );
151
    is( $result[0], 1, 'With TestAuth plugin, checkpw returns 1' );
152
    is( $result[1], 'test', 'With TestAuth plugin, checkpw returns test cardnumber' );
153
    is( $result[2], 'test', 'With TestAuth plugin, checkpw returns test userid' );
154
}
140
155
141
subtest 'checkpw lockout tests' => sub {
156
subtest 'checkpw lockout tests' => sub {
142
157
Lines 386-388 subtest 'Check value of login_attempts in checkpw' => sub { Link Here
386
};
401
};
387
402
388
$schema->storage->txn_rollback;
403
$schema->storage->txn_rollback;
389
- 
404
done_testing();

Return to bug 20340