Class: Hub::GitHubAPI::Configuration
- Inherits:
-
Object
- Object
- Hub::GitHubAPI::Configuration
- 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
- #askpass ⇒ Object
- #fallback_noecho(io) ⇒ Object
- #getbyte(io) ⇒ Object
-
#initialize(store) ⇒ Configuration
constructor
A new instance of Configuration.
- #noecho(io) ⇒ Object
- #normalize_host(host) ⇒ Object
- #oauth_token(host, user) ⇒ Object
- #password(host, user) ⇒ Object
- #prompt(what) ⇒ Object
- #prompt_auth_code ⇒ Object
-
#prompt_password(host, user) ⇒ Object
special prompt that has hidden input.
- #protocol(host) ⇒ Object
- #proxy_uri(with_ssl) ⇒ Object
- #username(host) ⇒ Object
- #value_to_persist(value = nil) ⇒ Object
Constructor Details
#initialize(store) ⇒ Configuration
Returns a new instance of Configuration.
502 503 504 505 506 |
# File 'lib/hub/github_api.rb', line 502 def initialize store @data = store # passwords are cached in memory instead of persistent store @password_cache = {} end |
Instance Method Details
#askpass ⇒ Object
581 582 583 584 585 |
# File 'lib/hub/github_api.rb', line 581 def askpass noecho $stdin do |input| input.gets.chomp end end |
#fallback_noecho(io) ⇒ Object
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/hub/github_api.rb', line 594 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
610 611 612 613 614 615 616 617 |
# File 'lib/hub/github_api.rb', line 610 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
587 588 589 590 591 592 |
# File 'lib/hub/github_api.rb', line 587 def noecho io require 'io/console' io.noecho { yield io } rescue LoadError fallback_noecho io end |
#normalize_host(host) ⇒ Object
508 509 510 511 |
# File 'lib/hub/github_api.rb', line 508 def normalize_host host host = host.downcase 'api.github.com' == host ? 'github.com' : host end |
#oauth_token(host, user) ⇒ Object
529 530 531 532 533 534 |
# File 'lib/hub/github_api.rb', line 529 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
523 524 525 526 527 |
# File 'lib/hub/github_api.rb', line 523 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
546 547 548 549 550 551 552 |
# File 'lib/hub/github_api.rb', line 546 def prompt what print "#{what}: " $stdin.gets.chomp rescue Interrupt puts abort end |
#prompt_auth_code ⇒ Object
570 571 572 573 574 575 576 |
# File 'lib/hub/github_api.rb', line 570 def prompt_auth_code print "two-factor authentication code: " $stdin.gets.chomp rescue Interrupt puts abort end |
#prompt_password(host, user) ⇒ Object
special prompt that has hidden input
555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/hub/github_api.rb', line 555 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) ⇒ Object
536 537 538 539 |
# File 'lib/hub/github_api.rb', line 536 def protocol host host = normalize_host host @data.fetch_value(host, nil, :protocol) { 'https' } end |
#proxy_uri(with_ssl) ⇒ Object
619 620 621 622 623 624 625 |
# File 'lib/hub/github_api.rb', line 619 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
513 514 515 516 517 518 519 520 521 |
# File 'lib/hub/github_api.rb', line 513 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
541 542 543 544 |
# File 'lib/hub/github_api.rb', line 541 def value_to_persist(value = nil) @data.persist_next_change! value end |