class Zend::Auth

Public Class Methods

api() click to toggle source
# File lib/zend/auth.rb, line 8
def api
  @api ||= ZendeskAPI::Client.new do |config|
    config.url = "https://#{domain}/api/v2"
    config.username = user
    config.password = password
  end
end
ask_for_account() click to toggle source
# File lib/zend/auth.rb, line 100
def ask_for_account
  say "Enter your Zendesk account (subdomain). Set a ZEND_ACCOUNT environment variable to skip this step.\n\n"
  @account = ask 'Zendesk subdomain: '
end
ask_for_and_save_credentials() click to toggle source
# File lib/zend/auth.rb, line 114
def ask_for_and_save_credentials
  @credentials = ask_for_credentials
  write_credentials
  verify

  @credentials
end
ask_for_credentials() click to toggle source
# File lib/zend/auth.rb, line 105
def ask_for_credentials
  say 'Enter your Zendesk credentials.' if env_zend_account

  email = ask 'E-mail address: '
  password = ask_secret 'Password (typing will be hidden): '

  [email, password]
end
ask_secret(message) click to toggle source
# File lib/zend/auth.rb, line 133
def ask_secret(message)
  HighLine.new.ask(message) do |q|
    q.echo = false
  end
end
authentication_failed() click to toggle source
# File lib/zend/auth.rb, line 122
def authentication_failed
  say 'Authentication failed.'
  delete_credentials

  if retry_login?
    ask_for_and_save_credentials
  else
    exit 1
  end
end
delete_credentials() click to toggle source
# File lib/zend/auth.rb, line 80
def delete_credentials
  if netrc
    netrc.delete(domain)
    netrc.save
  end
  @api, @account, @credentials = nil
end
domain() click to toggle source
# File lib/zend/auth.rb, line 25
def domain
  "#{get_account}.zendesk.com"
end
env_zend_account() click to toggle source
# File lib/zend/auth.rb, line 41
def env_zend_account
  ENV['ZEND_ACCOUNT']
end
get_account() click to toggle source
# File lib/zend/auth.rb, line 37
def get_account
  @account ||= (env_zend_account || ask_for_account)
end
get_credentials() click to toggle source
# File lib/zend/auth.rb, line 76
def get_credentials
  @credentials ||= (read_credentials || ask_for_and_save_credentials)
end
login() click to toggle source
# File lib/zend/auth.rb, line 16
def login
  delete_credentials
  get_credentials
end
logout() click to toggle source
# File lib/zend/auth.rb, line 21
def logout
  delete_credentials
end
netrc() click to toggle source
# File lib/zend/auth.rb, line 61
def netrc
  @netrc ||= begin
    File.exists?(netrc_path) && Netrc.read(netrc_path)
  rescue => error
    if error.message =~ /^Permission bits for/
      perm = File.stat(netrc_path).mode & 0777
      Kernel.abort "Permissions #{perm} for '#{netrc_path}' are too
      open. You should run `chmod 0600 #{netrc_path}` so that your
      credentials are NOT accessible by others."
    else
      raise error
    end
  end
end
netrc_path() click to toggle source
# File lib/zend/auth.rb, line 51
def netrc_path
  default = Netrc.default_path
  encrypted = default + '.gpg'
  if File.exists?(encrypted)
    encrypted
  else
    default
  end
end
password() click to toggle source
# File lib/zend/auth.rb, line 33
def password
  get_credentials[1]
end
read_credentials() click to toggle source
# File lib/zend/auth.rb, line 88
def read_credentials
  netrc[domain] if netrc
end
retry_login?() click to toggle source
# File lib/zend/auth.rb, line 139
def retry_login?
  @login_attempts ||= 0
  @login_attempts += 1
  @login_attempts < 3
end
user() click to toggle source
# File lib/zend/auth.rb, line 29
def user
  get_credentials[0]
end
verify() click to toggle source
# File lib/zend/auth.rb, line 45
def verify
  api.users.fetch!
rescue ZendeskAPI::Error::NetworkError => e
  authentication_failed if e.to_s.include?('401')
end
write_credentials() click to toggle source
# File lib/zend/auth.rb, line 92
def write_credentials
  FileUtils.mkdir_p(File.dirname(netrc_path))
  FileUtils.touch(netrc_path)
  FileUtils.chmod(0600, netrc_path)
  netrc[domain] = @credentials
  netrc.save
end