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.



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

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



342
343
344
345
346
347
348
349
# File 'lib/hub/github_api.rb', line 342

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



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/hub/github_api.rb', line 380

def askpass
  tty_state = `stty -g`
  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



327
328
329
330
# File 'lib/hub/github_api.rb', line 327

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

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



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

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

#password(host, user) ⇒ Object



351
352
353
354
355
# File 'lib/hub/github_api.rb', line 351

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



361
362
363
364
# File 'lib/hub/github_api.rb', line 361

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

#prompt_password(host, user) ⇒ Object

special prompt that has hidden input



367
368
369
370
371
372
373
374
375
376
377
# File 'lib/hub/github_api.rb', line 367

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



396
397
398
399
400
401
402
# File 'lib/hub/github_api.rb', line 396

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



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

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