Class: Zend::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/zend/auth.rb

Class Method Summary collapse

Class Method Details

.apiObject



8
9
10
11
12
13
14
# 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_accountObject



100
101
102
103
# File 'lib/zend/auth.rb', line 100

def 
  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_credentialsObject



114
115
116
117
118
119
120
# File 'lib/zend/auth.rb', line 114

def ask_for_and_save_credentials
  @credentials = ask_for_credentials
  write_credentials
  verify

  @credentials
end

.ask_for_credentialsObject



105
106
107
108
109
110
111
112
# File 'lib/zend/auth.rb', line 105

def ask_for_credentials
  say 'Enter your Zendesk credentials.' if 

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

  [email, password]
end

.ask_secret(message) ⇒ Object



133
134
135
136
137
# File 'lib/zend/auth.rb', line 133

def ask_secret(message)
  HighLine.new.ask(message) do |q|
    q.echo = false
  end
end

.authentication_failedObject



122
123
124
125
126
127
128
129
130
131
# 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_credentialsObject



80
81
82
83
84
85
86
# File 'lib/zend/auth.rb', line 80

def delete_credentials
  if netrc
    netrc.delete(domain)
    netrc.save
  end
  @api, @account, @credentials = nil
end

.domainObject



25
26
27
# File 'lib/zend/auth.rb', line 25

def domain
  "#{get_account}.zendesk.com"
end

.env_zend_accountObject



41
42
43
# File 'lib/zend/auth.rb', line 41

def 
  ENV['ZEND_ACCOUNT']
end

.get_accountObject



37
38
39
# File 'lib/zend/auth.rb', line 37

def 
  @account ||= ( || )
end

.get_credentialsObject



76
77
78
# File 'lib/zend/auth.rb', line 76

def get_credentials
  @credentials ||= (read_credentials || ask_for_and_save_credentials)
end

.loginObject



16
17
18
19
# File 'lib/zend/auth.rb', line 16

def 
  delete_credentials
  get_credentials
end

.logoutObject



21
22
23
# File 'lib/zend/auth.rb', line 21

def logout
  delete_credentials
end

.netrcObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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_pathObject



51
52
53
54
55
56
57
58
59
# 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

.passwordObject



33
34
35
# File 'lib/zend/auth.rb', line 33

def password
  get_credentials[1]
end

.read_credentialsObject



88
89
90
# File 'lib/zend/auth.rb', line 88

def read_credentials
  netrc[domain] if netrc
end

.retry_login?Boolean



139
140
141
142
143
# File 'lib/zend/auth.rb', line 139

def retry_login?
  @login_attempts ||= 0
  @login_attempts += 1
  @login_attempts < 3
end

.userObject



29
30
31
# File 'lib/zend/auth.rb', line 29

def user
  get_credentials[0]
end

.verifyObject



45
46
47
48
49
# 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_credentialsObject



92
93
94
95
96
97
98
# 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