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.

Constant Summary collapse

NULL =
defined?(File::NULL) ? File::NULL :
File.exist?('/dev/null') ? '/dev/null' : 'NUL'

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Configuration

Returns a new instance of Configuration.



441
442
443
444
445
# File 'lib/hub/github_api.rb', line 441

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

Instance Method Details

#askpassObject



512
513
514
515
516
# File 'lib/hub/github_api.rb', line 512

def askpass
  noecho $stdin do |input|
    input.gets.chomp
  end
end

#fallback_noecho(io) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/hub/github_api.rb', line 525

def fallback_noecho io
  tty_state = `stty -g 2>#{NULL}`
  system 'stty raw -echo -icanon isig' if $?.success?
  pass = ''
  while char = getbyte(io) and !(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

#getbyte(io) ⇒ Object



541
542
543
544
545
546
547
548
# File 'lib/hub/github_api.rb', line 541

def getbyte(io)
  if io.respond_to?(:getbyte)
    io.getbyte
  else
    # In Ruby <= 1.8.6, getc behaved the same
    io.getc
  end
end

#noecho(io) ⇒ Object



518
519
520
521
522
523
# File 'lib/hub/github_api.rb', line 518

def noecho io
  require 'io/console'
  io.noecho { yield io }
rescue LoadError
  fallback_noecho io
end

#normalize_host(host) ⇒ Object



447
448
449
450
# File 'lib/hub/github_api.rb', line 447

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

#oauth_token(host, user) ⇒ Object



468
469
470
471
472
473
# File 'lib/hub/github_api.rb', line 468

def oauth_token host, user
  host = normalize_host(host)
  @data.fetch_value(host, user, :oauth_token) do
    value_to_persist(yield)
  end
end

#password(host, user) ⇒ Object



462
463
464
465
466
# File 'lib/hub/github_api.rb', line 462

def password host, user
  return ENV['GITHUB_PASSWORD'] unless ENV['GITHUB_PASSWORD'].to_s.empty?
  host = normalize_host host
  @password_cache["#{user}@#{host}"] ||= prompt_password host, user
end

#prompt(what) ⇒ Object



480
481
482
483
484
485
# File 'lib/hub/github_api.rb', line 480

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

#prompt_auth_codeObject



502
503
504
505
506
507
# File 'lib/hub/github_api.rb', line 502

def prompt_auth_code
  print "two-factor authentication code: "
  $stdin.gets.chomp
rescue Interrupt
  abort
end

#prompt_password(host, user) ⇒ Object

special prompt that has hidden input



488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/hub/github_api.rb', line 488

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
rescue Interrupt
  abort
end

#proxy_uri(with_ssl) ⇒ Object



550
551
552
553
554
555
556
# File 'lib/hub/github_api.rb', line 550

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



452
453
454
455
456
457
458
459
460
# File 'lib/hub/github_api.rb', line 452

def username host
  return ENV['GITHUB_USER'] unless ENV['GITHUB_USER'].to_s.empty?
  host = normalize_host host
  @data.fetch_value(host, nil, :user) do
    if block_given? then yield
    else prompt "#{host} username"
    end
  end
end

#value_to_persist(value = nil) ⇒ Object



475
476
477
478
# File 'lib/hub/github_api.rb', line 475

def value_to_persist(value = nil)
  @data.persist_next_change!
  value
end