Module: Google::Auth::CredentialsLoader

Extended by:
Memoist
Included in:
DefaultCredentials, ServiceAccountCredentials, ServiceAccountJwtHeaderCredentials, UserRefreshCredentials
Defined in:
lib/googleauth/credentials_loader.rb

Overview

CredentialsLoader contains the behaviour used to locate and find default credentials files on the file system.

Constant Summary collapse

ENV_VAR =
'GOOGLE_APPLICATION_CREDENTIALS'
NOT_FOUND_ERROR =
"Unable to read the credential file specified by #{ENV_VAR}"
WELL_KNOWN_PATH =
'gcloud/application_default_credentials.json'
WELL_KNOWN_ERROR =
'Unable to read the default credential file'

Instance Method Summary collapse

Instance Method Details

#from_env(scope = nil) ⇒ Object

Creates an instance from the path specified in an environment variable.

Parameters:

  • scope (string|array|nil) (defaults to: nil)

    the scope(s) to access



65
66
67
68
69
70
71
72
73
74
# File 'lib/googleauth/credentials_loader.rb', line 65

def from_env(scope = nil)
  return nil unless 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(f, scope)
  end
rescue StandardError => e
  raise "#{NOT_FOUND_ERROR}: #{e}"
end

#from_well_known_path(scope = nil) ⇒ Object

Creates an instance from a well known path.

Parameters:

  • scope (string|array|nil) (defaults to: nil)

    the scope(s) to access



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/googleauth/credentials_loader.rb', line 79

def from_well_known_path(scope = nil)
  home_var, base = windows? ? 'APPDATA' : 'HOME', 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(f, scope)
  end
rescue StandardError => e
  raise "#{WELL_KNOWN_ERROR}: #{e}"
end

#make_creds(*args) ⇒ Object

make_creds proxies the construction of a credentials instance

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



57
58
59
# File 'lib/googleauth/credentials_loader.rb', line 57

def make_creds(*args)
  new(*args)
end

#windows?Boolean

determines if the current OS is windows

Returns:

  • (Boolean)


48
49
50
# File 'lib/googleauth/credentials_loader.rb', line 48

def windows?
  RbConfig::CONFIG['host_os'] =~ /Windows|mswin/
end