taler-exchange-helper-measure-validate-accepted-tos (5206B)
1 #!/bin/bash 2 # 3 # This file is part of TALER 4 # Copyright (C) 2024, 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 # Terms of service status is required. 37 echo "ACCEPTED_TERMS_OF_SERVICE" 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 lifts restrictions after the terms of service were accepted. Expiration rules are set based on the context." 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 "context" 61 echo "current_rules" 62 echo "default_rules" 63 exit 0 64 ;; 65 r) 66 # What to do next? 67 echo "validity_years" 68 echo "successor_measure" 69 exit 0 70 ;; 71 v) 72 echo "$0 v0.0.2" 73 exit 0 74 ;; 75 V) 76 VERBOSE=1 77 ;; 78 ?) 79 exit_fail "Unrecognized command line option" 80 ;; 81 esac 82 done 83 84 if [ 1 = "$VERBOSE" ] 85 then 86 echo "Running $0" 1>&2 87 fi 88 89 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlProgramInput 90 # for the full JSON with possible inputs. 91 92 # First, extract inputs we need 93 INPUTS=$(jq '{"current_rules":(.current_rules // .default_rules // error("neither current_rules nor default_rules provided")),"attributes":.attributes,"context":.context}') 94 95 # Check form ID, must be 'accept-tos' 96 FORM_ID=$(echo "$INPUTS" | jq -r '.attributes.FORM_ID') 97 98 # The 'form' here should be the accept TOS form 99 if [ "$FORM_ID" != "accept-tos" ] 100 then 101 echo "Unexpected form ID $FORM_ID" 1>&2 102 echo "$INPUTS" | exec taler-exchange-helper-measure-freeze 103 exit 1 104 fi 105 106 echo "$INPUTS" \ 107 | jq '.attributes' \ 108 | jq -r 'def get($k): 109 if has($k) 110 then .[$k] 111 else error("attribute missing") 112 end; 113 {"DOWNLOADED_TERMS_OF_SERVICE":get("DOWNLOADED_TERMS_OF_SERVICE"), 114 "ACCEPTED_TERMS_OF_SERVICE":get("ACCEPTED_TERMS_OF_SERVICE")}' \ 115 > /dev/null \ 116 || exec taler-exchange-helper-measure-freeze 117 118 # Get ToS acceptance confirmation. 119 TOS_ACCEPTED=$(echo "$INPUTS" | jq '.attributes.ACCEPTED_TERMS_OF_SERVICE // null') 120 # Get current rules. 121 CURRENT_RULES=$(echo "$INPUTS" | jq '.current_rules // null') 122 # Get context values. 123 VALIDITY_YEARS=$(echo "$INPUTS" | jq -r '.context.validity_years') 124 # Convert to seconds after epoch 125 EXPIRATION_STAMP=$((VALIDITY_YEARS * 365 * 24 * 60 * 60 + $(date +%s))) 126 # Convert to GNUnet-style JSON timestamp 127 EXPIRATION_TIME=$(jq -n --argjson es "$EXPIRATION_STAMP" '{"t_s":$es}') 128 129 SUCCESSOR_MEASURE=$(echo "$INPUTS" | jq '.context.successor_measure // .current_rules.successor_measure // null') 130 CUSTOM_MEASURES=$(echo "$INPUTS" | jq '.context.custom_measures // null') 131 132 # Validate accepted ToS version 133 if eval echo "$TOS_ACCEPTED" | grep "${EXCHANGE_AML_PROGRAM_TOPS_ENABLE_DEPOSITS_TOS_NAME}" > /dev/null 134 then 135 # Valid ToS acceptance 136 # Remove limitation from current rules. 137 NEW_RULES=$(echo "$CURRENT_RULES" | jq '(.rules[] |= if (.measures[0]=="accept-tos") then del(.) else . end)') 138 else 139 # Invalid ToS version 140 echo "Version of accepted ToS ${TOS_ACCEPTED} invalid, wanted ${EXCHANGE_AML_PROGRAM_TOPS_ENABLE_DEPOSITS_TOS_NAME}." 1>&2 141 NEW_RULES="$CURRENT_RULES" 142 fi 143 144 set -x 145 146 # Finally, output the new rules. 147 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlOutcome 148 # for the required output format. 149 jq -n \ 150 --argjson et "$EXPIRATION_TIME" \ 151 --argjson sm "$SUCCESSOR_MEASURE" \ 152 --argjson cm "$CUSTOM_MEASURES" \ 153 --argjson nr "$NEW_RULES" \ 154 '{"new_rules":($nr+{"expiration_time":$et,"successor_measure":$sm,"custom_measures":({}+$cm)})}|del(..|nulls)' 155 156 exit 0