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

(-)a/t/db_dependent/api/v1/two_factor_auth.t (-2 / +113 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 1;
20
use Test::More tests => 2;
21
use Test::Mojo;
21
use Test::Mojo;
22
use Test::MockModule;
22
use Test::MockModule;
23
23
Lines 41-52 $mocked_koha_email->mock( 'send_or_die', sub { Link Here
41
    return 1;
41
    return 1;
42
});
42
});
43
43
44
subtest 'registration and verification' => sub {
45
46
    plan tests => 22;
47
48
    $schema->storage->txn_begin;
49
50
    t::lib::Mocks::mock_preference('TwoFactorAuthentication', 'enabled');
51
52
    my $patron = $builder->build_object(
53
        {
54
            class => 'Koha::Patrons',
55
            value  => {
56
                flags => 20, # Staff access and Patron info
57
            }
58
        }
59
    );
60
61
    # Not authenticated yet - 401
62
    my $session = C4::Auth::get_session('');
63
    $session->param( 'ip',       '127.0.0.1' );
64
    $session->param( 'lasttime', time() );
65
    $session->flush;
66
67
    my $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration" );
68
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
69
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
70
    $t->request_ok($tx)->status_is(401);
71
72
    $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" );
73
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
74
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
75
    $t->request_ok($tx)->status_is(401);
76
77
    # Authenticated - can register
78
    $session->param( 'number',   $patron->borrowernumber );
79
    $session->param( 'id',       $patron->userid );
80
    $session->flush;
81
82
    $patron->auth_method('password');
83
    $patron->secret(undef);
84
    $patron->store;
85
86
    $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration" );
87
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
88
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
89
    $t->request_ok($tx)->status_is(201);
90
    my $secret32 = $t->tx->res->json->{secret32};
91
92
    $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" );
93
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
94
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
95
    $t->request_ok($tx)->status_is(400); # Missing parameter
96
97
    my $auth = Koha::Auth::TwoFactorAuth->new({patron => $patron, secret32 => $secret32});
98
    my $pin_code = $auth->code;
99
    $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" => form => {secret32 => $secret32, pin_code => $pin_code} );
100
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
101
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
102
    $t->request_ok($tx)->status_is(204);
103
104
    $patron = $patron->get_from_storage;
105
    is($patron->auth_method, 'two-factor');
106
    isnt($patron->secret, undef);
107
108
    $patron->auth_method('password');
109
    $patron->secret(undef);
110
    $patron->store;
111
112
    # Setting up 2FA - can register
113
    $session->param('waiting-for-2FA-setup', 1);
114
    $session->flush;
115
    $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration" );
116
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
117
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
118
    $t->request_ok($tx)->status_is(201);
119
    $secret32 = $t->tx->res->json->{secret32};
120
121
    $auth = Koha::Auth::TwoFactorAuth->new({patron => $patron, secret32 => $secret32});
122
    $pin_code = $auth->code;
123
    $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" => form => {secret32 => $secret32, pin_code => $pin_code} );
124
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
125
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
126
    $t->request_ok($tx)->status_is(204);
127
128
    $patron = $patron->get_from_storage;
129
    is($patron->auth_method, 'two-factor');
130
    isnt($patron->secret, undef);
131
132
    # 2FA already enabled - cannot register again
133
    $patron->auth_method('two-factor');
134
    $patron->encode_secret("nv4v65dpobpxgzldojsxiii");
135
    $patron->store;
136
137
    $session->param('waiting-for-2FA-setup', undef);
138
    $session->flush;
139
140
    $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration" );
141
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
142
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
143
    $t->request_ok($tx)->status_is(401);
144
145
    $tx = $t->ua->build_tx( POST => "/api/v1/auth/two-factor/registration/verification" );
146
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
147
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
148
    $t->request_ok($tx)->status_is(401);
149
150
    $schema->storage->txn_rollback;
151
};
152
44
subtest 'send_otp_token' => sub {
153
subtest 'send_otp_token' => sub {
45
154
46
    plan tests => 11;
155
    plan tests => 11;
47
156
48
    $schema->storage->txn_begin;
157
    $schema->storage->txn_begin;
49
158
159
    t::lib::Mocks::mock_preference('TwoFactorAuthentication', 'enabled');
160
50
    my $patron = $builder->build_object(
161
    my $patron = $builder->build_object(
51
        {
162
        {
52
            class => 'Koha::Patrons',
163
            class => 'Koha::Patrons',
Lines 65-70 subtest 'send_otp_token' => sub { Link Here
65
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
176
    $tx->req->cookies( { name => 'CGISESSID', value => $session->id } );
66
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
177
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
67
178
179
    # FIXME This is not correct They are authenticated!
68
    # Patron is not authenticated yet
180
    # Patron is not authenticated yet
69
    $t->request_ok($tx)->status_is(401);
181
    $t->request_ok($tx)->status_is(401);
70
182
71
- 

Return to bug 30588