#!/usr/bin/env bash # Generate JWT token for a specific RPC container # Usage: ./generate-jwt-token-for-container.sh [expiry_days] set -euo pipefail # Load IP configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}" VMID="${1:-}" USERNAME="${2:-rpc-user}" EXPIRY_DAYS="${3:-365}" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' info() { echo -e "${GREEN}[INFO]${NC} $1"; } warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } error() { echo -e "${RED}[ERROR]${NC} $1"; } if [ -z "$VMID" ]; then error "Usage: $0 [expiry_days]" error "Example: $0 2503 ali-full-access 365" exit 1 fi # Get JWT secret from container or saved file JWT_SECRET="" # Try to get from saved file first if [ -f "/tmp/jwt_secret_${VMID}.txt" ]; then JWT_SECRET=$(cat "/tmp/jwt_secret_${VMID}.txt") info "Using saved JWT secret for VMID $VMID" else # Try to get from container info "Retrieving JWT secret from VMID $VMID..." JWT_SECRET=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} \ "pct exec $VMID -- cat /etc/nginx/jwt_secret 2>/dev/null" || echo "") if [ -z "$JWT_SECRET" ]; then error "Failed to retrieve JWT secret. Make sure JWT authentication is configured on VMID $VMID" error "Run: ./scripts/setup-jwt-auth-all-rpc-containers.sh first" exit 1 fi fi # Calculate expiry time EXPIRY=$(date -d "+${EXPIRY_DAYS} days" +%s) NOW=$(date +%s) # Create JWT payload if command -v jq &> /dev/null; then PAYLOAD=$(jq -n \ --arg sub "$USERNAME" \ --arg iat "$NOW" \ --arg exp "$EXPIRY" \ '{sub: $sub, iat: ($iat | tonumber), exp: ($exp | tonumber)}') else # Fallback without jq PAYLOAD="{\"sub\":\"$USERNAME\",\"iat\":$NOW,\"exp\":$EXPIRY}" fi # Generate token using Python if command -v python3 &> /dev/null; then info "Generating JWT token using Python..." TOKEN=$(python3 <