Class: Linguist::Command::Auth

Inherits:
Base
  • Object
show all
Defined in:
lib/linguist_ruby/commands/auth.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#args, #autodetected_app

Instance Method Summary collapse

Methods inherited from Base

#app_urls, #escape, #extract_app_from_git_config, #extract_option, #extract_project_title_from_args, #extract_project_title_from_dir_name, #extract_project_title_from_git, #git_remotes, #git_url, #initialize, #linguist, #project, #project_title

Methods included from Helpers

#ask, #confirm, #confirm_command, #deprecate, #display, #error, #format_date, #git, #has_git?, #home_directory, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #shell

Constructor Details

This class inherits a constructor from Linguist::Command::Base

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



5
6
7
# File 'lib/linguist_ruby/commands/auth.rb', line 5

def credentials
  @credentials
end

Instance Method Details

#ask_for_and_save_credentialsObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/linguist_ruby/commands/auth.rb', line 103

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

#ask_for_credentialsObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/linguist_ruby/commands/auth.rb', line 64

def ask_for_credentials
  puts "Enter your Linguist credentials."

  print "Email: "
  user = ask

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

  [user, api_key]
end

#ask_for_passwordObject



95
96
97
98
99
100
101
# File 'lib/linguist_ruby/commands/auth.rb', line 95

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  return password
end

#ask_for_password_on_windowsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/linguist_ruby/commands/auth.rb', line 77

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

#auth_tokenObject

:nodoc:



35
36
37
38
# File 'lib/linguist_ruby/commands/auth.rb', line 35

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

#checkObject

just a stub; will raise if not authenticated



22
23
24
# File 'lib/linguist_ruby/commands/auth.rb', line 22

def check
  client.projects.all
end

#clientObject



7
8
9
# File 'lib/linguist_ruby/commands/auth.rb', line 7

def client
  @client ||= init_linguist
end

#credentials_fileObject



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

def credentials_file
  "#{home_directory}/.linguist/credentials"
end

#delete_credentialsObject



142
143
144
# File 'lib/linguist_ruby/commands/auth.rb', line 142

def delete_credentials
#      FileUtils.rm_f(credentials_file)
end

#echo_offObject



56
57
58
# File 'lib/linguist_ruby/commands/auth.rb', line 56

def echo_off
  system "stty -echo"
end

#echo_onObject



60
61
62
# File 'lib/linguist_ruby/commands/auth.rb', line 60

def echo_on
  system "stty echo"
end

#get_credentialsObject

:nodoc:



44
45
46
47
48
49
50
# File 'lib/linguist_ruby/commands/auth.rb', line 44

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

#hostObject



17
18
19
# File 'lib/linguist_ruby/commands/auth.rb', line 17

def host
  ENV['LINGUIST_HOST'] || 'lingui.st'
end

#init_linguistObject



11
12
13
14
15
# File 'lib/linguist_ruby/commands/auth.rb', line 11

def init_linguist
  client = Linguist::Client.new(:username => user, :auth_token => auth_token, :host => host)
#      client.on_warning { |msg| self.display("\n#{msg}\n\n") }
  client
end

#read_credentialsObject



52
53
54
# File 'lib/linguist_ruby/commands/auth.rb', line 52

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

#reauthorizeObject



26
27
28
# File 'lib/linguist_ruby/commands/auth.rb', line 26

def reauthorize
  @credentials = ask_for_and_save_credentials
end

#retry_login?Boolean

Returns:

  • (Boolean)


122
123
124
125
126
# File 'lib/linguist_ruby/commands/auth.rb', line 122

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

#set_credentials_permissionsObject



137
138
139
140
# File 'lib/linguist_ruby/commands/auth.rb', line 137

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

#userObject

:nodoc:



30
31
32
33
# File 'lib/linguist_ruby/commands/auth.rb', line 30

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

#write_credentialsObject



128
129
130
131
132
133
134
135
# File 'lib/linguist_ruby/commands/auth.rb', line 128

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