completed SMS gateway project
This commit is contained in:
31
app/channels/application_cable/connection.rb
Normal file
31
app/channels/application_cable/connection.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
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
|
||||
Reference in New Issue
Block a user