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

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

View File

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

View File

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

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

View File

@@ -1,17 +1,36 @@
<form>
<div class="form-group">
<label for="exampleInputEmail1">Business Name</label>
<input type="text" class="form-control" id="restaurant_name" aria-describedby="business_name" placeholder="Enter business name">
<small id="business_name" class="form-text text-muted">Name of business this system is license to</small>
<form action="/install" method="POST" class="row">
<div class="col-md-6">
<div class="form-group">
<label for="exampleInputEmail1">Business Name</label>
<input type="text" class="form-control" name="restaurant_name" aria-describedby="business_name" placeholder="Enter business name">
<small id="business_name" class="form-text text-muted">Name of business this system is license to</small>
</div>
<div class="form-group">
<label for="lblLicenseKey">License Key</label>
<input type="text" class="form-control" name="license_key" aria-describedby="license_key" placeholder="Add License Key">
<small class="form-text text-muted">Add License Key from Email</small>
</div>
<div class="form-group">
<label for="lblAdministrator">Administrator Username</label>
<input type="text" class="form-control" name="admin_user" aria-describedby="admin_user" placeholder="Administrator Username">
<small id="admin_user" class="form-text text-muted">First Employee who will be assign as administrator</small>
</div>
<div class="form-group">
<label for="admin_password">Password</label>
<input type="password" class="form-control" name="admin_password" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="lblAdministrator">Administrator Username</label>
<input type="text" class="form-control" id="admin_user" aria-describedby="admin_user" placeholder="Administrator Username">
<small id="admin_user" class="form-text text-muted">First Employee who will be assign as administrator</small>
<div class="col-md-6">
<div class="form-group">
<label for="lblAdministrator">Database Username</label>
<input type="text" class="form-control" name="db_user" aria-describedby="db_user" placeholder="Database Username">
</div>
<div class="form-group">
<label for="admin_password">Database Password</label>
<input type="password" class="form-control" name="db_password" placeholder="Database Password">
</div>
</div>
<div class="form-group">
<label for="admin_password">Password</label>
<input type="password" class="form-control" id="admin_password" placeholder="Password">
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-primary">Activate</button>
</div>
<button type="submit" class="btn btn-primary">Activate</button>
</form>

View File

@@ -1,8 +1,5 @@
<div class="row">
<div class="col-lg-3 col-md-3"></div>
<div class="col-lg-6 col-md-6 ">
<div class="card">
<div class="card col-md-12">
<div class="card-block">
<h4 class="card-title text-center">License Activation</h4>
<br/>
@@ -11,7 +8,4 @@
<%= render "install/form" %>
</div>
</div>
</div>
<div class="col-lg-3 col-md-3"></div>
</div>

View File

@@ -10,11 +10,26 @@
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<style>
.page {
padding-left: 0;
max-width: 80%;
margin: 2% auto;
overflow-x: hidden;
background-color: #2790a5;
}
.page .box {
display: block;
width: 100%;
color: #000;
}
</style>
</head>
<body>
<div class="container-fluid">
<body class="page">
<div class="box">
<%= yield %>
</div>

View File

@@ -1,4 +1,4 @@
config = YAML.load_file(Rails.root.join("config/sx.yml"))
config = YAML.load_file(Rails.root.join("config/license.yml"))
config.fetch(Rails.env, {}).each do |key, value|
ENV[key.upcase] = value.to_s
end

5
config/sx.yml → config/license.yml Executable file → Normal file
View File

@@ -1,7 +1,7 @@
development:
server_mode: cloud #local
sx_provision_url: http://192.168.1.162:3005/api
server_mode: local
license_key: IAAXHpbSWAfvlWGYpDoXvZdmuRABNGk
test:
sx_provision_url: secure.smartsales.asia/api
@@ -10,6 +10,5 @@ test:
# instead read values from the environment.
production:
server_mode: cloud
sx_provision_url: secure.smartsales.asia/api
license_key: IAAXHpbSWAfvlWGYpDoXvZdmuRABNGk

View File

@@ -12,6 +12,7 @@
development:
secret_key_base: b61d85f8ed2a1a9e0eeece3443b3e8f838d002cc1d9f32115d8e93db920e2957adfedc57501d44741211538f3108b742cdeada87d5bfae796c53da1f90a3cd61
sx_provision_url: secure.smartsales.asia/api
aes_key: <%= ENV['aes_key'] %>
aes_iv: <%= ENV['aes_iv'] %>
@@ -22,6 +23,7 @@ test:
# instead read values from the environment.
production:
secret_key_base: c4bc81065013f9a3506d385bcbd49586c42e586488144b0de90c7da36867de9fa880f46b5c4f86f0ce9b7c783bb5a73bdb0e5605a47716567294390e726d3e22
sx_provision_url: secure.smartsales.asia/api
aes_key: <%= ENV['aes_key'] %>
aes_iv: <%= ENV['aes_iv'] %>