#!/bin/bash # OAuth Dynamic Client Registration Script for AT Protocol # Registers a new OAuth client with the AIP server per RFC 7591 # Usage: bash scripts/register-oauth-client.sh set -e # Exit on any error # Configuration AIP_BASE="${AIP_BASE_URL:-http://localhost:8081}" CLIENT_BASE_URL="${CLIENT_BASE_URL:-http://localhost:8080}" CLIENT_NAME="${CLIENT_NAME:-Slice AT Proto Client}" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" CONFIG_FILE="$ROOT_DIR/.env" echo "๐Ÿš€ OAuth Dynamic Client Registration for Slice" echo "AIP Server: $AIP_BASE" echo "Client Base URL: $CLIENT_BASE_URL" echo "Client Name: $CLIENT_NAME" echo # Check if client is already registered if [ -f "$CONFIG_FILE" ]; then echo "โš ๏ธ Existing OAuth client configuration found at $CONFIG_FILE" echo -n "Do you want to register a new client? This will overwrite the existing config. (y/N): " read -r OVERWRITE if [ "$OVERWRITE" != "y" ] && [ "$OVERWRITE" != "Y" ]; then echo "โŒ Registration cancelled" exit 1 fi fi echo "๐Ÿ” Using OAuth registration endpoint..." REGISTRATION_ENDPOINT="$AIP_BASE/oauth/clients/register" echo "โœ… Registration endpoint: $REGISTRATION_ENDPOINT" echo # Create client registration request echo "๐Ÿ“ Creating client registration request..." REDIRECT_URI="$CLIENT_BASE_URL/oauth/callback" REGISTRATION_REQUEST=$(cat </dev/null || echo "$REGISTRATION_REQUEST" echo # Register the client echo "๐Ÿ”„ Registering client with AIP server..." REGISTRATION_RESPONSE=$(curl -s -X POST "$REGISTRATION_ENDPOINT" \ -H "Content-Type: application/json" \ -d "$REGISTRATION_REQUEST" || { echo "โŒ Failed to register client with AIP server" echo "Make sure the AIP server is running at $AIP_BASE" exit 1 }) echo "Registration response:" echo "$REGISTRATION_RESPONSE" | jq '.' 2>/dev/null || echo "$REGISTRATION_RESPONSE" echo # Extract client credentials CLIENT_ID=$(echo "$REGISTRATION_RESPONSE" | grep -o '"client_id":"[^"]*' | cut -d'"' -f4) CLIENT_SECRET=$(echo "$REGISTRATION_RESPONSE" | grep -o '"client_secret":"[^"]*' | cut -d'"' -f4) if [ -z "$CLIENT_ID" ] || [ -z "$CLIENT_SECRET" ]; then echo "โŒ Failed to extract client credentials from registration response" echo "Expected client_id and client_secret in response" echo "Response was: $REGISTRATION_RESPONSE" exit 1 fi echo "โœ… Client registered successfully!" echo "Client ID: $CLIENT_ID" echo "Client Secret: [REDACTED]" echo # Save credentials to .env.oauth file echo "๐Ÿ’พ Saving client credentials to $CONFIG_FILE..." cat > "$CONFIG_FILE" <