adding license

This commit is contained in:
Yan
2017-11-14 15:11:16 +06:30
parent a6252923f1
commit 683982d4f5
4 changed files with 35 additions and 26 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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"]

View File

@@ -1,5 +1,5 @@
development:
server_mode: local
server_mode: cloud
license_key: IAAXHpbSWAfvlWGYpDoXvZdmuRABNGk