licensing

This commit is contained in:
Yan
2017-11-14 18:35:14 +06:30
parent 222114a3c3
commit 93769bacb7
4 changed files with 92 additions and 65 deletions

View File

@@ -1,37 +0,0 @@
require 'openssl'
require 'base64'
require 'uri'
class AESEncDec {
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
<<<<<<< HEAD
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)
cipher.encrypt
cipher.key = ENV["aes_key"]
cipher.iv = ENV["aes_iv"]
encrypted = cipher.update(data) + cipher.final
encrypted = Base64.urlsafe_encode64(encrypted)
return encrypted
end
def self.decrypt(data)
cipher.decrypt
cipher.key = ENV["aes_key"]
cipher.iv = ENV["aes_iv"]
# Start the decryption
decoded = Base64.urlsafe_decode64(data)
decrypted = cipher.update(decoded) + cipher.final
end
}

View File

@@ -19,11 +19,8 @@ class License
self.class.base_uri server
end
# generate key for license file encrypt
# AESEncDec.export_key()
# @secret = ENV["aes_key"]
@params = { query: { device: "SX", token: SECRETS_CONFIG['provision_key'] } }
# @params = { query: { device: "SX", token: SECRETS_CONFIG['provision_key'] } }
end
def shop_code
@@ -34,14 +31,52 @@ class License
end
end
def detail_with_local_cache(lookup)
def detail_with_local_cache(lookup, key, iv)
##Check from local redis - if available load local otherwise get from remote
cache_key = "store:license:#{@secret}:hostname"
cache_key = "#{lookup}:license:#{key}:hostname"
# No Needs for current
# @secret = key
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
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: self.server_mode, lookup: lookup, encrypted_key: key, iv_key: iv} }
response = self.class.get("/subdomain", @params)
@license = response.parsed_response
if (@license["status"] == true)
assign()
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
return true
end
Rails.logger.info 'API License'
end
end
def detail_with_local_file(lookup, key, iv)
has_license = check_license_file
def detail_with_local_file(lookup)
has_license = verify_license()
if has_license
@@ -73,7 +108,6 @@ class License
Rails.logger.info 'API License'
else
@license = Marshal.load(cache_license) if cache_license
Rails.logger.info 'Cache License'
@@ -102,6 +136,20 @@ class License
return false
end
def verify_license
api_token = read_license("api_token")
@options = { query: {lookup_type: "application", token: api_token} }
response = self.class.get("/verify", @options)
@varified = response.parsed_response
Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s
if (@varified["status"])
check_expire_date
else
delete_license_file
end
end
def check_remote_license(license_key)
# @options = { query: {device: "cloud", key: license_key, skey: @secret, token: Rails.application.secrets.provision_key} }
@options = { query: {lookup_type: "application", encrypted_key: @secret, token: SECRETS_CONFIG['provision_key']} }
@@ -146,6 +194,19 @@ class License
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
end
end
end
def assign
# self.name = @license["name"]
# self.address_1 = @license["address_1"]