#!/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
# "phone" address type (and otherwise exit with failure),
# and finally move the .address.CONTACT_PHONE value to
# the "CONTACT_PHONE" field.
#
# Uses JQ to convert!
exec jq 'if .address_type!="phone" then halt_error(4) else (. | .id?) |= (. | tostring) | .CONTACT_PHONE=.address.CONTACT_PHONE | .FORM_ID="challenger-sms" | .FORM_VERSION=0 | del(.address) | del(.address_type) end'

# Example input:
# {
#   "id": 1,
#   "address": {
#     "CONTACT_PHONE": "+4112345678"
#   },
#   "address_type": "phone",
#   "expires": {
#     "t_s": 1775590216
#   }
# }
#
# Example output:
#
# {
#   "id": "1",
#   "expires": {
#     "t_s": 1775590216
#   }
#   "CONTACT_PHONE": "+4112345678"
# }
#
