new scripts freebox: reboot, get token
This commit is contained in:
Executable
+61
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
FREEBOX_HOST="${FREEBOX_HOST:-mafreebox.freebox.fr}"
|
||||
API_BASE="${API_BASE:-http://${FREEBOX_HOST}/api/v8}"
|
||||
|
||||
APP_ID="${APP_ID:-fr.bash.reboot}"
|
||||
APP_NAME="${APP_NAME:-RebootScript}"
|
||||
APP_VERSION="${APP_VERSION:-1.0}"
|
||||
DEVICE_NAME="${DEVICE_NAME:-bash-script}"
|
||||
TOKEN_FILE="${TOKEN_FILE:-./freebox_app_token.txt}"
|
||||
|
||||
command -v curl >/dev/null || { echo "curl is required" >&2; exit 1; }
|
||||
command -v jq >/dev/null || { echo "jq is required" >&2; exit 1; }
|
||||
|
||||
echo "Requesting app authorization..."
|
||||
auth_response="$(
|
||||
curl -fsS -X POST "${API_BASE}/login/authorize/" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"app_id\":\"${APP_ID}\",
|
||||
\"app_name\":\"${APP_NAME}\",
|
||||
\"app_version\":\"${APP_VERSION}\",
|
||||
\"device_name\":\"${DEVICE_NAME}\"
|
||||
}"
|
||||
)"
|
||||
|
||||
app_token="$(jq -r '.result.app_token // empty' <<<"$auth_response")"
|
||||
track_id="$(jq -r '.result.track_id // empty' <<<"$auth_response")"
|
||||
|
||||
if [[ -z "$app_token" || -z "$track_id" ]]; then
|
||||
echo "Authorization request failed:"
|
||||
echo "$auth_response"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Approve the request on the Freebox now."
|
||||
echo "track_id: $track_id"
|
||||
|
||||
while true; do
|
||||
status_response="$(curl -fsS "${API_BASE}/login/authorize/${track_id}")"
|
||||
status="$(jq -r '.result.status // empty' <<<"$status_response")"
|
||||
|
||||
case "$status" in
|
||||
granted)
|
||||
echo "$app_token" > "$TOKEN_FILE"
|
||||
chmod 600 "$TOKEN_FILE" 2>/dev/null || true
|
||||
echo "App token saved to: $TOKEN_FILE"
|
||||
echo "APP_ID=$APP_ID"
|
||||
echo "APP_TOKEN=$app_token"
|
||||
exit 0
|
||||
;;
|
||||
denied)
|
||||
echo "Authorization denied."
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
sleep 2
|
||||
;;
|
||||
esac
|
||||
done
|
||||
Reference in New Issue
Block a user