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

(-)a/C4/Auth.pm (-1 / +1 lines)
Lines 1639-1645 sub checkpw { Link Here
1639
1639
1640
        # Then, we check if it matches a valid koha user
1640
        # Then, we check if it matches a valid koha user
1641
        if ($shib_login) {
1641
        if ($shib_login) {
1642
            my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib( $dbh, $shib_login );    # EXTERNAL AUTH
1642
            my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib( $shib_login );    # EXTERNAL AUTH
1643
            ($retval) and return ( $retval, $retcard, $retuserid );
1643
            ($retval) and return ( $retval, $retcard, $retuserid );
1644
            return 0;
1644
            return 0;
1645
        }
1645
        }
(-)a/C4/Auth_with_shibboleth.pm (-13 / +9 lines)
Lines 21-26 use Modern::Perl; Link Here
21
21
22
use C4::Debug;
22
use C4::Debug;
23
use C4::Context;
23
use C4::Context;
24
use Koha::Database;
24
use Carp;
25
use Carp;
25
use CGI;
26
use CGI;
26
27
Lines 89-109 sub get_login_shib { Link Here
89
sub checkpw_shib {
90
sub checkpw_shib {
90
    $debug and warn "checkpw_shib";
91
    $debug and warn "checkpw_shib";
91
92
92
    my ( $dbh, $match ) = @_;
93
    my ( $match ) = @_;
93
    my ( $retnumber, $userid );
94
    my $config = _get_shib_config();
94
    my $config = _get_shib_config();
95
    $debug and warn "User Shibboleth-authenticated as: $match";
95
    $debug and warn "User Shibboleth-authenticated as: $match";
96
96
97
  # Does the given shibboleth attribute value ($match) match a valid koha user ?
97
    # Does the given shibboleth attribute value ($match) match a valid koha user ?
98
    my $sth = $dbh->prepare(
98
    my $borrower =
99
        "select cardnumber, userid from borrowers where $config->{matchpoint}=?"
99
      Koha::Database->new()->schema()->resultset('Borrower')
100
    );
100
      ->find( { $config->{matchpoint} => $match } );
101
    $sth->execute($match);
101
    if ( defined($borrower) ) {
102
    if ( $sth->rows ) {
102
        return ( 1, $borrower->get_column('cardnumber'), $borrower->get_column('userid') );
103
        my @retvals = $sth->fetchrow;
104
        $retnumber = $retvals[0];
105
        $userid    = $retvals[1];
106
        return ( 1, $retnumber, $userid );
107
    }
103
    }
108
104
109
    # If we reach this point, the user is not a valid koha user
105
    # If we reach this point, the user is not a valid koha user
Lines 265-271 Returns the shibboleth login attribute should it be found present in the http se Link Here
265
261
266
Given a database handle and a shib_login attribute, this routine checks for a matching local user and if found returns true, their cardnumber and their userid.  If a match is not found, then this returns false.
262
Given a database handle and a shib_login attribute, this routine checks for a matching local user and if found returns true, their cardnumber and their userid.  If a match is not found, then this returns false.
267
263
268
  my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib( $dbh, $shib_login );
264
  my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib( $shib_login );
269
265
270
=head1 SEE ALSO
266
=head1 SEE ALSO
271
267
(-)a/t/Auth_with_shibboleth.t (-33 / +33 lines)
Lines 7-13 use Modern::Perl; Link Here
7
use Test::More tests => 6;
7
use Test::More tests => 6;
8
use Test::MockModule;
8
use Test::MockModule;
9
use Test::Warn;
9
use Test::Warn;
10
use DBD::Mock;
10
use Test::DBIx::Class {schema_class => 'Koha::Schema', connect_info => ['dbi:SQLite:dbname=:memory:','','']};
11
11
12
use CGI;
12
use CGI;
13
use C4::Context;
13
use C4::Context;
Lines 17-41 my $matchpoint = 'userid'; Link Here
17
my %mapping = ( 'userid' => { 'is' => 'uid' }, );
17
my %mapping = ( 'userid' => { 'is' => 'uid' }, );
18
$ENV{'uid'} = "test1234";
18
$ENV{'uid'} = "test1234";
19
19
20
#my %shibboleth = (
21
#    'matchpoint' => $matchpoint,
22
#    'mapping'    => \%mapping
23
#);
24
25
# Setup Mocks
20
# Setup Mocks
26
## Mock Context
21
## Mock Context
27
my $context = new Test::MockModule('C4::Context');
22
my $context = new Test::MockModule('C4::Context');
28
23
29
### Mock ->dbh
30
$context->mock(
31
    '_new_dbh',
32
    sub {
33
        my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
34
          || die "Cannot create handle: $DBI::errstr\n";
35
        return $dbh;
36
    }
37
);
38
39
### Mock ->config
24
### Mock ->config
40
$context->mock( 'config', \&mockedConfig );
25
$context->mock( 'config', \&mockedConfig );
41
26
Lines 64-71 sub mockedPref { Link Here
64
    return $return;
49
    return $return;
65
}
50
}
66
51
67
# Convenience methods
52
## Mock Database
68
## Reset Context
53
my $database = new Test::MockModule('Koha::Database');
54
55
### Mock ->schema
56
$database->mock( 'schema', \&mockedSchema );
57
58
sub mockedSchema {
59
    return Schema();
60
}
61
62
## Convenience method to reset config
69
sub reset_config {
63
sub reset_config {
70
    $matchpoint = 'userid';
64
    $matchpoint = 'userid';
71
    %mapping    = ( 'userid' => { 'is' => 'uid' }, );
65
    %mapping    = ( 'userid' => { 'is' => 'uid' }, );
Lines 75-81 sub reset_config { Link Here
75
}
69
}
76
70
77
# Tests
71
# Tests
78
my $dbh = C4::Context->dbh();
72
##############################################################
79
73
80
# Can module load
74
# Can module load
81
use_ok('C4::Auth_with_shibboleth');
75
use_ok('C4::Auth_with_shibboleth');
Lines 155-175 subtest "get_login_shib tests" => sub { Link Here
155
149
156
## checkpw_shib
150
## checkpw_shib
157
subtest "checkpw_shib tests" => sub {
151
subtest "checkpw_shib tests" => sub {
158
    plan tests => 12;
152
    plan tests => 13;
159
160
    my $shib_login = 'test1234';
161
    my @borrower_results =
162
      ( [ 'cardnumber', 'userid' ], [ 'testcardnumber', 'test1234' ], );
163
    $dbh->{mock_add_resultset} = \@borrower_results;
164
153
154
    my $shib_login;
165
    my ( $retval, $retcard, $retuserid );
155
    my ( $retval, $retcard, $retuserid );
166
156
157
    # Setup Mock Database Data
158
    fixtures_ok [
159
        'Borrower' => [
160
            [qw/cardnumber userid surname address city/],
161
            [qw/testcardnumber test1234 renvoize myaddress johnston/],
162
        ],
163
      ],
164
      'Installed some custom fixtures via the Populate fixture class';
165
167
    # debug off
166
    # debug off
168
    $C4::Auth_with_shibboleth::debug = '0';
167
    $C4::Auth_with_shibboleth::debug = '0';
169
168
170
    # good user
169
    # good user
170
    $shib_login = "test1234";
171
    warnings_are {
171
    warnings_are {
172
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
172
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
173
    }
173
    }
174
    [], "good user with no debug";
174
    [], "good user with no debug";
175
    is( $retval,    "1",              "user authenticated" );
175
    is( $retval,    "1",              "user authenticated" );
Lines 177-197 subtest "checkpw_shib tests" => sub { Link Here
177
    is( $retuserid, "test1234",       "expected userid returned" );
177
    is( $retuserid, "test1234",       "expected userid returned" );
178
178
179
    # bad user
179
    # bad user
180
    $shib_login = 'martin';
180
    warnings_are {
181
    warnings_are {
181
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
182
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
182
    }
183
    }
183
    [], "bad user with no debug";
184
    [], "bad user with no debug";
184
    is( $retval, "0", "user not authenticated" );
185
    is( $retval, "0", "user not authenticated" );
185
186
186
    # reset db mock
187
    $dbh->{mock_add_resultset} = \@borrower_results;
188
189
    # debug on
187
    # debug on
190
    $C4::Auth_with_shibboleth::debug = '1';
188
    $C4::Auth_with_shibboleth::debug = '1';
191
189
192
    # good user
190
    # good user
191
    $shib_login = "test1234";
193
    warnings_exist {
192
    warnings_exist {
194
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
193
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
195
    }
194
    }
196
    [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
195
    [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
197
      "good user with debug enabled";
196
      "good user with debug enabled";
Lines 200-207 subtest "checkpw_shib tests" => sub { Link Here
200
    is( $retuserid, "test1234",       "expected userid returned" );
199
    is( $retuserid, "test1234",       "expected userid returned" );
201
200
202
    # bad user
201
    # bad user
202
    $shib_login = "martin";
203
    warnings_exist {
203
    warnings_exist {
204
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
204
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
205
    }
205
    }
206
    [
206
    [
207
        qr/checkpw_shib/,
207
        qr/checkpw_shib/,
Lines 218-220 is( C4::Auth_with_shibboleth::_get_uri(), Link Here
218
    "https://testopac.com", "https opac uri returned" );
218
    "https://testopac.com", "https opac uri returned" );
219
219
220
## _get_shib_config
220
## _get_shib_config
221
- 
221
# Internal helper function, covered in tests above

Return to bug 8446