Class: Hub::GitHubAPI::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/hub/github_api.rb

Overview

Provides authentication info per GitHub host such as username, password, and API/OAuth tokens.

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Configuration

Returns a new instance of Configuration.



300
301
302
303
304
# File 'lib/hub/github_api.rb', line 300

def initialize store
  @data = store
  # passwords are cached in memory instead of persistent store
  @password_cache = {}
end

Instance Method Details

#api_token(host, user) ⇒ Object



320
321
322
323
324
325
326
327
# File 'lib/hub/github_api.rb', line 320

def api_token host, user
  host = normalize_host host
  @data.fetch_value host, user, :api_token do
    if block_given? then yield
    else prompt "#{host} API token for #{user}"
    end
  end
end

#askpassObject

FIXME: probably not cross-platform



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/hub/github_api.rb', line 357

def askpass
  tty_state = `stty -g`
  system 'stty raw -echo -icanon isig' if $?.success?
  pass = ''
  while char = $stdin.getbyte and not (char == 13 or char == 10)
    if char == 127 or char == 8
      pass[-1,1] = '' unless pass.empty?
    else
      pass << char.chr
    end
  end
  pass
ensure
  system "stty #{tty_state}" unless tty_state.empty?
end

#normalize_host(host) ⇒ Object



306
307
308
309
# File 'lib/hub/github_api.rb', line 306

def normalize_host host
  host = host.downcase
  'api.github.com' == host ? 'github.com' : host
end

#oauth_token(host, user, &block) ⇒ Object



334
335
336
# File 'lib/hub/github_api.rb', line 334

def oauth_token host, user, &block
  @data.fetch_value normalize_host(host), user, :oauth_token, &block
end

#password(host, user) ⇒ Object



329
330
331
332
# File 'lib/hub/github_api.rb', line 329

def password host, user
  host = normalize_host host
  @password_cache["#{user}@#{host}"] ||= prompt_password host, user
end

#prompt(what) ⇒ Object



338
339
340
341
# File 'lib/hub/github_api.rb', line 338

def prompt what
  print "#{what}: "
  $stdin.gets.chomp
end

#prompt_password(host, user) ⇒ Object

special prompt that has hidden input



344
345
346
347
348
349
350
351
352
353
354
# File 'lib/hub/github_api.rb', line 344

def prompt_password host, user
  print "#{host} password for #{user} (never stored): "
  if $stdin.tty?
    password = askpass
    puts ''
    password
  else
    # in testing
    $stdin.gets.chomp
  end
end

#proxy_uri(with_ssl) ⇒ Object



373
374
375
376
377
378
379
# File 'lib/hub/github_api.rb', line 373

def proxy_uri(with_ssl)
  env_name = "HTTP#{with_ssl ? 'S' : ''}_PROXY"
  if proxy = ENV[env_name] || ENV[env_name.downcase] and !proxy.empty?
    proxy = "http://#{proxy}" unless proxy.include? '://'
    URI.parse proxy
  end
end

#username(host) ⇒ Object



311
312
313
314
315
316
317
318
# File 'lib/hub/github_api.rb', line 311

def username host
  host = normalize_host host
  @data.fetch_user host do
    if block_given? then yield
    else prompt "#{host} username"
    end
  end
end