From 04ae7414625eeaa045ef54e5f32ffb00921649fa Mon Sep 17 00:00:00 2001 From: Yan Date: Wed, 11 Apr 2018 11:27:05 +0630 Subject: [PATCH] cloud for api and doemal --- .gitignore | 1 + app/controllers/api/api_controller.rb | 31 ++++++++++++++++++- .../concerns/token_verification.rb | 11 +++++-- app/models/my_aes_crypt.rb | 15 +++++++++ config/initializers/action_controller.rb | 10 +++--- 5 files changed, 60 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index e32934b1..4b1b7cee 100755 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ pickle-email-*.html /gems/* ._*.* .rbenv-version +config/initializers/* config/deploy/config/* config/puma.rb #config/shops.json diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index 5b60b5df..04dc3a72 100755 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -1,7 +1,8 @@ class Api::ApiController < ActionController::API include TokenVerification - helper_method :current_token, :current_login_employee, :get_cashier + before_action :lookup_domain + helper_method :current_token, :current_login_employee, :get_cashier private #this is base api base controller to need to inherit. @@ -21,4 +22,32 @@ class Api::ApiController < ActionController::API def current_login_employee @employee = Employee.find_by_token_session(current_token) end + + def lookup_domain + if request.subdomain.present? && request.subdomain != "www" + from = request.subdomain.downcase + "." + request.domain.downcase + @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase + if (!@license.nil?) + logger.info "Location - " + @license.dbhost + ActiveRecord::Base.establish_connection(website_connection(@license)) + # authenticate_session_token + # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema + else8 + # 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 cache_license(url, lookup) + @license = License.new(url, lookup) + + if (@license.detail_with_local_cache(lookup) == true) + return @license + else + return nil + end + end end diff --git a/app/controllers/concerns/token_verification.rb b/app/controllers/concerns/token_verification.rb index 1d700c8b..419b9b3b 100755 --- a/app/controllers/concerns/token_verification.rb +++ b/app/controllers/concerns/token_verification.rb @@ -15,8 +15,15 @@ module TokenVerification def authenticate_token authenticate_with_http_token do |token, options| - #@current_user = User.find_by(api_key: token) - Rails.logger.debug "token - " + token.to_s + # Rails.logger.debug "token - " + token.to_s + if(!options.from.nil? && options.from == "DOEMAL"){ + if(ENV["SERVER_MODE"] === "cloud"){ + from = request.subdomain.downcase + "." + request.domain.downcase + aes = MyAesCrypt.new + return aes.checkKeyForAuth(from, token) + } + } + @user = Employee.authenticate_by_token(token) if @user return true diff --git a/app/models/my_aes_crypt.rb b/app/models/my_aes_crypt.rb index 680d0624..339585e4 100644 --- a/app/models/my_aes_crypt.rb +++ b/app/models/my_aes_crypt.rb @@ -56,6 +56,21 @@ class MyAesCrypt return aes_key, aes_iv end + def checkKeyForAuth(from,token){ + file_path = "config/shops.json" + shop_data = File.read(file_path) + + shop_json = JSON.parse(shop_data) + shop_json["data"].each do |j| + if j["lookup"] == from + if(j["value"]["key"] == token){ + return true + } + end + end + return false + } + private def encrypt(data) cipher.encrypt diff --git a/config/initializers/action_controller.rb b/config/initializers/action_controller.rb index 26255b59..b063f28e 100644 --- a/config/initializers/action_controller.rb +++ b/config/initializers/action_controller.rb @@ -20,11 +20,11 @@ class ActionController::Base end else #check for license file - if check_license - current_license(ENV["SX_PROVISION_URL"]) - else - redirect_to activate_path - end + # if check_license + # current_license(ENV["SX_PROVISION_URL"]) + # else + # redirect_to activate_path + # end end end