module Google::Auth::CredentialsLoader

def authorized_user_env_vars?

def authorized_user_env_vars?
  ([CLIENT_ID_VAR, CLIENT_SECRET_VAR, REFRESH_TOKEN_VAR] -
    ENV.keys).empty?
end

def from_env(scope = nil)

Parameters:
  • scope (string|array|nil) -- the scope(s) to access
def from_env(scope = nil)
  if ENV.key?(ENV_VAR)
    path = ENV[ENV_VAR]
    fail "file #{path} does not exist" unless File.exist?(path)
    File.open(path) do |f|
      return make_creds(json_key_io: f, scope: scope)
    end
  elsif service_account_env_vars? || authorized_user_env_vars?
    return make_creds(scope: scope)
  end
rescue StandardError => e
  raise "#{NOT_FOUND_ERROR}: #{e}"
end

def from_system_default_path(scope = nil)

Parameters:
  • scope (string|array|nil) -- the scope(s) to access
def from_system_default_path(scope = nil)
  if windows?
    return nil unless ENV['ProgramData']
    prefix = File.join(ENV['ProgramData'], 'Google/Auth')
  else
    prefix = '/etc/google/auth/'
  end
  path = File.join(prefix, CREDENTIALS_FILE_NAME)
  return nil unless File.exist?(path)
  File.open(path) do |f|
    return make_creds(json_key_io: f, scope: scope)
  end
rescue StandardError => e
  raise "#{SYSTEM_DEFAULT_ERROR}: #{e}"
end

def from_well_known_path(scope = nil)

Parameters:
  • scope (string|array|nil) -- the scope(s) to access
def from_well_known_path(scope = nil)
  home_var = windows? ? 'APPDATA' : 'HOME'
  base = WELL_KNOWN_PATH
  root = ENV[home_var].nil? ? '' : ENV[home_var]
  base = File.join('.config', base) unless windows?
  path = File.join(root, base)
  return nil unless File.exist?(path)
  File.open(path) do |f|
    return make_creds(json_key_io: f, scope: scope)
  end
rescue StandardError => e
  raise "#{WELL_KNOWN_ERROR}: #{e}"
end

def make_creds(*args)

be modified, allowing different instances to be created.
By default, it calls #new on the current class, but this behaviour can

make_creds proxies the construction of a credentials instance
def make_creds(*args)
  new(*args)
end

def service_account_env_vars?

def service_account_env_vars?
  ([PRIVATE_KEY_VAR, CLIENT_EMAIL_VAR] - ENV.keys).empty?
end

def windows?

determines if the current OS is windows
def windows?
  RbConfig::CONFIG['host_os'] =~ /Windows|mswin/
end