#!/bin/bash
# This file is in the public domain.
#
# Challenger's /info returns the 'id' as a Number, but the
# exchange oauth2 plugin expects it as a String.
# Additionally, we need to check that we got the expected
# "email" address type (and otherwise exit with failure),
# and finally move the .address.phone value to
# the "CONTACT_PHONE" field.
#
# Uses JQ to convert!
exec jq 'if .address_type!="email" then halt_error(4) else (. | .id?) |= (. | tostring) | .FORM_ID="challenger-email" | .FORM_VERSION=0 |  .CONTACT_EMAIL=.address.email | del(.address) | del(.address_type) end'

# Example input:
# {
#   "id": 1,
#   "address": {
#     "email": "me@example.com"
#   },
#   "address_type": "phone",
#   "expires": {
#     "t_s": 1775590216
#   }
# }
#
# Example output:
#
# {
#   "id": "1",
#   "expires": {
#     "t_s": 1775590216
#   }
#   "CONTACT_EMAIL": "me@example.com"
# }
#
