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.
538 539 540 541 542 |
# File 'lib/hub/github_api.rb', line 538 def initialize store @data = store # passwords are cached in memory instead of persistent store @password_cache = {} end |
Instance Method Details
#askpass ⇒ Object
617 618 619 620 621 |
# File 'lib/hub/github_api.rb', line 617 def askpass noecho $stdin do |input| input.gets.chomp end end |
#fallback_noecho(io) ⇒ Object
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'lib/hub/github_api.rb', line 630 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
646 647 648 649 650 651 652 653 |
# File 'lib/hub/github_api.rb', line 646 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
623 624 625 626 627 628 |
# File 'lib/hub/github_api.rb', line 623 def noecho io require 'io/console' io.noecho { yield io } rescue LoadError fallback_noecho io end |
#normalize_host(host) ⇒ Object
544 545 546 547 |
# File 'lib/hub/github_api.rb', line 544 def normalize_host host host = host.downcase 'api.github.com' == host ? 'github.com' : host end |
#oauth_token(host, user) ⇒ Object
565 566 567 568 569 570 |
# File 'lib/hub/github_api.rb', line 565 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
559 560 561 562 563 |
# File 'lib/hub/github_api.rb', line 559 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
582 583 584 585 586 587 588 |
# File 'lib/hub/github_api.rb', line 582 def prompt what print "#{what}: " $stdin.gets.chomp rescue Interrupt puts abort end |
#prompt_auth_code ⇒ Object
606 607 608 609 610 611 612 |
# File 'lib/hub/github_api.rb', line 606 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
591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/hub/github_api.rb', line 591 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
572 573 574 575 |
# File 'lib/hub/github_api.rb', line 572 def protocol host host = normalize_host host @data.fetch_value(host, nil, :protocol) { 'https' } end |
#proxy_uri(with_ssl) ⇒ Object
655 656 657 658 659 660 661 |
# File 'lib/hub/github_api.rb', line 655 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
549 550 551 552 553 554 555 556 557 |
# File 'lib/hub/github_api.rb', line 549 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
577 578 579 580 |
# File 'lib/hub/github_api.rb', line 577 def value_to_persist(value = nil) @data.persist_next_change! value end |