From f26d9664844f384e502eefd1411c9f77b66718a1 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Thu, 14 Apr 2022 13:24:39 +0000
Subject: [PATCH] Bug 28998: (follow-up) Apply changes to TwoFactorAuth module
 and script

Test plan:
Run t/db_dependent/Koha/Auth/TwoFactorAuth.t
Walk thru cycle register - logout/login - deregister - logout/login.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 Koha/Auth/TwoFactorAuth.pm               |  2 +-
 members/two_factor_auth.pl               |  2 +-
 t/db_dependent/Koha/Auth/TwoFactorAuth.t | 12 ++++++------
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/Koha/Auth/TwoFactorAuth.pm b/Koha/Auth/TwoFactorAuth.pm
index f2cce95872..9d7194558b 100644
--- a/Koha/Auth/TwoFactorAuth.pm
+++ b/Koha/Auth/TwoFactorAuth.pm
@@ -72,7 +72,7 @@ sub new {
     } elsif( $secret ) {
         $type = 'secret';
     } elsif( $patron->secret ) {
-        $secret32 = $patron->secret; # saved already in base32
+        $secret32 = $patron->decoded_secret; # saved already in base32
     } else {
         Koha::Exceptions::MissingParameter->throw("No secret passed or patron has no secret");
     }
diff --git a/members/two_factor_auth.pl b/members/two_factor_auth.pl
index 6d5847e0ac..cdb714f285 100755
--- a/members/two_factor_auth.pl
+++ b/members/two_factor_auth.pl
@@ -69,7 +69,7 @@ if ( $op eq 'register-2FA' ) {
 
     if ($verified) {
         # FIXME Generate a (new?) secret
-        $logged_in_user->secret($secret32);
+        $logged_in_user->encode_secret($secret32);
         $logged_in_user->auth_method('two-factor')->store;
         $op = 'registered';
         if( $logged_in_user->notice_email_address ) {
diff --git a/t/db_dependent/Koha/Auth/TwoFactorAuth.t b/t/db_dependent/Koha/Auth/TwoFactorAuth.t
index 4185e5cde0..d4faea1cc0 100755
--- a/t/db_dependent/Koha/Auth/TwoFactorAuth.t
+++ b/t/db_dependent/Koha/Auth/TwoFactorAuth.t
@@ -42,10 +42,10 @@ subtest 'new' => sub {
         'Croaked on wrong encoding';
 
     # Test passing secret or secret32 (converted to base32)
-    $patron->secret('nv4v65dpobpxgzldojsxiii'); # this is base32 already for 'my_top_secret!'
+    $patron->encode_secret('nv4v65dpobpxgzldojsxiii'); # this is base32 already for 'my_top_secret!'
     my $auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron });
-    is( $auth->secret32, $patron->secret, 'Base32 secret as expected' );
-    $auth->code( $patron->secret ); # trigger conversion by passing base32 to code
+    is( $auth->secret32, $patron->decoded_secret, 'Base32 secret as expected' );
+    $auth->code( $patron->decoded_secret ); # trigger conversion by passing base32 to code
     is( $auth->secret, 'my_top_secret!', 'Decoded secret fine too' );
     # The other way around
     $auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron, secret => 'my_top_secret!' });
@@ -64,7 +64,7 @@ subtest 'qr_code' => sub {
 
     t::lib::Mocks::mock_preference('TwoFactorAuthentication', 1);
     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
-    $patron->secret('you2wont2guess2it'); # this is base32 btw
+    $patron->encode_secret('you2wont2guess2it'); # this is base32 btw
     $patron->auth_method('two-factor');
     $patron->store;
 
@@ -76,7 +76,7 @@ subtest 'qr_code' => sub {
     $auth->clear;
 
     # Changing the secret should generate different data, right?
-    $patron->secret('no3really3not3cracked'); # base32
+    $patron->encode_secret('no3really3not3cracked'); # base32
     $patron->store;
     $auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron });
     my $img_data02 = $auth->qr_code;
@@ -92,7 +92,7 @@ subtest 'send_confirm_notice' => sub {
 
     t::lib::Mocks::mock_preference('TwoFactorAuthentication', 1);
     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
-    $patron->secret('you2wont2guess2it'); # this is base32 btw
+    $patron->encode_secret('you2wont2guess2it'); # this is base32 btw
     $patron->auth_method('two-factor');
     $patron->store;
     my $auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron });
-- 
2.20.1