summaryrefslogtreecommitdiff
path: root/src/kyclogic/taler-exchange-kyc-kycaid-converter.sh
blob: 68a1b6a0d55eed71b930f80537744faafba1b46f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# This file is in the public domain.
#
# This code converts (some of) the JSON output from KYCAID into the GNU Taler
# specific KYC attribute data (again in JSON format).  We may need to download
# and inline file data in the process, for authorization pass "-a" with the
# respective bearer token.
#

# Die if anything goes wrong.
set -eu

# Parse command-line options
while getopts ':a:' OPTION; do
    case "$OPTION" in
        a)
            TOKEN="$OPTARG"
            ;;
        ?)
        echo "Unrecognized command line option"
        exit 1
        ;;
    esac
done

# First, extract everything from stdin.
J=$(jq '{"type":.type,"email":.email,"phone":.phone,"first_name":.first_name,"name-middle":.middle_name,"last_name":.last_name,"dob":.dob,"residence_country":.residence_country,"gender":.gender,"pep":.pep,"addresses":.addresses,"documents":.documents,"company_name":.company_name,"business_activity_id":.business_activity_id,"registration_country":.registration_country,"documents":.documents,"decline_reasons":.decline_reasons}')

# TODO:
# log_failure (json_object_get (j, "decline_reasons"));

TYPE=$(echo "$J" | jq -r '.type')

N=0
DOCS_RAW=""
DOCS_JSON=""
for ID in $(jq -r '.documents[]|select(.status=="valid")|.id')
do
    TYPE=$(jq -r ".documents[]|select(.id==\"$ID\")|.type")
    EXPIRY=$(jq -r ".documents[]|select(.id==\"$ID\")|.expiry_date")
    DOCUMENT_FILE=$(mktemp -t tmp.XXXXXXXXXX)
    # Authorization: Token $TOKEN
    DOCUMENT_URL="https://api.kycaid.com/documents/$ID"
    if [ -z "${TOKEN:-}" ]
    then
        wget -q --output-document=- "$DOCUMENT_URL" \
            | gnunet-base32 > ${DOCUMENT_FILE}
    else
        wget -q --output-document=- "$DOCUMENT_URL" \
             --header "Authorization: Token $TOKEN" \
            | gnunet-base32 > ${DOCUMENT_FILE}
    fi
    DOCS_RAW="$DOCS_RAW --rawfile photo$N \"${DOCUMENT_FILE}\""
    if [ "$N" = 0 ]
    then
        DOCS_JSON="{\"type\":\"$TYPE\",\"image\":\$photo$N}"
    else
        DOCS_JSON="{\"type\":\"$TYPE\",\"image\":\$photo$N},$DOCS_JSON"
    fi
    N=$(expr $N + 1)
done


if [ "PERSON" = "${TYPE}" ]
then

  # Next, combine some fields into larger values.
  FULLNAME=$(echo "$J" | jq -r '[.first_name,.middle_name,.last_name]|join(" ")')
#  STREET=$(echo $J | jq -r '[."street-1",."street-2"]|join(" ")')
#  CITY=$(echo $J | jq -r '[.postcode,.city,."address-subdivision,.cc"]|join(" ")')

  # Combine into final result for individual.
  echo "$J" \
      | jq \
            --arg full_name "${FULLNAME}" \
            '{$full_name,"birthdate":.dob,"pep":.pep,"phone":.phone,"email":.email,"residences":.residence_country}' \
      | jq \
            'del(..|select(.==null))'

else
  # Combine into final result for business.
    echo "$J" \
        | jq \
              $DOCS_RAW \
              "{\"company_name\":.company_name,\"phone\":.phone,\"email\":.email,\"registration_country\":.registration_country,\"documents\":[${DOCS_JSON}]}" \
        | jq \
              'del(..|select(.==null))'
fi

exit 0