Class: Transcriptic::Auth

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.credentialsObject

Returns the value of attribute credentials.



3
4
5
# File 'lib/transcriptic/auth.rb', line 3

def credentials
  @credentials
end

Class Method Details

.api_keyObject

:nodoc:



49
50
51
52
# File 'lib/transcriptic/auth.rb', line 49

def api_key # :nodoc:
  get_credentials
  @credentials[1]
end

.ask_for_and_save_credentialsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/transcriptic/auth.rb', line 120

def ask_for_and_save_credentials
  begin
    @credentials = ask_for_credentials
    write_credentials
    check
  rescue ::RestClient::Unauthorized, ::RestClient::ResourceNotFound => e
    delete_credentials
    clear
    display "Authentication failed."
    retry if retry_login?
    exit 1
  rescue Exception => e
    delete_credentials
    raise e
  end
end

.ask_for_credentialsObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/transcriptic/auth.rb', line 82

def ask_for_credentials
  puts "Enter your Transcriptic credentials."

  print "Email: "
  user = ask

  print "Password: "
  password = running_on_windows? ? ask_for_password_on_windows : ask_for_password
  api_key = Transcriptic::Client.auth(user, password, host)['api_key']
  [user, api_key]
end

.ask_for_passwordObject



112
113
114
115
116
117
118
# File 'lib/transcriptic/auth.rb', line 112

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  return password
end

.ask_for_password_on_windowsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/transcriptic/auth.rb', line 94

def ask_for_password_on_windows
  require "Win32API"
  char = nil
  password = ''

  while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do
    break if char == 10 || char == 13 # received carriage return or newline
    if char == 127 || char == 8 # backspace and delete
      password.slice!(-1, 1)
    else
      # windows might throw a -1 at us so make sure to handle RangeError
      (password << char.chr) rescue RangeError
    end
  end
  puts
  return password
end

.checkObject

just a stub; will raise if not authenticated



28
29
30
# File 'lib/transcriptic/auth.rb', line 28

def check
  client.list
end

.clearObject



22
23
24
25
# File 'lib/transcriptic/auth.rb', line 22

def clear
  @credentials = nil
  @client = nil
end

.clientObject



5
6
7
8
9
10
11
# File 'lib/transcriptic/auth.rb', line 5

def client
  @client ||= begin
    client = Transcriptic::Client.new(user, api_key, host)
    client.on_warning { |msg| self.display("\n#{msg}\n\n") }
    client
  end
end

.credentials_fileObject



54
55
56
57
58
59
60
# File 'lib/transcriptic/auth.rb', line 54

def credentials_file
  if host == default_host
    "#{Transcriptic.home_directory}/.transcriptic/credentials"
  else
    "#{Transcriptic.home_directory}/.transcriptic/credentials.#{host}"
  end
end

.default_hostObject



32
33
34
# File 'lib/transcriptic/auth.rb', line 32

def default_host
  "transcriptic.com"
end

.delete_credentialsObject



156
157
158
159
# File 'lib/transcriptic/auth.rb', line 156

def delete_credentials
  FileUtils.rm_f(credentials_file)
  clear
end

.echo_offObject



74
75
76
# File 'lib/transcriptic/auth.rb', line 74

def echo_off
  system "stty -echo"
end

.echo_onObject



78
79
80
# File 'lib/transcriptic/auth.rb', line 78

def echo_on
  system "stty echo"
end

.get_credentialsObject

:nodoc:



62
63
64
65
66
67
68
# File 'lib/transcriptic/auth.rb', line 62

def get_credentials    # :nodoc:
  return if @credentials
  unless @credentials = read_credentials
    ask_for_and_save_credentials
  end
  @credentials
end

.hostObject



36
37
38
# File 'lib/transcriptic/auth.rb', line 36

def host
  ENV['TRANSCRIPTIC_HOST'] || default_host
end

.loginObject



13
14
15
16
# File 'lib/transcriptic/auth.rb', line 13

def 
  delete_credentials
  get_credentials
end

.logoutObject



18
19
20
# File 'lib/transcriptic/auth.rb', line 18

def logout
  delete_credentials
end

.read_credentialsObject



70
71
72
# File 'lib/transcriptic/auth.rb', line 70

def read_credentials
  File.exists?(credentials_file) and File.read(credentials_file).split("\n")
end

.reauthorizeObject



40
41
42
# File 'lib/transcriptic/auth.rb', line 40

def reauthorize
  @credentials = ask_for_and_save_credentials
end

.retry_login?Boolean

Returns:

  • (Boolean)


137
138
139
140
141
# File 'lib/transcriptic/auth.rb', line 137

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

.set_credentials_permissionsObject



151
152
153
154
# File 'lib/transcriptic/auth.rb', line 151

def set_credentials_permissions
  FileUtils.chmod 0700, File.dirname(credentials_file)
  FileUtils.chmod 0600, credentials_file
end

.userObject

:nodoc:



44
45
46
47
# File 'lib/transcriptic/auth.rb', line 44

def user    # :nodoc:
  get_credentials
  @credentials[0]
end

.write_credentialsObject



143
144
145
146
147
148
149
# File 'lib/transcriptic/auth.rb', line 143

def write_credentials
  FileUtils.mkdir_p(File.dirname(credentials_file))
  f = File.open(credentials_file, 'w')
  f.puts self.credentials
  f.close
  set_credentials_permissions
end