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

(-)a/Koha/Patron.pm (+35 lines)
Lines 36-41 use Koha::CirculationRules; Link Here
36
use Koha::Club::Enrollments;
36
use Koha::Club::Enrollments;
37
use Koha::Database;
37
use Koha::Database;
38
use Koha::DateUtils qw( dt_from_string );
38
use Koha::DateUtils qw( dt_from_string );
39
use Koha::Encryption;
39
use Koha::Exceptions::Password;
40
use Koha::Exceptions::Password;
40
use Koha::Holds;
41
use Koha::Holds;
41
use Koha::Old::Checkouts;
42
use Koha::Old::Checkouts;
Lines 2116-2121 sub has_messaging_preference { Link Here
2116
    )->count;
2117
    )->count;
2117
}
2118
}
2118
2119
2120
=head3
2121
2122
    $patron->encode_secret($secret32);
2123
2124
    Secret (TwoFactorAuth expects it in base32 format) is encrypted.
2125
    You still need to call ->store.
2126
2127
=cut
2128
2129
sub encode_secret {
2130
    my ( $self, $secret ) = @_;
2131
    if( $secret ) {
2132
        return $self->secret( Koha::Encryption->new->encrypt_hex($secret) );
2133
    }
2134
    return $self->secret($secret);
2135
}
2136
2137
=head3
2138
2139
    my $secret32 = $patron->decoded_secret;
2140
2141
    Decode the patron secret. We expect to get back a base32 string, but this
2142
    is not checked here. Caller of encode_secret is responsible for that.
2143
2144
=cut
2145
2146
sub decoded_secret {
2147
    my ( $self ) = @_;
2148
    if( $self->secret ) {
2149
        return Koha::Encryption->new->decrypt_hex( $self->secret );
2150
    }
2151
    return $self->secret;
2152
}
2153
2119
=head2 Internal methods
2154
=head2 Internal methods
2120
2155
2121
=head3 _type
2156
=head3 _type
(-)a/t/db_dependent/Koha/Patron.t (-2 / +20 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 15;
22
use Test::More tests => 16;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
use Test::Warn;
25
25
Lines 1107-1109 subtest 'recalls() tests' => sub { Link Here
1107
1107
1108
    $schema->storage->txn_rollback;
1108
    $schema->storage->txn_rollback;
1109
};
1109
};
1110
- 
1110
1111
subtest 'encode_secret and decoded_secret' => sub {
1112
    plan tests => 5;
1113
    $schema->storage->txn_begin;
1114
1115
    t::lib::Mocks::mock_config('encryption_key', 't0P_secret');
1116
1117
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1118
    is( $patron->decoded_secret, undef, 'TestBuilder does not initialize it' );
1119
    $patron->secret(q{});
1120
    is( $patron->decoded_secret, q{}, 'Empty string case' );
1121
1122
    $patron->encode_secret('encrypt_me'); # Note: lazy testing; should be base32 string normally.
1123
    is( length($patron->secret) > 0, 1, 'Secret length' );
1124
    isnt( $patron->secret, 'encrypt_me', 'Encrypted column' );
1125
    is( $patron->decoded_secret, 'encrypt_me', 'Decrypted column' );
1126
1127
    $schema->storage->txn_rollback;
1128
};

Return to bug 28998