Class: HTAuth::Digest
- Inherits:
-
Object
- Object
- HTAuth::Digest
- Defined in:
- lib/htauth/digest.rb
Constant Summary collapse
- MAX_PASSWD_LENGTH =
255
Instance Attribute Summary collapse
-
#digest_file ⇒ Object
Returns the value of attribute digest_file.
Instance Method Summary collapse
-
#initialize ⇒ Digest
constructor
A new instance of Digest.
- #option_parser ⇒ Object
- #options ⇒ Object
- #parse_options(argv) ⇒ Object
- #run(argv, env = ENV) ⇒ Object
- #show_help ⇒ Object
- #show_version ⇒ Object
Constructor Details
#initialize ⇒ Digest
Returns a new instance of Digest.
17 18 19 20 21 |
# File 'lib/htauth/digest.rb', line 17 def initialize @digest_file = nil @option_parser = nil @options = nil end |
Instance Attribute Details
#digest_file ⇒ Object
Returns the value of attribute digest_file.
15 16 17 |
# File 'lib/htauth/digest.rb', line 15 def digest_file @digest_file end |
Instance Method Details
#option_parser ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/htauth/digest.rb', line 37 def option_parser if not @option_parser then @option_parser = OptionParser.new do |op| op. = "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| .file_mode = DigestFile::CREATE end op.on("-D", "--delete", "Delete the specified user.") do |d| .delete_entry = d end op.on("-h", "--help", "Display this help.") do |h| .show_help = h end op.on("-v", "--version", "Show version info.") do |v| .show_version = v end end end @option_parser end |
#options ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/htauth/digest.rb', line 23 def 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) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/htauth/digest.rb', line 71 def (argv) begin option_parser.parse!(argv) show_version if .show_version show_help if .show_help or argv.size < 3 .passwdfile = argv.shift .realm = argv.shift .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) ⇒ Object
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 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/htauth/digest.rb', line 87 def run(argv, env = ENV) begin (argv) digest_file = DigestFile.new(.passwdfile, .file_mode) if .delete_entry then digest_file.delete(.username, .realm) else # initialize here so that if $stdin is overwritten it gets picked up hl = ::HighLine.new action = digest_file.has_entry?(.username, .realm) ? "Changing" : "Adding" $stdout.puts "#{action} password for #{.username} in realm #{.realm}." pw_in = hl.ask(" New password: ") { |q| q.echo = '*' } raise PasswordError, "password '#{pw_in}' too long" if pw_in.length >= MAX_PASSWD_LENGTH pw_validate = hl.ask("Re-type new password: ") { |q| q.echo = '*' } raise PasswordError, "They don't match, sorry." unless pw_in == pw_validate digest_file.add_or_update(.username, .realm, pw_in) end digest_file.save! rescue HTAuth::FileAccessError => fae msg = "Could not open password file #{.passwdfile} " $stderr.puts "#{msg}: #{fae.}" $stderr.puts fae.backtrace.join("\n") exit 1 rescue HTAuth::PasswordError => pe $stderr.puts "#{pe.}" exit 1 rescue HTAuth::DigestFileError => fe $stderr.puts "#{fe.}" exit 1 rescue SignalException => se $stderr.puts $stderr.puts "Interrupted #{se}" exit 1 end exit 0 end |
#show_help ⇒ Object
61 62 63 64 |
# File 'lib/htauth/digest.rb', line 61 def show_help $stdout.puts option_parser exit 1 end |