taler-exchange-kyc-persona-converter.sh (2355B)
1 #!/bin/bash 2 # This file is in the public domain. 3 # 4 # This code converts (some of) the JSON output from Persona into the GNU Taler 5 # specific KYC attribute data (again in JSON format). We may need to download 6 # and inline file data in the process, for authorization pass "-a" with the 7 # respective bearer token. 8 # 9 10 # Die if anything goes wrong. 11 set -eu 12 13 # Parse command-line options 14 while getopts ':a:' OPTION; do 15 case "$OPTION" in 16 a) 17 TOKEN="$OPTARG" 18 ;; 19 ?) 20 echo "Unrecognized command line option" 21 exit 1 22 ;; 23 esac 24 done 25 26 27 # First, extract everything from stdin. 28 J=$(jq '{"first":.data.attributes."name-first","middle":.data.attributes."name-middle","last":.data.attributes."name-last","cc":.data.attributes.fields."address-country-code".value,"birthdate":.data.attributes.birthdate,"city":.data.attributes."address-city","postcode":.data.attributes."address-postal-code","street-1":.data.attributes."address-street-1","street-2":.data.attributes."address-street-2","address-subdivision":.data.attributes."address-subdivision","identification-number":.data.attributes."identification-number","photo":.included[]|select(.type=="verification/government-id")|.attributes|select(.status=="passed")|."front-photo-url"}') 29 30 31 # Next, combine some fields into larger values. 32 FULLNAME=$(echo "$J" | jq -r '[.first,.middle,.last]|join(" ")') 33 STREET=$(echo "$J" | jq -r '[."street-1",."street-2"]|join(" ")') 34 CITY=$(echo "$J" | jq -r '[.postcode,.city,."address-subdivision,.cc"]|join(" ")') 35 36 # Download and base32-encode the photo 37 PHOTO_URL=$(echo "$J" | jq -r '.photo') 38 PHOTO_FILE=$(mktemp -t tmp.XXXXXXXXXX) 39 if [ -z "${TOKEN:-}" ] 40 then 41 wget -q --output-document=- "$PHOTO_URL" | gnunet-base32 > "${PHOTO_FILE}" 42 else 43 wget -q --output-document=- --header "Authorization: Bearer $TOKEN" "$PHOTO_URL" | gnunet-base32 > "${PHOTO_FILE}" 44 fi 45 46 # Combine into final result. 47 echo "$J" \ 48 | jq \ 49 --arg full_name "${FULLNAME}" \ 50 --arg street "${STREET}" \ 51 --arg city "${CITY}" \ 52 --rawfile photo "${PHOTO_FILE}" \ 53 '{"FORM_ID":"persona-individual",FULL_NAME: $full_name, $street, $city, DATE_OF_BIRTH: .birthdate, "residences":.cc, "identification_number": ."identification-number", $photo}' \ 54 | jq \ 55 'del(..|select(.==null))' 56 57 exit 0