Class: Zillabyte::Auth

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/zillabyte/auth.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Helpers

ask, display, error, format_with_bang, longest, read_multiline, with_tty

Class Attribute Details

.credentials ⇒ Object

Returns the value of attribute credentials.



10
11
12
# File 'lib/zillabyte/auth.rb', line 10

def credentials
  @credentials
end

Class Method Details

.api_key ⇒ Object

get_credentials end



54
55
56
# File 'lib/zillabyte/auth.rb', line 54

def api_key
  get_credentials
end

.ask_for_and_save_credentials ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/zillabyte/auth.rb', line 139

def ask_for_and_save_credentials
  begin
    @credentials = ask_for_credentials
    write_credentials
    # check
  end
  @credentials
end

.ask_for_credentials ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/zillabyte/auth.rb', line 125

def ask_for_credentials
  puts "Enter your Zillabyte credentials."
  print "Email: "
  $stdout.flush()
  email = ask
  print "Auth Token: "
  $stdout.flush()
  auth_token = ask
  print "\n" 
  
  auth_token
  
end

.default_host ⇒ Object

just a stub; will raise if not authenticated def check api.get_user end



34
35
36
# File 'lib/zillabyte/auth.rb', line 34

def default_host
  ENV['ZILLABYTE_API_HOST'] || 'api.zillabyte.com'
end

.delete_credentials ⇒ Object



62
63
64
65
66
# File 'lib/zillabyte/auth.rb', line 62

def delete_credentials
  netrc.delete(host)
  netrc.save
  @credentials = nil
end

.echo_off ⇒ Object



113
114
115
116
117
# File 'lib/zillabyte/auth.rb', line 113

def echo_off
  with_tty do
    system "stty -echo"
  end
end

.echo_on ⇒ Object



119
120
121
122
123
# File 'lib/zillabyte/auth.rb', line 119

def echo_on
  with_tty do
    system "stty echo"
  end
end

.get_credentials ⇒ Object

:nodoc:



58
59
60
# File 'lib/zillabyte/auth.rb', line 58

def get_credentials    # :nodoc:
  @credentials ||= (read_credentials || ask_for_and_save_credentials)
end

.get_credentials! ⇒ Object



15
16
17
# File 'lib/zillabyte/auth.rb', line 15

def get_credentials!
  
end

.host ⇒ Object



38
39
40
# File 'lib/zillabyte/auth.rb', line 38

def host
  default_host
end

.login ⇒ Object



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

def 
  delete_credentials
  get_credentials
end

.logout ⇒ Object



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

def logout
  delete_credentials
end

.netrc ⇒ Object

:nodoc:



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/zillabyte/auth.rb', line 78

def netrc   # :nodoc:
  @netrc ||= begin
    Netrc.read(netrc_path)
  rescue => error
    if error.message =~ /^Permission bits for/
      perm = File.stat(netrc_path).mode & 0777
      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 ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/zillabyte/auth.rb', line 68

def netrc_path
  default = Netrc.default_path
  encrypted = default + ".gpg"
  if File.exists?(encrypted)
    encrypted
  else
    default
  end
end

.read_credentials ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zillabyte/auth.rb', line 91

def read_credentials
  if ENV['ZILLABYTE_API_KEY']
    ENV['ZILLABYTE_API_KEY']
  else
    # read netrc credentials if they exist
    if netrc
      if netrc[host]
        netrc[host][1]
      else
        nil
      end
    end
  end
end

.reauthorize ⇒ Object



42
43
44
# File 'lib/zillabyte/auth.rb', line 42

def reauthorize
  @credentials = ask_for_and_save_credentials
end

.retry_login? ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
# File 'lib/zillabyte/auth.rb', line 148

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

.write_credentials ⇒ Object



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

def write_credentials
  FileUtils.mkdir_p(File.dirname(netrc_path))
  FileUtils.touch(netrc_path)
  netrc[host] = ["login", self.credentials]
  netrc.save
end