taler-exchange-helper-measure-tops-3rdparty-check (5837B)
1 #!/bin/bash 2 # 3 # This file is part of TALER 4 # Copyright (C) 2025 Taler Systems SA 5 # 6 # TALER is free software; you can redistribute it and/or modify it under the 7 # terms of the GNU General Public License as published by the Free Software 8 # Foundation; either version 3, or (at your option) any later version. 9 # 10 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY 11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 # 14 # You should have received a copy of the GNU General Public License along with 15 # TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/license> 16 # 17 18 # Hard error reporting on. 19 set -eu 20 21 22 # Exit, with error message (hard failure) 23 function exit_fail() { 24 echo " FAIL: " "$@" >&2 25 EXIT_STATUS=1 26 exit "$EXIT_STATUS" 27 } 28 29 CONF="$HOME/.config/taler-exchange.conf" 30 VERBOSE=0 31 32 while getopts 'ac:hirvV' OPTION; 33 do 34 case "$OPTION" in 35 a) 36 # Controlling entity 3rd person field is required. 37 echo "THIRD_PARTY_OWNERSHIP" 38 exit 0 39 ;; 40 c) 41 # shellcheck disable=SC2034 42 CONF="$OPTARG" 43 ;; 44 h) 45 echo "This is a KYC measure program that determines the next VQF form to ask for (if any) based on the type of legal entity the customer claimed to be on the primary form." 46 echo 'Supported options:' 47 echo ' -a -- show required attributes' 48 # shellcheck disable=SC2016 49 echo ' -c $CONF -- set configuration' 50 echo ' -h -- print this help' 51 echo ' -i -- show required inputs' 52 echo ' -r -- show required context' 53 echo ' -v -- show version' 54 echo ' -V -- be verbose' 55 exit 0 56 ;; 57 i) 58 # Need context and current_rules. 59 echo "attributes" 60 echo "current_rules" 61 exit 0 62 ;; 63 r) 64 # Nothing needed from context 65 exit 0 66 ;; 67 v) 68 echo "$0 v0.0.4" 69 exit 0 70 ;; 71 V) 72 VERBOSE=1 73 ;; 74 ?) 75 exit_fail "Unrecognized command line option" 76 ;; 77 esac 78 done 79 80 if [ 1 = "$VERBOSE" ] 81 then 82 echo "Running $0" 1>&2 83 fi 84 85 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlProgramInput 86 # for the full JSON with possible inputs. 87 88 # First, extract inputs we need 89 INPUTS=$(jq '{"current_rules":.current_rules,"attributes":.attributes}') 90 91 # Check form ID, must be 'vqf-902.11' 92 FORM_ID=$(echo "$INPUTS" | jq -r '.attributes.FORM_ID') 93 94 # The 'form' here should be the VQF 902.11 customer form 95 if [ "$FORM_ID" != "vqf_902_11_customer" ] 96 then 97 echo "Unexpected form ID $FORM_ID" 1>&2 98 exec taler-exchange-helper-measure-freeze 99 fi 100 101 # Check all mandatory attributes are present. 102 echo "$INPUTS" \ 103 | jq '.attributes' \ 104 | jq -r 'def get($k): 105 if has($k) 106 then .[$k] 107 else error("attribute \($k) missing") 108 end; 109 {"THIRD_PARTY_OWNERSHIP":get("THIRD_PARTY_OWNERSHIP"), 110 "CONTROL_REASON":get("CONTROL_REASON"), 111 "SIGN_DATE":get("SIGN_DATE"), 112 "SIGNATURE":get("SIGNATURE")}' \ 113 > /dev/null \ 114 || exec taler-exchange-helper-measure-freeze 115 116 117 # Get entity type 118 CONTROL3P=$(echo "$INPUTS" | jq -r '.attributes.THIRD_PARTY_OWNERSHIP') 119 # Get current rules. 120 CURRENT_RULES=$(echo "$INPUTS" | jq '.current_rules // null') 121 # Get context values. 122 EXPIRATION_TIME=$(echo "$INPUTS" | jq '.context.expiration_time // .current_rules.expiration_time // null') 123 124 FORM="error" 125 126 INVESTIGATE="false" 127 case "$CONTROL3P" 128 in 129 "false") 130 FORM="none" 131 ;; 132 "true") 133 FORM="vqf-902.9" 134 ;; 135 esac 136 137 NEW_MEASURES="null" 138 # Check high-level case 139 case "$FORM" 140 in 141 "error") 142 # This should not happen, immediately trigger investigation and show error to the user. 143 echo "ERROR: Unexpected value for controlling entity is 3rd person '${CONTROL3P}'" 1>&2 144 NEW_RULES=$(echo "$CURRENT_RULES" | jq '(.rules[] |= if (.rule_name=="deposit-limit-zero" or .measures[0] == "form-vqf-902.11") then .measures=["inform-internal-error"] else . end)') 145 INVESTIGATE="true" 146 ;; 147 "none") 148 # Move into investigation mode. 149 echo "INFO: Passing data to taler-exchange-helper-measure-inform-investigate" 1>&2 150 NEW_RULES=$(echo "$CURRENT_RULES" | jq '(.rules[] |= if (.rule_name=="deposit-limit-zero" or .measures[0] == "form-vqf-902.11") then .measures=["form-'${FORM}'"] else . end)') 151 echo "$INPUTS" | taler-exchange-helper-measure-inform-investigate 152 exit $? 153 ;; 154 *) 155 # Proceed to FORM. 156 echo "Selected VQF form ${FORM}." 1>&2 157 # Force user to fill in $FORM 158 NEW_RULES=$(echo "$CURRENT_RULES" | jq '(.rules[] |= if (.rule_name=="deposit-limit-zero" or .measures[0] == "form-vqf-902.11") then .measures=["form-'${FORM}'"] else . end)') 159 NEW_MEASURES='"form-'${FORM}'"' 160 ;; 161 esac 162 163 # When the information expires, we start the full KYX process 164 # again. 165 SUCCESSOR_MEASURE='"kyx"' 166 167 # Finally, output the new rules. 168 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlOutcome 169 # for the required output format. 170 jq -n \ 171 --argjson inv "$INVESTIGATE" \ 172 --argjson et "$EXPIRATION_TIME" \ 173 --argjson sm "$SUCCESSOR_MEASURE" \ 174 --argjson nm "$NEW_MEASURES" \ 175 --argjson nr "$NEW_RULES" \ 176 '{"to_investigate":$inv,"new_measures":$nm,"new_rules":($nr+{"expiration_time":$et,"successor_measure":$sm,"custom_measures":({}+$nr.custom_measures)})}|del(..|nulls)' 177 178 exit 0