Module: Itsi::Server::Passfile
- Defined in:
- lib/itsi/passfile.rb
Class Method Summary collapse
- .add(filename, algorithm) ⇒ Object
- .echo(_, algorithm) ⇒ Object
- .list(filename) ⇒ Object
- .load(filename) ⇒ Object
- .remove(filename) ⇒ Object
- .save(creds, filename) ⇒ Object
Class Method Details
.add(filename, algorithm) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/itsi/passfile.rb', line 56 def add(filename, algorithm) return unless (creds = load(filename)) print "Enter username: " username = $stdin.gets.chomp print "Enter password: " password = $stdin.noecho(&:gets).chomp puts print "Confirm password: " password_confirm = $stdin.noecho(&:gets).chomp puts if password != password_confirm puts "Error: Passwords do not match!" exit(1) end creds[username] = Itsi.create_password_hash(password, algorithm) save(creds, filename) puts "User '#{username}' added." end |
.echo(_, algorithm) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/itsi/passfile.rb', line 35 def echo(_, algorithm) print "Enter username: " username = $stdin.gets.chomp print "Enter password: " password = $stdin.noecho(&:gets).chomp puts print "Confirm password: " password_confirm = $stdin.noecho(&:gets).chomp puts if password != password_confirm puts "Error: Passwords do not match!" exit(1) end puts "#{username}:#{Itsi.create_password_hash(password, algorithm)}" end |
.list(filename) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/itsi/passfile.rb', line 98 def list(filename) puts "Current credentials in '#{filename}':" return unless (creds = load(filename)) creds.each do |u, p| puts "#{u}:#{p}" end end |
.load(filename) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/itsi/passfile.rb', line 8 def load(filename) if filename.nil? || filename.strip.empty? puts "Error: a valid filename is required." return nil end creds = {} if File.exist?(filename) File.foreach(filename) do |line| line.chomp! next if line.empty? user, pass = line.split(":", 2) creds[user] = pass end end creds end |
.remove(filename) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/itsi/passfile.rb', line 83 def remove(filename) return unless (creds = load(filename)) print "Enter username to remove: " username = $stdin.gets.chomp if creds.key?(username) creds.delete(username) save(creds, filename) puts "User '#{username}' removed." else puts "Warning: User '#{username}' not found." end end |
.save(creds, filename) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/itsi/passfile.rb', line 27 def save(creds, filename) File.open(filename, "w", 0o600) do |f| creds.each do |u, p| f.puts "#{u}:#{p}" end end end |