class HTAuth::CLI::Digest

Implemenation of the commandline htdigest-ruby

Constants

MAX_PASSWD_LENGTH

Attributes

digest_file[RW]

Public Class Methods

new() click to toggle source
# File lib/htauth/cli/digest.rb, line 18
def initialize
  @digest_file = nil
  @option_parser = nil
  @options = nil
end

Public Instance Methods

option_parser() click to toggle source
# File lib/htauth/cli/digest.rb, line 38
def option_parser
  if not @option_parser then
    @option_parser = OptionParser.new(nil, 14) do |op|
      op.banner = "Usage: #{op.program_name} [options] passwordfile realm username"
      op.on("-c", "--create", "Create a new digest password file; this overwrites an existing file.") do |c|
        options.file_mode = DigestFile::CREATE
      end

      op.on("-D", "--delete", "Delete the specified user.") do |d|
        options.delete_entry = d
      end

      op.on("-h", "--help", "Display this help.") do |h|
        options.show_help = h
      end

      op.on("-v", "--version", "Show version info.") do |v|
        options.show_version = v
      end
    end
  end
  @option_parser
end
options() click to toggle source
# File lib/htauth/cli/digest.rb, line 24
def options
  if @options.nil? then
    @options                = ::OpenStruct.new
    @options.show_version   = false
    @options.show_help      = false
    @options.file_mode      = DigestFile::ALTER
    @options.passwdfile     = nil
    @options.realm          = nil
    @options.username       = nil
    @options.delete_entry   = false
  end
  @options
end
parse_options(argv) click to toggle source
# File lib/htauth/cli/digest.rb, line 72
def parse_options(argv)
  begin
    option_parser.parse!(argv)
    show_version if options.show_version
    show_help if options.show_help or argv.size < 3

    options.passwdfile = argv.shift
    options.realm      = argv.shift
    options.username   = argv.shift
  rescue ::OptionParser::ParseError => pe
    $stderr.puts "ERROR: #{option_parser.program_name} - #{pe}"
    $stderr.puts "Try `#{option_parser.program_name} --help` for more information"
    exit 1
  end
end
run(argv, env = ENV) click to toggle source
# File lib/htauth/cli/digest.rb, line 88
def run(argv, env = ENV)
  begin
    parse_options(argv)
    digest_file = DigestFile.new(options.passwdfile, options.file_mode)

    if options.delete_entry then
      digest_file.delete(options.username, options.realm)
    else
      console = Console.new

      action = digest_file.has_entry?(options.username, options.realm) ? "Changing" : "Adding"

      console.say "#{action} password for #{options.username} in realm #{options.realm}."

      pw_in       = console.ask("        New password: ")
      raise PasswordError, "password '#{pw_in}' too long" if pw_in.length >= MAX_PASSWD_LENGTH

      pw_validate = console.ask("Re-type new password: ")
      raise PasswordError, "They don't match, sorry." unless pw_in == pw_validate

      digest_file.add_or_update(options.username, options.realm, pw_in)
    end

    digest_file.save!

  rescue HTAuth::FileAccessError => fae
    msg = "Could not open password file #{options.passwdfile} "
    $stderr.puts "#{msg}: #{fae.message}"
    $stderr.puts fae.backtrace.join("\n")
    exit 1
  rescue HTAuth::Error => pe
    $stderr.puts "#{pe.message}"
    exit 1
  rescue SignalException => se
    $stderr.puts
    $stderr.puts "Interrupted #{se}"
    exit 1
  end
  exit 0
end
show_help() click to toggle source
# File lib/htauth/cli/digest.rb, line 62
def show_help
  $stdout.puts option_parser
  exit 1
end
show_version() click to toggle source
# File lib/htauth/cli/digest.rb, line 67
def show_version
  $stdout.puts "#{option_parser.program_name}: version #{HTAuth::VERSION}"
  exit 1
end