Class | HTAuth::Digest |
In: |
lib/htauth/digest.rb
|
Parent: | Object |
MAX_PASSWD_LENGTH | = | 255 |
digest_file | [RW] |
# File lib/htauth/digest.rb, line 35 35: def option_parser 36: if not @option_parser then 37: @option_parser = OptionParser.new do |op| 38: op.banner = "Usage: #{op.program_name} [options] passwordfile realm username" 39: op.on("-c", "--create", "Create a new digest password file; this overwrites an existing file.") do |c| 40: options.file_mode = DigestFile::CREATE 41: end 42: 43: op.on("-D", "--delete", "Delete the specified user.") do |d| 44: options.delete_entry = d 45: end 46: 47: op.on("-h", "--help", "Display this help.") do |h| 48: options.show_help = h 49: end 50: 51: op.on("-v", "--version", "Show version info.") do |v| 52: options.show_version = v 53: end 54: end 55: end 56: @option_parser 57: end
# File lib/htauth/digest.rb, line 21 21: def options 22: if @options.nil? then 23: @options = ::OpenStruct.new 24: @options.show_version = false 25: @options.show_help = false 26: @options.file_mode = DigestFile::ALTER 27: @options.passwdfile = nil 28: @options.realm = nil 29: @options.username = nil 30: @options.delete_entry = false 31: end 32: @options 33: end
# File lib/htauth/digest.rb, line 69 69: def parse_options(argv) 70: begin 71: option_parser.parse!(argv) 72: show_version if options.show_version 73: show_help if options.show_help or argv.size < 3 74: 75: options.passwdfile = argv.shift 76: options.realm = argv.shift 77: options.username = argv.shift 78: rescue ::OptionParser::ParseError => pe 79: $stderr.puts "ERROR: #{option_parser.program_name} - #{pe}" 80: $stderr.puts "Try `#{option_parser.program_name} --help` for more information" 81: exit 1 82: end 83: end
# File lib/htauth/digest.rb, line 85 85: def run(argv) 86: begin 87: parse_options(argv) 88: digest_file = DigestFile.new(options.passwdfile, options.file_mode) 89: 90: if options.delete_entry then 91: digest_file.delete(options.username, options.realm) 92: else 93: # initialize here so that if $stdin is overwritten it gest picked up 94: hl = ::HighLine.new 95: 96: action = digest_file.has_entry?(options.username, options.realm) ? "Changing" : "Adding" 97: 98: $stdout.puts "#{action} password for #{options.username} in realm #{options.realm}." 99: 100: pw_in = hl.ask(" New password: ") { |q| q.echo = '*' } 101: raise PasswordError, "password '#{pw_in}' too long" if pw_in.length >= MAX_PASSWD_LENGTH 102: 103: pw_validate = hl.ask("Re-type new password: ") { |q| q.echo = '*' } 104: raise PasswordError, "They don't match, sorry." unless pw_in == pw_validate 105: 106: digest_file.add_or_update(options.username, options.realm, pw_in) 107: end 108: 109: digest_file.save! 110: 111: rescue HTAuth::FileAccessError => fae 112: msg = "Could not open password file #{options.passwdfile} " 113: $stderr.puts "#{msg}: #{fae.message}" 114: $stderr.puts fae.backtrace.join("\n") 115: exit 1 116: rescue HTAuth::PasswordError => pe 117: $stderr.puts "#{pe.message}" 118: exit 1 119: rescue HTAuth::DigestFileError => fe 120: $stderr.puts "#{fe.message}" 121: exit 1 122: rescue SignalException => se 123: $stderr.puts 124: $stderr.puts "Interrupted" 125: exit 1 126: end 127: exit 0 128: end
# File lib/htauth/digest.rb, line 59 59: def show_help 60: $stdout.puts option_parser 61: exit 1 62: end