Files
sx-fc/app/models/license.rb
2017-11-15 17:55:51 +06:30

270 lines
8.2 KiB
Ruby
Executable File

class License
include HTTParty
base_uri "provision.zsai.ws/api"
attr_accessor :name, :address_1, :address_2, :township, :city, :country, :email, :phone, :fax, :logo, :subdomain,
:plan_activation_date, :plan_next_renewal_date, :plan_max_products,:plan_max_customers, :plan_active_connections,
:dbhost, :dbschema, :dbusername, :dbpassword, :exchange_unqiue_id, :localqueue_host,:server_mode,:localhost_address,
:localqueue_user, :localqueue_password, :remotequeue_host, :remotequeue_user, :remotequeue_password, :api_token, :app_token
@license = nil
@secret = nil
def initialize(server = "", lookup = "")
#this code is hard-code to reflect server mode - Very important.
self.server_mode = ENV["SERVER_MODE"]
if (server != "")
self.class.base_uri server
end
# @secret = ENV["aes_key"]
# @params = { query: { device: "SX", token: SECRETS_CONFIG['provision_key'] } }
end
def shop_code
if ( self.subdomain.length > 3)
return self.subdomain[0,3].upcase
else
return self.subdomain.upcase
end
end
def detail_with_local_cache(lookup, key, iv)
##Check from local redis - if available load local otherwise get from remote
cache_key = "#{lookup}:license:#{key}:hostname"
# No Needs for current
# @secret = key
cache_license = nil
##Get redis connection from connection pool
Redis.current do |conn|
cache_license = conn.get(cache_key)
end
Rails.logger.info "Cache key - " + cache_key.to_s
if cache_license.nil?
##change the d/e key
# @options = { query: {device: "SXlite", lookup: lookup, skey: @secret, token: SECRETS_CONFIG['provision_key']} }
@params = { query: { lookup_type: self.server_mode, lookup: lookup, encrypted_key: key, iv_key: iv} }
response = self.class.get("/subdomain", @params)
@license = response.parsed_response
if (@license["status"] == true)
assign()
Rails.logger.info "License - " + response.parsed_response.to_s
Redis.current do |conn|
##Remote - store the remote response in local redis cache
conn.set(cache_key, Marshal.dump(@license))
##ADD to List to remove later
conn.sadd("License:cache:keys", cache_key)
end
return true
end
Rails.logger.info 'API License'
end
end
def detail_with_local_file()
has_license = true #verify_license()
if has_license
puts "VERIFIED"
end
end
# if cache_license.nil?
# ##change the d/e key
# @params = { query: { lookup_type: self.server_mode, lookup: lookup, encrypted_key: key, iv_key: iv} }
# response = self.class.get("/request_license", @params)
# @license = response.parsed_response
# if (@license["status"] == true)
# assign()
# Rails.logger.info "License - " + response.parsed_response.to_s
# Redis.current do |conn|
# ##Remote - store the remote response in local redis cache
# conn.set(cache_key, Marshal.dump(@license))
# ##ADD to List to remove later
# conn.sadd("License:cache:keys", cache_key)
# end
# return true
# end
# Rails.logger.info 'API License'
# else
# @license = Marshal.load(cache_license) if cache_license
# Rails.logger.info 'Cache License'
# if (@license["status"] == true)
# assign()
# return true
# end
# end
# return false
end
# def detail
# response = self.class.get("/subdomain", @options)
# @license = response.parsed_response
# Rails.logger.debug "License - " + response.parsed_response.to_s
# if (@license["status"] == true)
# assign()
# return true
# end
# return false
# end
# License Activation
def license_activate
end
def verify_license
api_token = read_license("api_token")
@params = { query: {lookup_type: "application", token: api_token} }
response = self.class.get("/verify", @params)
@varified = response.parsed_response
Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s
if (@varified["status"])
if (!check_expired(@varified["plan_next_renewal_date"]))
return true
end
else
delete_license_file
end
return false
end
# def check_remote_license(license_key)
# # @options = { query: {device: "cloud", key: license_key, skey: @secret, token: Rails.application.secrets.provision_key} }
# @options = { query: {lookup_type: "application", encrypted_key: @secret, token: SECRETS_CONFIG['provision_key']} }
# response = self.class.get("/license", @options)
# @license = response.parsed_response
# Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s
# if (@license["status"])
# assign()
# end
# return @license["status"]
# end
# def verify_by_api_token(api_token)
# @options = { query: {device: "SX", api_token: api_token, skey: @secret, token: SECRETS_CONFIG['provision_key']} }
# response = self.class.get("/verify_token", @options)
# @license = response.parsed_response
# Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s
# if (@license["status"])
# assign()
# end
# return @license["status"]
# end
#Load License is remove from the cloud license because - this license is must be validated against subdmain instead of license.data from file.
# Check License expired date from PROVISION SERVER
def check_expired(renewal_date)
if (renewal_date < Date.today)
return true
else
return false
end
end
# Check License File exists
def self.check_license_file
return File.exist?("config/license.yml")
end
# read line by key for license file
def read_license(key)
decrypted_line = ""
if File.exist?("config/license.yml")
File.open("config/license.yml").each do |line|
if line.include? (key)
decrypted_line_array = line.split(":")
decrypted_line = AESCrypt.decrypt(decrypted_line_array[1])
end
end
end
end
private
# Delete License File
def delete_license_file
File.delete("config/license.yml") if File.exist?("config/license.yml")
end
# Assign db info for Cloud
def assign
# self.name = @license["name"]
# self.address_1 = @license["address_1"]
# self.address_2 = @license["address_2"]
# self.township = @license["township"]
# self.city = @license["city"]
# self.country = @license["country"]
# self.email = @license["email"]
# self.phone = @license["phone"]
# self.fax = @license["fax"]
# self.logo = @license["logo"]
# self.localhost_address = @license["localhost_address"]
# self.subdomain = @license["subdomain"]
# self.plan_activation_date = Date.parse(@license["plan_activation_date"])
# self.plan_next_renewal_date = Date.parse(@license["plan_next_renewal_date"])
## self.plan_activation_date = Date.strptime(@license["plan_activation_date"], "%Y-%m-%d")
## self.plan_next_renewal_date = Date.strptime(@license["plan_next_renewal_date"], "%Y-%m-%d")
# self.plan_max_products = @license["plan_max_products"].to_i
# self.plan_max_customers = @license["plan_max_customers"].to_i
# self.plan_active_connections = @license["plan_active_connections"].to_i
salt = @license["secret_key"]
if (@license["dbhost"] || @license["dbschema"] || @license["dbusername"] || @license["dbpassword"] )
self.dbhost = AESCrypt.decrypt(@license["dbhost"], salt)
self.dbschema = AESCrypt.decrypt(@license["dbschema"], salt)
self.dbusername = AESCrypt.decrypt(@license["dbusername"], salt)
self.dbpassword = AESCrypt.decrypt(@license["dbpassword"], salt)
end
# self.exchange_unqiue_id = @license["exchange_unqiue_id"]
# self.localqueue_host= @license["localqueue_host"]
# self.localqueue_user= @license["localqueue_user"]
# self.localqueue_password= @license["localqueue_password"]
# self.remotequeue_host = @license["remotequeue_host"]
# self.remotequeue_user = @license["remotequeue_user"]
# self.remotequeue_password = @license["remotequeue_password"]
# self.api_token = @license["api_token"]
# self.app_token = @license["app_token"]
end
end