Module: LeapSalesforce::Auth
- Defined in:
- lib/leap_salesforce/auth.rb
Overview
Code related to authorising in LeapSalesforce Specific authorisation parameters are stored here. More general settings are stored at the layer above this
Constant Summary collapse
- SETUP_JWT_LINK =
Returns Setup JWT trail head.
'https://trailhead.salesforce.com/content/learn/projects/automate-cicd-with-gitlab/https://trailhead.salesforce.com/content/learn/projects/automate-cicd-with-gitlab/integrate-with-gitlab'
Class Attribute Summary collapse
-
.access_token ⇒ String
Access token taken from sfdx.
-
.instance_url ⇒ String
Instance URL taken from sfdx.
-
.sfdx_display_output ⇒ String
Output from SFDX command.
Class Method Summary collapse
-
.extract_sfdx_variables ⇒ Object
Extract sfdx variables and store them Set ENV to true to authenticate and run against sandbox.
-
.jwt_file? ⇒ TrueClass
Whether JWT file exists Exception is raised if it is not.
-
.manually_set_auth? ⇒ Boolean
Whether SFDX Access token and instance url have been manually set If not they will be attempted to be retrieved via JWT, SF_USERNAME & SF_CONSUMER_KEY.
-
.sfdx_variables? ⇒ Boolean
Whether Sfdx authentication variables are set.
Class Attribute Details
.access_token ⇒ String
Returns Access token taken from sfdx. Value is extracted if not cached. If not using sfdx this will not be stored here but within Soaspec which caches the OAuth result.
25 26 27 28 29 30 31 32 |
# File 'lib/leap_salesforce/auth.rb', line 25 def access_token return @access_token if @access_token extract_sfdx_variables raise 'Instance URL was not set correctly' if @access_token&.empty? @access_token end |
.instance_url ⇒ String
Returns Instance URL taken from sfdx. Value is extracted if not cached.
36 37 38 39 40 41 42 43 |
# File 'lib/leap_salesforce/auth.rb', line 36 def instance_url return @instance_url if @instance_url extract_sfdx_variables raise 'Instance URL was not set correctly' if @instance_url&.empty? @instance_url end |
.sfdx_display_output ⇒ String
Returns Output from SFDX command.
20 21 22 |
# File 'lib/leap_salesforce/auth.rb', line 20 def sfdx_display_output @sfdx_display_output end |
Class Method Details
.extract_sfdx_variables ⇒ Object
Extract sfdx variables and store them Set ENV to true to authenticate and run against sandbox
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/leap_salesforce/auth.rb', line 74 def extract_sfdx_variables return unless LeapSalesforce.sfdx LeapSalesforce.client_id = ENV['SF_CONSUMER_KEY'] if manually_set_auth? url_obtained = ENV['SFDX_INSTANCE_URL'] self.instance_url = url_obtained[-1] == '/' ? url_obtained[0..-2] : url_obtained self.access_token = ENV['SFDX_ACCESS_TOKEN'] return end unless ENV['SF_USERNAME'] && ENV['SF_CONSUMER_KEY'] logger.error "No CREDENTIALS FILE found at #{CREDENTIAL_FILE}." unless File.exist? CREDENTIAL_FILE raise SetupError, 'Please set SF_USERNAME and SF_CONSUMER_KEY environment variables' end # TODO: These scripts only work in a linux shell script_name = if ENV['SCRATCH_ORG'] && !ENV['SCRATCH_ORG'].empty? raise "Please set SCRATCH_ORG_ALIAS when SCRATCH_ORG(#{ENV['SCRATCH_ORG']}) is set" unless ENV['SCRATCH_ORG_ALIAS'] puts "Using sandbox #{ENV['SCRATCH_ORG_ALIAS']}" 'get_scratch_auth_jwt' else 'get_prod_auth' end puts "Using JWT folder '#{ENV['JWT_FOLDER']}'" self.sfdx_display_output = `sh #{__dir__}/#{script_name}.sh` self.access_token = from_output 'Access Token' url_obtained = from_output 'Instance Url' self.instance_url = url_obtained[-1] == '/' ? url_obtained[0..-2] : url_obtained end |
.jwt_file? ⇒ TrueClass
Returns Whether JWT file exists Exception is raised if it is not.
52 53 54 55 56 57 58 59 60 |
# File 'lib/leap_salesforce/auth.rb', line 52 def jwt_file? ENV['JWT_FOLDER'] ||= Dir.exist?('JWT') ? 'JWT' : 'assets' unless File.exist?(File.join(ENV['JWT_FOLDER'], 'server.key')) raise LeapSalesforce::SetupError, 'Please create JWT file in ' \ " '#{ENV['JWT_FOLDER']}/server.key' following #{SETUP_JWT_LINK}" end true end |
.manually_set_auth? ⇒ Boolean
Returns Whether SFDX Access token and instance url have been manually set If not they will be attempted to be retrieved via JWT, SF_USERNAME & SF_CONSUMER_KEY.
64 65 66 67 68 69 70 |
# File 'lib/leap_salesforce/auth.rb', line 64 def manually_set_auth? ENV['SFDX_ACCESS_TOKEN'] ||= ENV['SCRATCH_ACCESS_TOKEN'] ENV['SFDX_INSTANCE_URL'] ||= ENV['SCRATCH_INSTANCE_URL'] return false unless ENV['SFDX_ACCESS_TOKEN'] && ENV['SFDX_INSTANCE_URL'] !ENV['SFDX_ACCESS_TOKEN'].empty? && !ENV['SFDX_INSTANCE_URL'].empty? end |
.sfdx_variables? ⇒ Boolean
Returns Whether Sfdx authentication variables are set.
46 47 48 |
# File 'lib/leap_salesforce/auth.rb', line 46 def sfdx_variables? !(access_token && instance_url).nil? end |