32 lines
742 B
Ruby
32 lines
742 B
Ruby
module ApplicationCable
|
|
class Connection < ActionCable::Connection::Base
|
|
identified_by :current_gateway
|
|
|
|
def connect
|
|
self.current_gateway = find_verified_gateway
|
|
logger.info "Gateway #{current_gateway.device_id} connected"
|
|
end
|
|
|
|
private
|
|
|
|
def find_verified_gateway
|
|
# Get API key from request params
|
|
api_key = request.params[:api_key]
|
|
|
|
if api_key.blank?
|
|
reject_unauthorized_connection
|
|
end
|
|
|
|
# Hash the API key and find gateway
|
|
api_key_digest = Digest::SHA256.hexdigest(api_key)
|
|
gateway = Gateway.find_by(api_key_digest: api_key_digest, active: true)
|
|
|
|
if gateway
|
|
gateway
|
|
else
|
|
reject_unauthorized_connection
|
|
end
|
|
end
|
|
end
|
|
end
|