add license and encryption for cloud

This commit is contained in:
Yan
2017-11-10 14:34:46 +06:30
11 changed files with 123 additions and 63 deletions

View File

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

View File

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