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

(-)a/Koha/Auth/TwoFactorAuth.pm (-42 lines)
Lines 25-33 use Koha::Exceptions::Patron; Link Here
25
25
26
use base qw( Auth::GoogleAuth );
26
use base qw( Auth::GoogleAuth );
27
27
28
use constant CONFIRM_NOTICE_REG => '2FA_ENABLE';
29
use constant CONFIRM_NOTICE_DEREG => '2FA_DISABLE';
30
31
=head1 NAME
28
=head1 NAME
32
29
33
Koha::Auth::TwoFactorAuth- Koha class deal with Two factor authentication
30
Koha::Auth::TwoFactorAuth- Koha class deal with Two factor authentication
Lines 40-46 my $secret = Koha::AuthUtils::generate_salt( 'weak', 16 ); Link Here
40
my $auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron, secret => $secret });
37
my $auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron, secret => $secret });
41
my $image_src = $auth->qr_code;
38
my $image_src = $auth->qr_code;
42
my $ok = $auth->verify( $pin_code, 1 );
39
my $ok = $auth->verify( $pin_code, 1 );
43
$auth->send_confirm_notice({ patron => $patron });
44
40
45
It's based on Auth::GoogleAuth
41
It's based on Auth::GoogleAuth
46
42
Lines 108-149 sub qr_code { Link Here
108
    return "data:image/png;base64,". encode_base64( $data, q{} ); # does not contain newlines
104
    return "data:image/png;base64,". encode_base64( $data, q{} ); # does not contain newlines
109
}
105
}
110
106
111
=head3 send_confirm_notice
112
113
    $auth->send_confirm_notice({ patron => $p, deregister => 1 });
114
115
    Send a notice to confirm (de)registering 2FA.
116
    Parameter patron is mandatory.
117
    If there is no deregister param, a register notice is sent.
118
    If the patron has no email address, we throw an exception.
119
120
=cut
121
122
sub send_confirm_notice {
123
    my ( $self, $params ) = @_;
124
    my $patron = $params->{patron};
125
    my $deregister = $params->{deregister};
126
127
    Koha::Exceptions::MissingParameter->throw("Mandatory patron parameter missing")
128
        unless $patron && ref($patron) eq 'Koha::Patron';
129
    Koha::Exceptions::Patron::MissingEmailAddress->throw
130
        if !$patron->notice_email_address;
131
132
    my $letter = C4::Letters::GetPreparedLetter (
133
        module => 'members', # called patrons on interface
134
        letter_code => $deregister ? CONFIRM_NOTICE_DEREG : CONFIRM_NOTICE_REG,
135
        branchcode => $patron->branchcode,
136
        lang => $patron->lang,
137
        tables => {
138
            'branches'    => $patron->branchcode,
139
            'borrowers'   => $patron->id,
140
        },
141
    );
142
    C4::Letters::EnqueueLetter({
143
        letter         => $letter,
144
        borrowernumber => $patron->id,
145
        message_transport_type => 'email',
146
    }) or warn "Couldnt enqueue 2FA notice for patron ". $patron->id;
147
}
148
149
1;
107
1;
(-)a/members/two_factor_auth.pl (-2 / +16 lines)
Lines 73-79 if ( $op eq 'register-2FA' ) { Link Here
73
        $logged_in_user->auth_method('two-factor')->store;
73
        $logged_in_user->auth_method('two-factor')->store;
74
        $op = 'registered';
74
        $op = 'registered';
75
        if( $logged_in_user->notice_email_address ) {
75
        if( $logged_in_user->notice_email_address ) {
76
            $auth->send_confirm_notice({ patron => $logged_in_user });
76
            $logged_in_user->queue_notice({
77
                letter_params => {
78
                    module => 'members', letter_code => '2FA_ENABLE',
79
                    branchcode => $logged_in_user->branchcode, lang => $logged_in_user->lang,
80
                    tables => { branches => $logged_in_user->branchcode, borrowers => $logged_in_user->id },
81
                },
82
                message_transports => [ 'email' ],
83
            });
77
        }
84
        }
78
    }
85
    }
79
    else {
86
    else {
Lines 104-110 elsif ( $op eq 'disable-2FA' ) { Link Here
104
    $logged_in_user->secret(undef);
111
    $logged_in_user->secret(undef);
105
    $logged_in_user->auth_method('password')->store;
112
    $logged_in_user->auth_method('password')->store;
106
    if( $logged_in_user->notice_email_address ) {
113
    if( $logged_in_user->notice_email_address ) {
107
        $auth->send_confirm_notice({ patron => $logged_in_user, deregister => 1 });
114
        $logged_in_user->queue_notice({
115
            letter_params => {
116
                module => 'members', letter_code => '2FA_DISABLE',
117
                branchcode => $logged_in_user->branchcode, lang => $logged_in_user->lang,
118
                tables => { branches => $logged_in_user->branchcode, borrowers => $logged_in_user->id },
119
            },
120
            message_transports => [ 'email' ],
121
        });
108
    }
122
    }
109
}
123
}
110
124
(-)a/t/db_dependent/Koha/Auth/TwoFactorAuth.t (-32 / +1 lines)
Lines 1-5 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
use Test::More tests => 3;
2
use Test::More tests => 2;
3
use Test::Exception;
3
use Test::Exception;
4
use Test::MockModule;
4
use Test::MockModule;
5
5
Lines 85-117 subtest 'qr_code' => sub { Link Here
85
85
86
    $schema->storage->txn_rollback;
86
    $schema->storage->txn_rollback;
87
};
87
};
88
89
subtest 'send_confirm_notice' => sub {
90
    plan tests => 4;
91
    $schema->storage->txn_begin;
92
93
    t::lib::Mocks::mock_preference('TwoFactorAuthentication', 1);
94
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
95
    $patron->secret('you2wont2guess2it'); # this is base32 btw
96
    $patron->auth_method('two-factor');
97
    $patron->store;
98
    my $auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron });
99
100
    # Trivial tests: no patron, no email
101
    throws_ok { $auth->send_confirm_notice; }
102
        'Koha::Exceptions::MissingParameter',
103
        'Croaked on missing patron';
104
    $patron->set({ email => undef, emailpro => undef, B_email => undef });
105
    throws_ok { $auth->send_confirm_notice({ patron => $patron }) }
106
        'Koha::Exceptions::Patron::MissingEmailAddress',
107
        'Croaked on missing email';
108
109
    $patron->email('noreply@doof.nl')->store;
110
    $auth->send_confirm_notice({ patron => $patron });
111
    is( Koha::Notice::Messages->search({ borrowernumber => $patron->id, letter_code => '2FA_REGISTER' })->count, 1, 'Found message' );
112
    $auth->send_confirm_notice({ patron => $patron, deregister => 1 });
113
    is( Koha::Notice::Messages->search({ borrowernumber => $patron->id, letter_code => '2FA_DEREGISTER' })->count, 1, 'Found message' );
114
115
    $schema->storage->txn_rollback;
116
    $mocked_stuffer->unmock;
117
};
118
- 

Return to bug 29894