Sha256: 880f43758e1cade74443a55ed725707791045a4545c4fa577fde173008c52e00

Contents?: true

Size: 1.74 KB

Versions: 26

Compression:

Stored size: 1.74 KB

Contents

require 'highline'
require 'net/netrc'

require 'fig/user_input_error'

module Fig; end
module Fig::Protocol; end

# Login information acquisition via .netrc.
module Fig::Protocol::NetRCEnabled
  private

  NetRCEntry = Struct.new :username, :password

  def initialize_netrc()
    @netrc_entries_by_host = {}

    return
  end

  def get_authentication_for(host, prompt_if_missing)
    if @netrc_entries_by_host.include? host
      return @netrc_entries_by_host[host]
    end

    entry = nil
    if prompt_if_missing
      entry = get_authentication_from_environment
    end

    if ! entry
      begin
        login_data = Net::Netrc.locate host
        if login_data
          entry = NetRCEntry.new login_data.login, login_data.password
        elsif prompt_if_missing
          entry = get_authentication_from_user(host)
        end
      rescue SecurityError => error
        raise Fig::UserInputError.new error.message
      end
    end

    @netrc_entries_by_host[host] = entry

    return entry
  end

  def get_authentication_from_environment()
    username = ENV['FIG_USERNAME']
    password = ENV['FIG_PASSWORD']

    if username.nil? && password.nil?
      return nil
    end

    if ! username.nil? && ! password.nil?
      return NetRCEntry.new username, password
    end

    if password.nil?
      raise Fig::UserInputError.new \
        'FIG_USERNAME is set but FIG_PASSWORD is not.'
    end

    raise Fig::UserInputError.new 'FIG_PASSWORD is set but FIG_USERNAME is not.'
  end

  def get_authentication_from_user(host)
    username = HighLine.new.ask("Username for #{host}: ") { |q| q.echo = true }
    password = HighLine.new.ask("Password for #{username}@#{host}: ") {
      |q| q.echo = false
    }

    return NetRCEntry.new username, password
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
fig-1.22.0 lib/fig/protocol/netrc_enabled.rb
fig-1.21.1.beta.2 lib/fig/protocol/netrc_enabled.rb
fig-1.21.1.beta.1 lib/fig/protocol/netrc_enabled.rb
fig-1.21.0 lib/fig/protocol/netrc_enabled.rb
fig-1.20.1.beta.1 lib/fig/protocol/netrc_enabled.rb
fig-1.20.0 lib/fig/protocol/netrc_enabled.rb
fig-1.19.0 lib/fig/protocol/netrc_enabled.rb
fig-1.18.0 lib/fig/protocol/netrc_enabled.rb
fig-1.17.0 lib/fig/protocol/netrc_enabled.rb
fig-1.16.1.beta.1 lib/fig/protocol/netrc_enabled.rb
fig-1.16.0 lib/fig/protocol/netrc_enabled.rb
fig-1.15.1.beta.2 lib/fig/protocol/netrc_enabled.rb
fig-1.15.1.beta.1 lib/fig/protocol/netrc_enabled.rb
fig-1.15.0 lib/fig/protocol/netrc_enabled.rb
fig-1.14.0 lib/fig/protocol/netrc_enabled.rb
fig-1.13.0 lib/fig/protocol/netrc_enabled.rb
fig-1.12.0 lib/fig/protocol/netrc_enabled.rb
fig-1.11.0 lib/fig/protocol/netrc_enabled.rb
fig-1.10.0 lib/fig/protocol/netrc_enabled.rb
fig-1.9.0 lib/fig/protocol/netrc_enabled.rb