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.



335
336
337
338
339
# File 'lib/hub/github_api.rb', line 335

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



362
363
364
365
366
367
368
369
# File 'lib/hub/github_api.rb', line 362

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



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/hub/github_api.rb', line 402

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

#normalize_host(host) ⇒ Object



341
342
343
344
# File 'lib/hub/github_api.rb', line 341

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

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



377
378
379
# File 'lib/hub/github_api.rb', line 377

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

#password(host, user) ⇒ Object



371
372
373
374
375
# File 'lib/hub/github_api.rb', line 371

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



381
382
383
384
# File 'lib/hub/github_api.rb', line 381

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

#prompt_password(host, user) ⇒ Object

special prompt that has hidden input



387
388
389
390
391
392
393
394
395
396
397
# File 'lib/hub/github_api.rb', line 387

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



418
419
420
421
422
423
424
# File 'lib/hub/github_api.rb', line 418

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

#update_username(host, old_username, new_username) ⇒ Object



356
357
358
359
360
# File 'lib/hub/github_api.rb', line 356

def update_username host, old_username, new_username
  entry = @data.entry_for_user(normalize_host(host), old_username)
  entry['user'] = new_username
  @data.save
end

#username(host) ⇒ Object



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

def username host
  return ENV['GITHUB_USER'] unless ENV['GITHUB_USER'].to_s.empty?
  host = normalize_host host
  @data.fetch_user host do
    if block_given? then yield
    else prompt "#{host} username"
    end
  end
end