taler-exchange-helper-measure-tops-sms-check (4419B)
1 #!/bin/bash 2 # 3 # This file is part of TALER 4 # Copyright (C) 2024 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 # Exit, with error message (hard failure) 22 function exit_fail() { 23 echo " FAIL: " "$@" >&2 24 EXIT_STATUS=1 25 exit "$EXIT_STATUS" 26 } 27 28 CONF="$HOME/.config/taler-exchange.conf" 29 VERBOSE=0 30 31 while getopts 'ac:hirvV' OPTION; 32 do 33 case "$OPTION" in 34 a) 35 # Phone number is required. 36 echo "CONTACT_PHONE" 37 exit 0 38 ;; 39 c) 40 # shellcheck disable=SC2034 41 CONF="$OPTARG" 42 ;; 43 h) 44 echo "This is a KYC measure program that lifts restrictions on withdraw and P2P transfers after a phone number was confirmed via SMS. Expiration rules are set based on the context." 45 echo 'Supported options:' 46 echo ' -a -- show required attributes' 47 # shellcheck disable=SC2016 48 echo ' -c $CONF -- set configuration' 49 echo ' -h -- print this help' 50 echo ' -i -- show required inputs' 51 echo ' -r -- show required context' 52 echo ' -v -- show version' 53 echo ' -V -- be verbose' 54 exit 0 55 ;; 56 i) 57 # Need attributes, context and current_rules. 58 echo "attributes" 59 echo "context" 60 echo "current_rules" 61 exit 0 62 ;; 63 r) 64 exit 0 65 ;; 66 v) 67 echo "$0 v0.0.2" 68 exit 0 69 ;; 70 V) 71 VERBOSE=1 72 ;; 73 ?) 74 exit_fail "Unrecognized command line option" 75 ;; 76 esac 77 done 78 79 if [ 1 = "$VERBOSE" ] 80 then 81 echo "Running $0" 1>&2 82 fi 83 84 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlProgramInput 85 # for the full JSON with possible inputs. 86 87 # First, extract inputs we need 88 INPUTS=$(jq '{"current_rules":.current_rules,"attributes":.attributes,"context":.context}') 89 90 # Get phone number. 91 PHONE_NUMBER=$(echo "$INPUTS" | jq -r '.attributes.CONTACT_PHONE // null') 92 # Get current rules. 93 CURRENT_RULES=$(echo "$INPUTS" | jq '.current_rules // null') 94 # Get context values. 95 EXPIRATION_TIME=$(echo "$INPUTS" | jq '.context.expiration_time // .attributes.expires // .current_rules.expiration_time // null') 96 SUCCESSOR_MEASURE=$(echo "$INPUTS" | jq '.context.successor_measure // .current_rules.successor_measure // null') 97 CUSTOM_MEASURES=$(echo "$INPUTS" | jq '.context.custom_measures // null') 98 99 # Validate phone number 100 if eval echo "$PHONE_NUMBER" | grep -E -e "${EXCHANGE_AML_PROGRAM_TOPS_SMS_CHECK_REGEX}" > /dev/null 101 then 102 # Valid phone number 103 # Remove limitation from current rules. 104 NEW_RULES=$(echo "$CURRENT_RULES" | jq 'del(.rules[] | select ((.rule_name=="p2p-domestic-identification-requirement") or (.rule_name=="withdraw-limit-low") ))') 105 106 # Remove test rules that should now be satisfied 107 NEW_RULES=$(echo "$NEW_RULES" | jq 'del(.rules[] | select (.rule_name=="balance-testing-limit1") )') 108 109 TO_INVESTIGATE="false" 110 else 111 # Invalid phone number 112 echo "Phone number ${PHONE_NUMBER} invalid." 1>&2 113 NEW_RULES=$(echo "$CURRENT_RULES" | jq '.+{"to_investigate": true}') 114 TO_INVESTIGATE="true" 115 fi 116 117 118 # Finally, output the new rules. 119 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlOutcome 120 # for the required output format. 121 122 exec jq -n \ 123 --argjson et "$EXPIRATION_TIME" \ 124 --argjson sm "$SUCCESSOR_MEASURE" \ 125 --argjson cm "$CUSTOM_MEASURES" \ 126 --argjson nr "$NEW_RULES" \ 127 --argjson inv "$TO_INVESTIGATE" \ 128 '{"new_rules":($nr+{"expiration_time":$et,"successor_measure":$sm,"custom_measures":({}+$nr.custom_measures+$cm)}),"to_investigate":$inv}|del(..|nulls)'