Module: Password
Overview
gist.github.com/2040373 Ask for a password from CLI, if it exists.
Instance Method Summary collapse
- #ask(prompt = "Enter password: ") ⇒ Object
- #ask_for_password_on_jruby(prompt = "Enter password: ") ⇒ Object
-
#ask_for_password_on_unix(prompt = "Enter password: ") ⇒ Object
— Lé code.
- #ask_for_password_on_windows(prompt = "Enter password: ") ⇒ Object
- #beos? ⇒ Boolean
-
#bsd? ⇒ Boolean
patches welcome: rh, oel, sl, slc, cent, fed, ubt, deb, mint, sles, suse, arch, parabola, nixos, ad ⧝.
- #freebsd? ⇒ Boolean
-
#ios? ⇒ Boolean
e.g..
- #jruby? ⇒ Boolean
-
#linux? ⇒ Boolean
Can you spot the implication?.
-
#mac? ⇒ Boolean
NOTE: 99.999% of mac detection code has a subtle flaw.
- #netbsd? ⇒ Boolean
- #openbsd? ⇒ Boolean
- #os2? ⇒ Boolean
- #solaris? ⇒ Boolean
- #unix? ⇒ Boolean
-
#windows? ⇒ Boolean
— Platform inspectors.
Instance Method Details
#ask(prompt = "Enter password: ") ⇒ Object
125 126 127 128 129 130 |
# File 'lib/password/password.rb', line 125 def ask(prompt = "Enter password: ") %w|windows unix jruby|.each do |platform| eval "return ask_for_password_on_#{platform}(prompt) if #{platform}?" end raise "Could not read password on unknown Ruby platform: #{RUBY_DESCRIPTION}" end |
#ask_for_password_on_jruby(prompt = "Enter password: ") ⇒ Object
79 80 81 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 121 122 123 |
# File 'lib/password/password.rb', line 79 def ask_for_password_on_jruby(prompt = "Enter password: ") raise 'Could not ask for password because there is no interactive terminal (tty)' unless $stdin.tty? password='' require 'java' include_class 'java.lang.System' include_class 'java.io.Console' unless prompt.nil? $stderr.print prompt $stderr.flush end console = System.console() return unless console != java.null loop do break unless (read_passwd = console.readPassword()) != java.null passwd_chr = java.lang.String.new(read_passwd).to_string case passwd_chr when "\e" # ESC password.length.times do |i| password[i] = ' ' end return nil when "", "\r", "\n" # Enter (for certain), ..., ... break when "\177", "\b" # Ctrl-H, Bkspace if password.length > 0 password[-1] = ' ' password.slice!(-1, 1) end when "\004" # Ctrl-D password.length.times do |i| password[i] = ' ' end password = '' else password << passwd_chr end end $stderr.puts passwd end |
#ask_for_password_on_unix(prompt = "Enter password: ") ⇒ Object
— Lé code
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/password/password.rb', line 37 def ask_for_password_on_unix(prompt = "Enter password: ") raise 'Could not ask for password because there is no interactive terminal (tty)' unless $stdin.tty? unless prompt.nil? $stderr.print prompt $stderr.flush end raise 'Could not disable echo to ask for password security' unless system 'stty -echo -icanon' password = $stdin.gets password.chomp! if password password ensure raise 'Could not re-enable echo while securely asking for password' unless system 'stty echo icanon' end |
#ask_for_password_on_windows(prompt = "Enter password: ") ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/password/password.rb', line 51 def ask_for_password_on_windows(prompt = "Enter password: ") raise 'Could not ask for password because there is no interactive terminal (tty)' unless $stdin.tty? require 'Win32API' char = nil password = '' unless prompt.nil? $stderr.print prompt $stderr.flush end while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do break if char == 10 || char == 13 # return or newline if char == 127 || char == 8 # backspace and delete password[-1] = ' ' password.slice!(-1, 1) else password << char.chr end end char = ' ' $stderr.puts password end |
#beos? ⇒ Boolean
11 |
# File 'lib/password/password.rb', line 11 def beos? ; RUBY_PLATFORM =~ /beos/i end |
#bsd? ⇒ Boolean
patches welcome: rh, oel, sl, slc, cent, fed, ubt, deb, mint, sles, suse, arch, parabola, nixos, ad ⧝
27 |
# File 'lib/password/password.rb', line 27 def bsd? ; freebsd? || netbsd? || openbsd? end |
#freebsd? ⇒ Boolean
28 |
# File 'lib/password/password.rb', line 28 def freebsd? ; RUBY_PLATFORM =~ /freebsd/i end |
#ios? ⇒ Boolean
e.g.. ‘x86_64-darwin11.3.0’ MRI 1.9.3 Lion
'x86_64-apple-darwin11.3.0' Rubinius Lion
'i686-darwin11.3.0' RubyEnterprise Lion
'universal-darwin10.0' MacRuby Lion
21 |
# File 'lib/password/password.rb', line 21 def ios? ; RUBY_PLATFORM =~ /arm-darwin/i end |
#jruby? ⇒ Boolean
9 |
# File 'lib/password/password.rb', line 9 def jruby? ; RUBY_PLATFORM =~ /java/i end |
#linux? ⇒ Boolean
Can you spot the implication?
25 |
# File 'lib/password/password.rb', line 25 def linux? ; RUBY_PLATFORM =~ /linux/i end |
#mac? ⇒ Boolean
NOTE: 99.999% of mac detection code has a subtle flaw.
i.e.: RUBY_PLATFORM =~ /darwin|apple|mac/i
16 |
# File 'lib/password/password.rb', line 16 def mac? ; RUBY_PLATFORM =~ /.*(sal|86).*-darwin1/i end |
#netbsd? ⇒ Boolean
29 |
# File 'lib/password/password.rb', line 29 def netbsd? ; RUBY_PLATFORM =~ /netbsd/i end |
#openbsd? ⇒ Boolean
30 |
# File 'lib/password/password.rb', line 30 def openbsd? ; RUBY_PLATFORM =~ /openbsd/i end |
#os2? ⇒ Boolean
10 |
# File 'lib/password/password.rb', line 10 def os2? ; RUBY_PLATFORM =~ /os2/i end |
#solaris? ⇒ Boolean
31 |
# File 'lib/password/password.rb', line 31 def solaris? ; RUBY_PLATFORM =~ /solaris/i end |
#unix? ⇒ Boolean
12 |
# File 'lib/password/password.rb', line 12 def unix? ; !jruby? && !windows? && !os2? && !beos? end |
#windows? ⇒ Boolean
— Platform inspectors
8 |
# File 'lib/password/password.rb', line 8 def windows? ; RUBY_PLATFORM =~ /win(32|dows|ce)|djgpp|(ms|cyg|bcc)win|mingw32/i end |