From 642d0daa7c299404888fab30ae2cdf9c17b94885 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 14 Apr 2022 12:35:30 +0000 Subject: [PATCH] Bug 28998: (follow-up) Add Patron->encode_secret and ->decoded_secret Test plan: Run t/db_dependent/Koha/Patron.t Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize Signed-off-by: Fridolin Somers --- Koha/Patron.pm | 35 +++++++++++++++++++++++++++++++++++ t/db_dependent/Koha/Patron.t | 21 ++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 266e60846c..2b61397c7f 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -36,6 +36,7 @@ use Koha::CirculationRules; use Koha::Club::Enrollments; use Koha::Database; use Koha::DateUtils qw( dt_from_string ); +use Koha::Encryption; use Koha::Exceptions::Password; use Koha::Holds; use Koha::Old::Checkouts; @@ -2130,6 +2131,40 @@ sub can_patron_change_staff_only_lists { return 0; } +=head3 + + $patron->encode_secret($secret32); + + Secret (TwoFactorAuth expects it in base32 format) is encrypted. + You still need to call ->store. + +=cut + +sub encode_secret { + my ( $self, $secret ) = @_; + if( $secret ) { + return $self->secret( Koha::Encryption->new->encrypt_hex($secret) ); + } + return $self->secret($secret); +} + +=head3 + + my $secret32 = $patron->decoded_secret; + + Decode the patron secret. We expect to get back a base32 string, but this + is not checked here. Caller of encode_secret is responsible for that. + +=cut + +sub decoded_secret { + my ( $self ) = @_; + if( $self->secret ) { + return Koha::Encryption->new->decrypt_hex( $self->secret ); + } + return $self->secret; +} + =head2 Internal methods =head3 _type diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 6418d6066f..711aa66953 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 16; +use Test::More tests => 17; use Test::Exception; use Test::Warn; @@ -1137,3 +1137,22 @@ subtest 'recalls() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'encode_secret and decoded_secret' => sub { + plan tests => 5; + $schema->storage->txn_begin; + + t::lib::Mocks::mock_config('encryption_key', 't0P_secret'); + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + is( $patron->decoded_secret, undef, 'TestBuilder does not initialize it' ); + $patron->secret(q{}); + is( $patron->decoded_secret, q{}, 'Empty string case' ); + + $patron->encode_secret('encrypt_me'); # Note: lazy testing; should be base32 string normally. + is( length($patron->secret) > 0, 1, 'Secret length' ); + isnt( $patron->secret, 'encrypt_me', 'Encrypted column' ); + is( $patron->decoded_secret, 'encrypt_me', 'Decrypted column' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5