#!/bin/bash
# Step 1: Request device code
RESPONSE=$(curl -s -X POST https://api.noorle.com/oauth/device/authorize \
-H "Content-Type: application/json" \
-d '{
"client_id": "noorle-cli",
"scope": "api"
}')
DEVICE_CODE=$(echo $RESPONSE | jq -r '.device_code')
USER_CODE=$(echo $RESPONSE | jq -r '.user_code')
VERIFY_URI=$(echo $RESPONSE | jq -r '.verification_uri')
INTERVAL=$(echo $RESPONSE | jq -r '.interval')
# Step 2: Display verification instructions
echo "Please visit: $VERIFY_URI"
echo "Enter code: $USER_CODE"
# Step 3: Poll for token
while true; do
TOKEN_RESPONSE=$(curl -s -X POST https://api.noorle.com/oauth/token \
-H "Content-Type: application/json" \
-d "{
\"grant_type\": \"urn:ietf:params:oauth:grant-type:device_code\",
\"device_code\": \"$DEVICE_CODE\",
\"client_id\": \"noorle-cli\"
}")
# Check for token
if echo $TOKEN_RESPONSE | jq -e '.access_token' >/dev/null 2>&1; then
ACCESS_TOKEN=$(echo $TOKEN_RESPONSE | jq -r '.access_token')
echo "✓ Authenticated!"
echo $ACCESS_TOKEN > ~/.noorle/token
break
fi
# Check for error
if echo $TOKEN_RESPONSE | jq -e '.error' >/dev/null 2>&1; then
ERROR=$(echo $TOKEN_RESPONSE | jq -r '.error')
if [ "$ERROR" == "authorization_pending" ]; then
echo "Waiting for approval..."
elif [ "$ERROR" == "slow_down" ]; then
INTERVAL=$((INTERVAL + 5))
else
echo "Error: $ERROR"
exit 1
fi
fi
sleep $INTERVAL
done