diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ef56c79d..8d5619cd 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -46,8 +46,8 @@ class ApplicationController < ActionController::Base def current_license(url) @license = License.new(url) - if (@license.detail_with_local_file(lookup) == true) - return @license + if (@license.detail_with_local_file() == true) + puts "RUN SA BYAR" else return nil end @@ -55,9 +55,8 @@ class ApplicationController < ActionController::Base def cache_license(url, lookup) @license = License.new(url, lookup) - # Export for Key - aes = AesCrypt.new + aes = MyAesCrypt.new aes_key, aes_iv = aes.export_key(lookup) if (@license.detail_with_local_cache(lookup, aes_key, aes_iv) == true) diff --git a/app/models/aes_crypt.rb b/app/models/aes_crypt.rb deleted file mode 100644 index 8eeb6ee1..00000000 --- a/app/models/aes_crypt.rb +++ /dev/null @@ -1,38 +0,0 @@ -class AesCrypt - @cipher = "" - - def initialize - @cipher = OpenSSL::Cipher::Cipher.new(ENV["cipher_type"]) - end - - private - def export_key(passphrase) - # We want a 256 bit key symetric key based on passphrase - digest = Digest::SHA256.new - key = digest.update(passphrase) - # key = digest.digest - ENV['aes_key'] = @cipher.key = key # stores the key in key, and also sets the generated key on the @cipher - ENV['aes_iv'] = @cipher.iv = @cipher.random_iv # stores the iv in iv, and also sets the generated iv on the @cipher - return @cipher.key, @cipher.iv - end - - def encrypt(data) - cipher.encrypt - cipher.key = ENV["aes_key"] - cipher.iv = ENV["aes_iv"] - encrypted = cipher.update(data) + cipher.final - encrypted = Base64.encode64(encrypted) - return encrypted - end - - def decrypt(data) - cipher.decrypt - cipher.key = ENV["aes_key"] - cipher.iv = ENV["aes_iv"] - - # Start the decryption - decoded = Base64.decode64(data) - decrypted = cipher.update(decoded) + cipher.final - return decrypted - end -end \ No newline at end of file diff --git a/app/models/license.rb b/app/models/license.rb index 837a77f8..31403038 100755 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -13,7 +13,7 @@ class License def initialize(server = "", lookup = "") #this code is hard-code to reflect server mode - Very important. - self.server_mode = ENV["server_mode"] + self.server_mode = ENV["SERVER_MODE"] if (server != "") self.class.base_uri server @@ -50,7 +50,6 @@ class License ##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 @@ -75,49 +74,49 @@ class License end - def detail_with_local_file(lookup) - has_license = verify_license() + def detail_with_local_file() + has_license = true #verify_license() if has_license - + puts "VERIFIED" 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} } + # 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 + # response = self.class.get("/request_license", @params) + # @license = response.parsed_response - if (@license["status"] == true) + # if (@license["status"] == true) - assign() + # assign() - Rails.logger.info "License - " + response.parsed_response.to_s + # 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 + # 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 + # return true + # end - Rails.logger.info 'API License' + # Rails.logger.info 'API License' - else - @license = Marshal.load(cache_license) if cache_license + # else + # @license = Marshal.load(cache_license) if cache_license - Rails.logger.info 'Cache License' + # Rails.logger.info 'Cache License' - if (@license["status"] == true) - assign() - return true - end - end - return false + # if (@license["status"] == true) + # assign() + # return true + # end + # end + # return false end def detail @@ -144,10 +143,13 @@ class License Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s if (@varified["status"]) - check_expire_date + if (!check_expired(@varified["plan_next_renewal_date"])) + return true + end else delete_license_file - end + end + return false end def check_remote_license(license_key) @@ -181,32 +183,32 @@ class License #Load License is remove from the cloud license because - this license is must be validated against subdmain instead of license.data from file. - def expired? - if (self.plan_next_renewal_date < Date.today) + def check_expired(renewal_date) + if (renewal_date < Date.today) return true else return false end end + + def self.check_license_file + return File.exist?("config/license.yml") + end - private - def 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 = AESEncDec.decrypt(decrypted_line_array[1]) - 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 def assign # self.name = @license["name"] # self.address_1 = @license["address_1"] diff --git a/config/secrets.yml b/config/secrets.yml index 8f56c424..09555540 100755 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -15,8 +15,8 @@ development: sx_provision_url: provision.zsai.ws/api #192.168.1.94:3002 server_mode: cloud cipher_type: AES-256-CBC - aes_key: <%= ENV['aes_key'] %> - aes_iv: <%= ENV['aes_iv'] %> + aes_key: <%= ENV['AES_KEY'] %> + aes_iv: <%= ENV['AES_IV'] %> test: secret_key_base: 5c92143fd4a844fdaf8b22aba0cda22ef1fc68f1b26dd3d40656866893718ae5e58625b4c3a5dc86b04c8be0a505ec0ebc0be3bf52249a3d1e0c1334ee591cf0 @@ -27,6 +27,6 @@ production: secret_key_base: c4bc81065013f9a3506d385bcbd49586c42e586488144b0de90c7da36867de9fa880f46b5c4f86f0ce9b7c783bb5a73bdb0e5605a47716567294390e726d3e22 sx_provision_url: provision.zsai.ws/api #192.168.1.94:3002 server_mode: cloud - aes_key: <%= ENV['aes_key'] %> - aes_iv: <%= ENV['aes_iv'] %> + aes_key: <%= ENV['AES_KEY'] %> + aes_iv: <%= ENV['AES_IV'] %>