diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5d68d8c5..087eb8fc 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -23,8 +23,8 @@ class ApplicationController < ActionController::Base end def lookup_domain - # if request.subdomain.present? && request.subdomain != "www" - @license = current_license(ENV["SX_PROVISION_URL"], "chromisreal-16") # request.subdomain.downcase + if request.subdomain.present? && request.subdomain != "www" + @license = current_license(ENV["SX_PROVISION_URL"], request.subdomain.downcase) # request.subdomain.downcase if (!@license.nil?) # logger.info "Location - " + @license.name ActiveRecord::Base.establish_connection(website_connection(@license)) @@ -35,16 +35,17 @@ class ApplicationController < ActionController::Base # redirect_to root_url(:host => request.domain) + "store_error" render :json => [{ status: false, message: 'Invalid Access!'}] end - # end + end end def current_license(url, key) @license = License.new(url, key) - ##creating md5 hash - md5_hostname = Digest::MD5.new - md5key = md5_hostname.update(request.host) - if (@license.detail_with_local_cache(key, md5key.to_s) == true) + # Export for Key + passphrase = key + ENV["secret_key_base"] + key, iv = AESEncDec.export_key(passphrase) + + if (@license.detail_with_local_file(key, key, iv) == true) #if (@license.detail == true) return @license diff --git a/app/models/AESEncDec.rb b/app/models/AESEncDec.rb index e175a3a9..b6231df7 100644 --- a/app/models/AESEncDec.rb +++ b/app/models/AESEncDec.rb @@ -5,9 +5,14 @@ require 'uri' class AESEncDec { cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") - def self.export_key - ENV['aes_key'] = cipher.key = cipher.random_key # stores the key in key, and also sets the generated key on the cipher + def self.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 self.encrypt(data) diff --git a/app/models/license.rb b/app/models/license.rb index a7365107..b32eb48f 100755 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -20,9 +20,9 @@ class License end # generate key for license file encrypt - AESEncDec.export_key() + # AESEncDec.export_key() - @secret = ENV["aes_key"] + # @secret = ENV["aes_key"] @params = { query: { device: "SX", token: SECRETS_CONFIG['provision_key'] } } end @@ -32,32 +32,32 @@ class License else return self.subdomain.upcase end - end + end - def self.check_license_file - return File.exist?("config/license.yml") - end - - def detail_with_local_cache(lookup, key) + def detail_with_local_file(lookup, key, iv) ##Check from local redis - if available load local otherwise get from remote - cache_key = "store:license:#{key}:hostname" + #cache_key = "store:license:#{key}:hostname" # No Needs for current # @secret = key - cache_license = nil + #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 + #Rails.logger.info "Cache key - " + cache_key.to_s + + has_license = check_license_file + + if has_license + + end 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: "cloud", lookup: "subdomain", token: SECRETS_CONFIG['license_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 @@ -95,7 +95,6 @@ class License end def detail - response = self.class.get("/subdomain", @options) @license = response.parsed_response @@ -151,6 +150,10 @@ class License end private + def check_license_file + return File.exist?("config/license.yml") + end + def assign # self.name = @license["name"] # self.address_1 = @license["address_1"] diff --git a/config/license.yml b/config/license.yml index 5e66e752..fc7d1355 100644 --- a/config/license.yml +++ b/config/license.yml @@ -1,5 +1,5 @@ development: - server_mode: local + server_mode: cloud license_key: IAAXHpbSWAfvlWGYpDoXvZdmuRABNGk