From a70f9a43e6a97769661b3c2ffe1881afc75386d5 Mon Sep 17 00:00:00 2001 From: Yan Date: Fri, 10 Nov 2017 14:11:10 +0630 Subject: [PATCH] license --- app/controllers/application_controller.rb | 9 ++++----- app/models/AESEncDec.rb | 8 ++++---- app/models/license.rb | 13 ++++++++++--- config/initializers/license.rb | 0 config/license.yml | 0 5 files changed, 18 insertions(+), 12 deletions(-) mode change 100755 => 100644 config/initializers/license.rb mode change 100755 => 100644 config/license.yml diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index aab3f86f..da9fde0a 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception # lookup domain for db from provision - before_action :lookup_domain, :set_locale + before_action :check_license, :lookup_domain, :set_locale helper_method :current_company,:current_login_employee,:current_user # alias_method :current_user, :current_login_employee,:current_user @@ -94,10 +94,9 @@ class ApplicationController < ActionController::Base private def check_license - # if current_company.nil? - # redirect_to install_path - # end - if !File.directory?("/config/license.yml") + if License.check_license_file + + else redirect_to install_path end end diff --git a/app/models/AESEncDec.rb b/app/models/AESEncDec.rb index ec84dcb7..e175a3a9 100644 --- a/app/models/AESEncDec.rb +++ b/app/models/AESEncDec.rb @@ -5,12 +5,12 @@ require 'uri' class AESEncDec { cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") - def export_key + 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 ENV['aes_iv'] = cipher.iv = cipher.random_iv # stores the iv in iv, and also sets the generated iv on the cipher end - def encrypt(data) + def self.encrypt(data) cipher.encrypt cipher.key = ENV["aes_key"] cipher.iv = ENV["aes_iv"] @@ -19,13 +19,13 @@ class AESEncDec { return encrypted end - def decrypt + def self.decrypt(data) cipher.decrypt cipher.key = ENV["aes_key"] cipher.iv = ENV["aes_iv"] # Start the decryption - decoded = Base64.urlsafe_decode64(encrypted) + decoded = Base64.urlsafe_decode64(data) decrypted = cipher.update(decoded) + cipher.final end } \ No newline at end of file diff --git a/app/models/license.rb b/app/models/license.rb index d7c23fbf..6089218a 100755 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -19,7 +19,10 @@ class License self.class.base_uri server end - @secret = SecureRandom.hex(10) + # generate key for license file encrypt + AESCrypt.export_key() + + @secret = ENV["aes_key"] @params = { query: { device: "SX", token: SECRETS_CONFIG['provision_key'] } } end @@ -31,6 +34,10 @@ class License end end + def self.check_license_file + return File.exist?("config/license.yml") + end + def detail_with_local_cache(lookup, key) ##Check from local redis - if available load local otherwise get from remote cache_key = "store:license:#{key}:hostname" @@ -50,7 +57,7 @@ class License if cache_license.nil? ##change the d/e key # @options = { query: {device: "SXlite", lookup: lookup, skey: @secret, token: SECRETS_CONFIG['provision_key']} } - @params = { query: { device: "SXlite", token: SECRETS_CONFIG['license_key']} } + @params = { query: { lookup_type: "cloud", lookup: "subdomain", token: SECRETS_CONFIG['license_key']} } response = self.class.get("/request_license", @params) @license = response.parsed_response @@ -106,7 +113,7 @@ class License def check_remote_license(license_key) # @options = { query: {device: "cloud", key: license_key, skey: @secret, token: Rails.application.secrets.provision_key} } - @options = { query: {device: "SX", key: license_key, skey: @secret, token: SECRETS_CONFIG['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 diff --git a/config/initializers/license.rb b/config/initializers/license.rb old mode 100755 new mode 100644 diff --git a/config/license.yml b/config/license.yml old mode 100755 new mode 100644