71 lines
1.7 KiB
Ruby
71 lines
1.7 KiB
Ruby
#!/usr/bin/env ruby
|
|
# Test script to verify gateway API key authentication
|
|
# Usage: ruby test_gateway_auth.rb <api_key>
|
|
|
|
require_relative 'config/environment'
|
|
require 'digest'
|
|
|
|
api_key = ARGV[0]
|
|
|
|
if api_key.nil? || api_key.empty?
|
|
puts "Usage: ruby test_gateway_auth.rb <api_key>"
|
|
puts ""
|
|
puts "Example:"
|
|
puts " ruby test_gateway_auth.rb gw_live_abc123..."
|
|
exit 1
|
|
end
|
|
|
|
puts "Testing API Key Authentication"
|
|
puts "=" * 60
|
|
puts "API Key: #{api_key[0..20]}..." # Show only first part for security
|
|
puts ""
|
|
|
|
# Hash the API key
|
|
api_key_digest = Digest::SHA256.hexdigest(api_key)
|
|
puts "Key Digest: #{api_key_digest[0..20]}..."
|
|
puts ""
|
|
|
|
# Find gateway
|
|
gateway = Gateway.find_by(api_key_digest: api_key_digest)
|
|
|
|
if gateway.nil?
|
|
puts "❌ FAILED: No gateway found with this API key"
|
|
puts ""
|
|
puts "Possible issues:"
|
|
puts " 1. API key is incorrect"
|
|
puts " 2. Gateway was deleted"
|
|
puts " 3. API key was regenerated"
|
|
puts ""
|
|
puts "Available active gateways:"
|
|
Gateway.where(active: true).each do |g|
|
|
puts " - #{g.name} (#{g.device_id})"
|
|
end
|
|
exit 1
|
|
end
|
|
|
|
puts "✅ Gateway Found!"
|
|
puts "-" * 60
|
|
puts "Name: #{gateway.name}"
|
|
puts "Device ID: #{gateway.device_id}"
|
|
puts "Status: #{gateway.status}"
|
|
puts "Active: #{gateway.active}"
|
|
puts "Created: #{gateway.created_at}"
|
|
puts ""
|
|
|
|
if !gateway.active
|
|
puts "⚠️ WARNING: Gateway is NOT active"
|
|
puts "The WebSocket connection will be rejected"
|
|
puts ""
|
|
puts "To activate:"
|
|
puts " Gateway.find(#{gateway.id}).update!(active: true)"
|
|
exit 1
|
|
end
|
|
|
|
puts "✅ Authentication would succeed!"
|
|
puts ""
|
|
puts "WebSocket URL to use:"
|
|
puts " ws://localhost:3000/cable?api_key=#{api_key}"
|
|
puts ""
|
|
puts "Or from network:"
|
|
puts " ws://YOUR_IP:3000/cable?api_key=#{api_key}"
|