require 'apartment/migrator' module ZuoraConnect module Controllers module Helpers extend ActiveSupport::Concern def authenticate_app_api_request start_time = Time.now if !request.headers["API-Token"].blank? @appinstance = ZuoraConnect::AppInstance.where(:api_token => request.headers["API-Token"]).first Rails.logger.debug("[#{@appinstance.id}] API REQUEST - API token") if !@appinstance.blank? check_instance else authenticate_or_request_with_http_basic do |username, password| @appinstance = ZuoraConnect::AppInstance.where(:token => password).first @appinstance ||= ZuoraConnect::AppInstance.where(:api_token => password).first Rails.logger.debug("[#{@appinstance.id}] API REQUEST - Basic Auth") if !@appinstance.blank? check_instance end end Rails.logger.debug("[#{@appinstance.blank? ? "N/A" : @appinstance.id}] Authenticate App API Request Completed In - #{(Time.now - start_time).round(2)}s") end def authenticate_connect_app_request start_time = Time.now if ZuoraConnect.configuration.mode == "Production" if request["data"] setup_instance_via_data else setup_instance_via_session end else setup_instance_via_dev_mode end #Call .data_lookup with the current session to retrieve session. In some cases session may be stored/cache in redis #so data lookup provides a model method that can be overriden per app. @appinstance.new_session(:session => @appinstance.data_lookup(:session => session)) PaperTrail.whodunnit = session["#{@appinstance.id}::user::email"] if defined?(PaperTrail) && session["#{@appinstance.id}::user::email"].present? I18n.locale = session["#{@appinstance.id}::user::locale"] ? session["#{@appinstance.id}::user::locale"] : @appinstance.locale Time.zone = session["#{@appinstance.id}::user::timezone"] ? session["#{@appinstance.id}::user::timezone"] : @appinstance.timezone Rails.logger.debug("[#{@appinstance.blank? ? "N/A" : @appinstance.id}] Authenticate App Request Completed In - #{(Time.now - start_time).round(2)}s") end def persist_connect_app_session if @appinstance.present? if defined?(Redis.current) && Rails.application.config.session_store == RedisSessionStore @appinstance.cache_app_instance else session.merge!(@appinstance.save_data) end end end def check_connect_admin! raise ZuoraConnect::Exceptions::AccessDenied.new("User is not an authorized admin for this application") if !session["#{@appinstance.id}::admin"] end def check_connect_admin return session["#{@appinstance.id}::admin"] end private def setup_instance_via_data session["valid"] |= false reset_session values = JSON.parse(ZuoraConnect::AppInstance.decrypt_response(Base64.urlsafe_decode64(request["data"]))) Rails.logger.debug("Data: #{values.to_json}") if values["param_data"] values["param_data"].each do |k ,v| params[k] = v end end session["#{values["appInstance"]}::destroy"] = values["destroy"] session["#{values["appInstance"]}::valid"] = false session["appInstance"] = values["appInstance"] if values["current_user"] session["#{values["appInstance"]}::admin"] = values["current_user"]["admin"] ? values["current_user"]["admin"] : false session["#{values["appInstance"]}::user::timezone"] = values["current_user"]["timezone"] session["#{values["appInstance"]}::user::locale"] = values["current_user"]["locale"] session["#{values["appInstance"]}::user::email"] = values["current_user"]["email"] end @appinstance = ZuoraConnect::AppInstance.where(:id => values["appInstance"].to_i).first if @appinstance.blank? Apartment::Tenant.switch!("public") Apartment::Tenant.create(values["appInstance"].to_s) @appinstance = ZuoraConnect::AppInstance.create!(:api_token => values[:api_token],:id => values["appInstance"].to_i, :access_token => values["access_token"].blank? ? values["user"] : values["access_token"], :token => values["refresh_token"] , :refresh_token => values["refresh_token"].blank? ? values["key"] : values["refresh_token"], :oauth_expires_at => values["expires"]) end Rails.logger.debug("[#{@appinstance.id}] App Params: #{values.inspect}}") if Rails.env != "production" @appinstance.access_token = values["access_token"] if !values["access_token"].blank? && @appinstance.access_token != values["access_token"] @appinstance.refresh_token = values["refresh_token"] if !values["refresh_token"].blank? && @appinstance.refresh_token != values["refresh_token"] @appinstance.oauth_expires_at = values["expires"] if !values["expires"].blank? @appinstance.api_token = values["api_token"] if !values["api_token"].blank? && @appinstance.api_token != values["api_token"] @appinstance.save end def setup_instance_via_session if !session["appInstance"].blank? @appinstance = ZuoraConnect::AppInstance.where(:id => session["appInstance"]).first else raise ZuoraConnect::Exceptions::SessionInvalid.new("Session Blank -- Relaunch Application") end end def setup_instance_via_dev_mode session["appInstance"] = ZuoraConnect.configuration.dev_mode_appinstance user = ZuoraConnect.configuration.dev_mode_user key = ZuoraConnect.configuration.dev_mode_pass session["valid"] = true values = {:user => user , :key => key, :appinstance => session["appInstance"]} @appinstance = ZuoraConnect::AppInstance.where(:id => values[:appinstance].to_i).first if @appinstance.blank? Apartment::Tenant.switch!("public") Apartment::Tenant.create(values[:appinstance].to_s) @appinstance = ZuoraConnect::AppInstance.create!(:id => values[:appinstance].to_i, :access_token => values[:user], :refresh_token => values[:key]) end if @appinstance.access_token.blank? || @appinstance.refresh_token.blank? @appinstance.update_attributes(:access_token => values["user"], :refresh_token => values["key"]) end session["#{@appinstance.id}::admin"] = ZuoraConnect.configuration.dev_mode_admin end #API ONLY def check_instance if !@appinstance.blank? @appinstance.new_session(:session => @appinstance.data_lookup(:session => session)) Thread.current[:appinstance] = @appinstance PaperTrail.whodunnit = "API User" if defined?(PaperTrail) return @appinstance.valid else render text: "Access Denied", status: :unauthorized end end end end end