class Embulk::Input::GoogleSpreadsheets::Auth

Attributes

auth_method[R]

Public Class Methods

new(task) click to toggle source
# File lib/embulk/input/google_spreadsheets/auth.rb, line 11
def initialize(task)
  @auth_method = task['auth_method']
  @json_key = task['json_keyfile']
end

Public Instance Methods

authenticate() click to toggle source
# File lib/embulk/input/google_spreadsheets/auth.rb, line 16
def authenticate
  case auth_method
  when 'authorized_user'
    key = StringIO.new(credentials.to_json)
    return Google::Auth::UserRefreshCredentials.make_creds(json_key_io: key, scope: scope)
  when 'compute_engine'
    return Google::Auth::GCECredentials.new
  when 'service_account'
    key = StringIO.new(credentials.to_json)
    return Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: key, scope: scope)
  when 'application_default'
    return Google::Auth.get_application_default([scope])
  else
    raise ConfigError.new("Unknown auth method: #{auth_method}")
  end
end
scope() click to toggle source
# File lib/embulk/input/google_spreadsheets/auth.rb, line 33
def scope
  Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY
end

Private Instance Methods

application_default_credentials_file() click to toggle source
# File lib/embulk/input/google_spreadsheets/auth.rb, line 51
def application_default_credentials_file
  @application_default_credentials_file ||=
    File.expand_path('~/.config/gcloud/application_default_credentials.json')
end
credentials() click to toggle source
# File lib/embulk/input/google_spreadsheets/auth.rb, line 39
def credentials
  JSON.parse(@json_key || File.read(credentials_file))
end
credentials_file() click to toggle source
# File lib/embulk/input/google_spreadsheets/auth.rb, line 43
def credentials_file
  @credentials_file ||= File.expand_path(
    # ref. https://developers.google.com/identity/protocols/application-default-credentials
    (File.exist?(global_application_default_credentials_file) ?
      global_application_default_credentials_file : application_default_credentials_file)
  )
end
global_application_default_credentials_file() click to toggle source
# File lib/embulk/input/google_spreadsheets/auth.rb, line 56
def global_application_default_credentials_file
  @global_application_default_credentials_file ||=
    '/etc/google/auth/application_default_credentials.json'
end