class Hub::GitHubAPI::Configuration

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

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

Constants

NULL

Public Class Methods

new(store) click to toggle source
# File lib/hub/github_api.rb, line 523
def initialize store
  @data = store
  # passwords are cached in memory instead of persistent store
  @password_cache = {}
end

Public Instance Methods

askpass() click to toggle source
# File lib/hub/github_api.rb, line 602
def askpass
  noecho $stdin do |input|
    input.gets.chomp
  end
end
fallback_noecho(io) click to toggle source
# File lib/hub/github_api.rb, line 615
def fallback_noecho io
  tty_state = %xstty -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) click to toggle source
# File lib/hub/github_api.rb, line 631
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) { |io| ... } click to toggle source
# File lib/hub/github_api.rb, line 608
def noecho io
  require 'io/console'
  io.noecho { yield io }
rescue LoadError
  fallback_noecho io
end
normalize_host(host) click to toggle source
# File lib/hub/github_api.rb, line 529
def normalize_host host
  host = host.downcase
  'api.github.com' == host ? 'github.com' : host
end
oauth_token(host, user) { || ... } click to toggle source
# File lib/hub/github_api.rb, line 550
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) click to toggle source
# File lib/hub/github_api.rb, line 544
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) click to toggle source
# File lib/hub/github_api.rb, line 567
def prompt what
  print "#{what}: "
  $stdin.gets.chomp
rescue Interrupt
  puts
  abort
end
prompt_auth_code() click to toggle source
# File lib/hub/github_api.rb, line 591
def prompt_auth_code
  print "two-factor authentication code: "
  $stdin.gets.chomp
rescue Interrupt
  puts
  abort
end
prompt_password(host, user) click to toggle source

special prompt that has hidden input

# File lib/hub/github_api.rb, line 576
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
  puts
  abort
end
protocol(host) click to toggle source
# File lib/hub/github_api.rb, line 557
def protocol host
  host = normalize_host host
  @data.fetch_value(host, nil, :protocol) { 'https' }
end
proxy_uri(with_ssl) click to toggle source
# File lib/hub/github_api.rb, line 640
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) { || ... } click to toggle source
# File lib/hub/github_api.rb, line 534
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) click to toggle source
# File lib/hub/github_api.rb, line 562
def value_to_persist(value = nil)
  @data.persist_next_change!
  value
end