#!/usr/bin/env bash # Generate JWT token for Permissioned RPC access # Usage: ./generate-jwt-token.sh [username] [expiry_days] set -euo pipefail PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}" VMID=2501 USERNAME="${1:-rpc-user}" EXPIRY_DAYS="${2:-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"; } # Check if jq is installed if ! command -v jq &> /dev/null; then error "jq is required but not installed. Install with: sudo apt install jq" exit 1 fi # Get JWT secret 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." exit 1 fi # Calculate expiry time EXPIRY=$(date -d "+${EXPIRY_DAYS} days" +%s) NOW=$(date +%s) # Create JWT payload PAYLOAD=$(jq -n \ --arg sub "$USERNAME" \ --arg iat "$NOW" \ --arg exp "$EXPIRY" \ '{sub: $sub, iat: ($iat | tonumber), exp: ($exp | tonumber)}') # Check if node is available for JWT generation if command -v node &> /dev/null; then info "Generating JWT token using Node.js..." # Create temporary script TEMP_SCRIPT=$(mktemp) cat > "$TEMP_SCRIPT" </dev/null) rm -f "$TEMP_SCRIPT" if [ -n "$TOKEN" ]; then echo "" info "JWT Token generated successfully!" echo "" echo "Token: $TOKEN" echo "" echo "Usage:" echo " curl -k -H 'Authorization: Bearer $TOKEN' \\" echo " -H 'Content-Type: application/json' \\" echo " -d '{\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\",\"params\":[],\"id\":1}' \\" echo " https://rpc-http-prv.d-bis.org" echo "" exit 0 fi fi # Fallback: Use Python if available if command -v python3 &> /dev/null; then info "Generating JWT token using Python..." TOKEN=$(python3 <