Class: HTAuth::CLI::Digest

Inherits:
Object
  • Object
show all
Defined in:
lib/htauth/cli/digest.rb

Overview

Internal: Implemenation of the commandline htdigest-ruby

Constant Summary collapse

MAX_PASSWD_LENGTH =
255

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDigest

Returns a new instance of Digest.



16
17
18
19
20
# File 'lib/htauth/cli/digest.rb', line 16

def initialize
  @digest_file = nil
  @option_parser = nil
  @options = nil
end

Instance Attribute Details

#digest_fileObject

Returns the value of attribute digest_file.



14
15
16
# File 'lib/htauth/cli/digest.rb', line 14

def digest_file
  @digest_file
end

Instance Method Details

#option_parserObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/htauth/cli/digest.rb', line 36

def option_parser
  @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
  @option_parser
end

#optionsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/htauth/cli/digest.rb', line 22

def options
  if @options.nil?
    @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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/htauth/cli/digest.rb', line 68

def parse_options(argv)
  option_parser.parse!(argv)
  show_version if options.show_version
  show_help if options.show_help || (argv.size < 3)

  options.passwdfile = argv.shift
  options.realm      = argv.shift
  options.username   = argv.shift
rescue ::OptionParser::ParseError => e
  warn "ERROR: #{option_parser.program_name} - #{e}"
  warn "Try `#{option_parser.program_name} --help` for more information"
  exit 1
end

#run(argv, _env = ENV) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/htauth/cli/digest.rb', line 82

def run(argv, _env = ENV)
  begin
    parse_options(argv)
    digest_file = DigestFile.new(options.passwdfile, options.file_mode)

    if options.delete_entry
      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 => e
    msg = "Could not open password file #{options.passwdfile} "
    warn "#{msg}: #{e.message}"
    warn e.backtrace.join("\n")
    exit 1
  rescue HTAuth::Error => e
    warn e.message
    exit 1
  rescue SignalException => e
    $stderr.puts
    warn "Interrupted #{e}"
    exit 1
  end
  exit 0
end

#show_helpObject



58
59
60
61
# File 'lib/htauth/cli/digest.rb', line 58

def show_help
  $stdout.puts option_parser
  exit 1
end

#show_versionObject



63
64
65
66
# File 'lib/htauth/cli/digest.rb', line 63

def show_version
  $stdout.puts "#{option_parser.program_name}: version #{HTAuth::VERSION}"
  exit 1
end