Class: Transcriptic::Auth

Inherits:
Object
  • Object
show all
Extended by:
Thor::Shell, Helpers
Defined in:
lib/transcriptic/auth.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Helpers

json_decode, json_encode, running_on_a_mac?, running_on_windows?

Class Attribute Details

.credentialsObject

Returns the value of attribute credentials.



6
7
8
# File 'lib/transcriptic/auth.rb', line 6

def credentials
  @credentials
end

Class Method Details

.api_keyObject

:nodoc:



52
53
54
55
# File 'lib/transcriptic/auth.rb', line 52

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

.ask_for_and_save_credentialsObject



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

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

.ask_for_credentialsObject



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

def ask_for_credentials
  puts "Enter your Transcriptic credentials."

  user = ask "Email: "

  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



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

def ask_for_password
  echo_off
  password = ask "Password: "
  puts
  echo_on
  return password
end

.ask_for_password_on_windowsObject



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

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

  print "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



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

def check
  client.list_runs
end

.clearObject



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

def clear
  @credentials = nil
  @client = nil
end

.clientObject



8
9
10
11
12
13
14
# File 'lib/transcriptic/auth.rb', line 8

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



57
58
59
60
61
62
63
# File 'lib/transcriptic/auth.rb', line 57

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

.default_hostObject



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

def default_host
  "https://secure.transcriptic.com"
end

.delete_credentialsObject



158
159
160
161
# File 'lib/transcriptic/auth.rb', line 158

def delete_credentials
  FileUtils.rm_f(credentials_file)
  clear
end

.echo_offObject



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

def echo_off
  system "stty -echo"
end

.echo_onObject



81
82
83
# File 'lib/transcriptic/auth.rb', line 81

def echo_on
  system "stty echo"
end

.get_credentialsObject

:nodoc:



65
66
67
68
69
70
71
# File 'lib/transcriptic/auth.rb', line 65

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

.hostObject



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

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

.loginObject



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

def 
  delete_credentials
  get_credentials
end

.logoutObject



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

def logout
  delete_credentials
end

.read_credentialsObject



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

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

.reauthorizeObject



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

def reauthorize
  @credentials = ask_for_and_save_credentials
end

.retry_login?Boolean

Returns:

  • (Boolean)


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

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

.set_credentials_permissionsObject



153
154
155
156
# File 'lib/transcriptic/auth.rb', line 153

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

.userObject

:nodoc:



47
48
49
50
# File 'lib/transcriptic/auth.rb', line 47

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

.write_credentialsObject



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

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