challenger

OAuth 2.0-based authentication service that validates user can receive messages at a certain address
Log | Files | Refs | Submodules | README | LICENSE

challenger_do_validate_and_solve_pin.sql (2794B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2024 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 challenger_do_validate_and_solve_pin;
     18 CREATE FUNCTION challenger_do_validate_and_solve_pin (
     19   IN in_nonce BYTEA,
     20   IN in_new_pin INT4,
     21   IN in_now INT8,
     22   OUT out_not_found BOOLEAN,
     23   OUT out_exhausted BOOLEAN, -- set to TRUE if attempts were already exhausted
     24   OUT out_no_challenge BOOLEAN,
     25   OUT out_solved BOOLEAN,
     26   OUT out_state TEXT,
     27   OUT out_address_attempts_left INT4,
     28   OUT out_auth_attempts_left INT4,
     29   OUT out_pin_transmissions_left INT4,
     30   OUT out_client_redirect_uri TEXT)
     31 LANGUAGE plpgsql
     32 AS $$
     33 DECLARE
     34   my_status RECORD;
     35 BEGIN
     36 
     37 SELECT auth_attempts_left
     38       ,address_attempts_left
     39       ,pin_transmissions_left
     40       ,last_pin
     41       ,client_redirect_uri
     42       ,client_state
     43   INTO my_status
     44   FROM validations
     45  WHERE nonce=in_nonce
     46    AND expiration_time > in_now
     47    FOR UPDATE;
     48 
     49 IF NOT FOUND
     50 THEN
     51   out_not_found=TRUE;
     52   out_no_challenge=TRUE;
     53   out_exhausted=FALSE;
     54   out_solved=FALSE;
     55   out_address_attempts_left=0;
     56   out_auth_attempts_left=0;
     57   out_pin_transmissions_left=0;
     58   out_client_redirect_uri=NULL;
     59   out_state=NULL;
     60   RETURN;
     61 END IF;
     62 out_not_found=FALSE;
     63 out_address_attempts_left=my_status.address_attempts_left;
     64 out_pin_transmissions_left=my_status.pin_transmissions_left;
     65 out_client_redirect_uri=my_status.client_redirect_uri;
     66 out_state=my_status.client_state;
     67 
     68 IF (my_status.last_pin IS NULL)
     69 THEN
     70   out_solved=FALSE;
     71   out_exhausted=FALSE;
     72   out_auth_attempts_left=0;
     73   out_no_challenge=TRUE;
     74   RETURN;
     75 END IF;
     76 out_no_challenge=FALSE;
     77 
     78 IF (0 > my_status.auth_attempts_left)
     79 THEN
     80   out_solved=TRUE;
     81   out_exhausted=TRUE;
     82   out_auth_attempts_left=0;
     83   RETURN;
     84 END IF;
     85 
     86 IF (0 = my_status.auth_attempts_left)
     87 THEN
     88   out_solved=FALSE;
     89   out_exhausted=TRUE;
     90   out_auth_attempts_left=0;
     91   RETURN;
     92 END IF;
     93 out_exhausted=FALSE;
     94 out_solved = (my_status.last_pin = in_new_pin);
     95 
     96 IF NOT out_solved
     97 THEN
     98   out_auth_attempts_left=my_status.auth_attempts_left-1;
     99 ELSE
    100   out_auth_attempts_left=-1; -- solved: no more attempts
    101 END IF;
    102 
    103 UPDATE validations
    104  SET auth_attempts_left=out_auth_attempts_left
    105  WHERE nonce=in_nonce;
    106 
    107 RETURN;
    108 
    109 END $$;