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'
PRIVATE_KEY_VAR =
'GOOGLE_PRIVATE_KEY'
CLIENT_EMAIL_VAR =
'GOOGLE_CLIENT_EMAIL'
CLIENT_ID_VAR =
'GOOGLE_CLIENT_ID'
CLIENT_SECRET_VAR =
'GOOGLE_CLIENT_SECRET'
REFRESH_TOKEN_VAR =
'GOOGLE_REFRESH_TOKEN'
ACCOUNT_TYPE_VAR =
'GOOGLE_ACCOUNT_TYPE'
CREDENTIALS_FILE_NAME =
'application_default_credentials.json'
NOT_FOUND_ERROR =
"Unable to read the credential file specified by #{ENV_VAR}"
WELL_KNOWN_PATH =
"gcloud/#{CREDENTIALS_FILE_NAME}"
WELL_KNOWN_ERROR =
'Unable to read the default credential file'
SYSTEM_DEFAULT_ERROR =
'Unable to read the system 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



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

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  || authorized_user_env_vars?
    return make_creds(scope: scope)
  end
rescue StandardError => e
  raise "#{NOT_FOUND_ERROR}: #{e}"
end

#from_system_default_path(scope = nil) ⇒ Object

Creates an instance from the system default path

Parameters:

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

    the scope(s) to access



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/googleauth/credentials_loader.rb', line 110

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

#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



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/googleauth/credentials_loader.rb', line 93

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

#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.



68
69
70
# File 'lib/googleauth/credentials_loader.rb', line 68

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

#windows?Boolean

determines if the current OS is windows

Returns:

  • (Boolean)


59
60
61
# File 'lib/googleauth/credentials_loader.rb', line 59

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