class Transcriptic::Auth

Attributes

credentials[RW]

Public Class Methods

ask_for_and_save_credentials() click to toggle source
# 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_credentials() click to toggle source
# 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_password() click to toggle source
# 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_windows() click to toggle source
# 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
check() click to toggle source

just a stub; will raise if not authenticated

# File lib/transcriptic/auth.rb, line 31
def check
  client.list_runs
end
clear() click to toggle source
# File lib/transcriptic/auth.rb, line 25
def clear
  @credentials = nil
  @client = nil
end
client() click to toggle source
# 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_file() click to toggle source
# 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_host() click to toggle source
# File lib/transcriptic/auth.rb, line 35
def default_host
  "https://secure.transcriptic.com"
end
delete_credentials() click to toggle source
# File lib/transcriptic/auth.rb, line 158
def delete_credentials
  FileUtils.rm_f(credentials_file)
  clear
end
echo_off() click to toggle source
# File lib/transcriptic/auth.rb, line 77
def echo_off
  system "stty -echo"
end
echo_on() click to toggle source
# File lib/transcriptic/auth.rb, line 81
def echo_on
  system "stty echo"
end
host() click to toggle source
# File lib/transcriptic/auth.rb, line 39
def host
  ENV['TRANSCRIPTIC_HOST'] || default_host
end
login() click to toggle source
# File lib/transcriptic/auth.rb, line 16
def login
  delete_credentials
  get_credentials
end
logout() click to toggle source
# File lib/transcriptic/auth.rb, line 21
def logout
  delete_credentials
end
read_credentials() click to toggle source
# File lib/transcriptic/auth.rb, line 73
def read_credentials
  File.exists?(credentials_file) and File.read(credentials_file).split("\n")
end
reauthorize() click to toggle source
# File lib/transcriptic/auth.rb, line 43
def reauthorize
  @credentials = ask_for_and_save_credentials
end
retry_login?() click to toggle source
# File lib/transcriptic/auth.rb, line 139
def retry_login?
  @login_attempts ||= 0
  @login_attempts += 1
  @login_attempts < 3
end
set_credentials_permissions() click to toggle source
# File lib/transcriptic/auth.rb, line 153
def set_credentials_permissions
  FileUtils.chmod 0700, File.dirname(credentials_file)
  FileUtils.chmod 0600, credentials_file
end
write_credentials() click to toggle source
# 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