diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 896d0b5b..d378a694 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 @@ -42,11 +42,12 @@ class ApplicationController < ActionController::Base @license = License.new(url, subdomain) ##creating md5 hash - md5_hostname = Digest::MD5.new - md5key = md5_hostname.update(request.host) - if (@license.detail_with_local_cache(subdomain, md5key.to_s) == true) - #if (@license.detail == true) + # md5_hostname = Digest::MD5.new + # md5key = md5_hostname.update(request.host) + # if (@license.detail_with_local_cache(subdomain, md5key.to_s) == true) + #if (@license.detail == true) + if (@license.detail_with_local_cache(subdomain) == true) return @license else return nil @@ -94,7 +95,11 @@ class ApplicationController < ActionController::Base private def check_license - if current_company.nil? + if License.check_license_file + # if !License.check_license_valid + # redirect_to install_path + # end + else redirect_to install_path end end diff --git a/app/controllers/install_controller.rb b/app/controllers/install_controller.rb index 1ffc0b2d..cf16adfe 100755 --- a/app/controllers/install_controller.rb +++ b/app/controllers/install_controller.rb @@ -1,8 +1,42 @@ class InstallController < BaseController + def index + end - def index + def create + restaurant = params[:restaurant_name] + license_key = params[:license_key] + admin_user = params[:admin_user] + admin_password = params[:admin_password] + end + + def lookup_domain + if request.subdomain.present? && request.subdomain != "www" + @license = current_license(ENV["SX_PROVISION_URL"], request.subdomain.downcase) + if (!@license.nil?) + # logger.info "Location - " + @license.name + ActiveRecord::Base.establish_connection(website_connection(@license)) + # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema + else + # reconnect_default_db + logger.info 'License is nil' + # redirect_to root_url(:host => request.domain) + "store_error" + render :json => [{ status: false, message: 'Invalid Access!'}] + end + end end - def create + 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) + #if (@license.detail == true) + + return @license + else + return nil + end end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 037767c9..27024d75 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -6,19 +6,5 @@ module ApplicationHelper when :error then "alert alert-error fade-in" when :alert then "alert alert-error fade-in" end - end - - # For Pageless - # def pageless(total_pages, url=nil, container=nil) - # opts = { - # :totalPages => total_pages, - # :url => url, - # :loaderMsg => 'Loading more pages...', - # :loaderImage => image_path('load.gif') - # } - - # container && opts[:container] ||= container - - # javascript_tag("$('#{container}').pageless(#{opts.to_json});") - # end + 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 2a8fca4e..537e1221 100755 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -13,14 +13,17 @@ class License def initialize(server = "", lookup = "") #this code is hard-code to reflect server mode - Very important. - self.server_mode = "cloud" + self.server_mode = ENV["server_mode"] if (server != "") self.class.base_uri server end - @secret = SecureRandom.hex(10) - @params = { query: { device: "SXlite", token: SECRETS_CONFIG['provision_key'] } } + # generate key for license file encrypt + AESCrypt.export_key() + + @secret = ENV["aes_key"] + @params = { query: { device: "SX", token: SECRETS_CONFIG['provision_key'] } } end def shop_code @@ -31,9 +34,13 @@ class License end end - def detail_with_local_cache(lookup, key) + def self.check_license_file + return File.exist?("config/license.yml") + end + + def detail_with_local_cache(lookup) ##Check from local redis - if available load local otherwise get from remote - cache_key = "store:license:#{key}:hostname" + cache_key = "store:license:#{@secret}:hostname" # No Needs for current # @secret = key @@ -49,8 +56,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: { lookup_type: "cloud", lookup: lookup, encrypted_key: SECRETS_CONFIG['provision_key']} } + @params = { query: { lookup_type: "cloud", lookup: lookup, encrypted_key: @secret } } response = self.class.get("/request_license", @params) @license = response.parsed_response @@ -106,7 +112,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: "SXlite", 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 @@ -120,7 +126,7 @@ class License end def verify_by_api_token(api_token) - @options = { query: {device: "SXlite", api_token: api_token, skey: @secret, token: SECRETS_CONFIG['provision_key']} } + @options = { query: {device: "SX", api_token: api_token, skey: @secret, token: SECRETS_CONFIG['provision_key']} } response = self.class.get("/verify", @options) @license = response.parsed_response diff --git a/app/views/install/_form.html.erb b/app/views/install/_form.html.erb index e1b78851..288c431a 100755 --- a/app/views/install/_form.html.erb +++ b/app/views/install/_form.html.erb @@ -1,17 +1,36 @@ -