pg_solve_mfa_challenge.sql (2190B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2025 Taler Systems SA 4 -- 5 -- TALER is free software; you can redistribute it and/or modify it under the 6 -- terms of the GNU General Public License as published by the Free Software 7 -- Foundation; either version 3, or (at your option) any later version. 8 -- 9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 -- A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 -- 13 -- You should have received a copy of the GNU General Public License along with 14 -- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 -- 16 17 DROP FUNCTION IF EXISTS merchant_do_solve_mfa_challenge; 18 CREATE FUNCTION merchant_do_solve_mfa_challenge ( 19 IN in_challenge_id INT8, 20 IN in_h_body BYTEA, 21 IN in_solution TEXT, 22 IN in_now INT8, 23 OUT out_solved BOOLEAN, 24 OUT out_retry_counter INT4 25 ) 26 LANGUAGE plpgsql 27 AS $$ 28 DECLARE 29 my_confirmation_date INT8; 30 DECLARE 31 my_rec RECORD; 32 BEGIN 33 34 -- Check if challenge exists and matches 35 SELECT 36 tc.confirmation_date 37 ,tc.retry_counter 38 ,(tc.code = in_solution) AS solved 39 INTO 40 my_rec 41 FROM tan_challenges tc 42 WHERE tc.challenge_id = in_challenge_id 43 AND tc.h_body = in_h_body 44 AND tc.expiration_date > in_now; 45 46 IF NOT FOUND 47 THEN 48 out_solved = FALSE; 49 RETURN; 50 END IF; 51 52 my_confirmation_date = my_rec.confirmation_date; 53 out_retry_counter = my_rec.retry_counter; 54 out_solved = my_rec.solved; 55 56 -- Check if already solved before 57 IF my_confirmation_date IS NOT NULL 58 THEN 59 out_solved = TRUE; 60 RETURN; 61 END IF; 62 63 IF (0 = out_retry_counter) 64 THEN 65 out_solved = FALSE; 66 RETURN; 67 END IF; 68 69 IF out_solved 70 THEN 71 -- Newly solved, update DB! 72 my_confirmation_date = in_now; 73 UPDATE tan_challenges 74 SET confirmation_date = my_confirmation_date 75 WHERE challenge_id = in_challenge_id; 76 ELSE 77 -- Failed to solve, decrement retry counter 78 out_retry_counter = out_retry_counter - 1; 79 UPDATE tan_challenges 80 SET retry_counter = out_retry_counter 81 WHERE challenge_id = in_challenge_id; 82 END IF; 83 END; 84 $$;