summaryrefslogtreecommitdiff
path: root/examples/password_input.lua
blob: 29940628cec8fe74e29dea8906d9df1e3fcbe0c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
local sys = require "system"

print [[

This example shows how to disable the "echo" of characters read to the console,
useful for reading secrets from the user.

]]

--- Function to read from stdin without echoing the input (for secrets etc).
-- It will (in a platform agnostic way) disable echo on the terminal, read the
-- input, and then re-enable echo.
-- @param ... Arguments to pass to `io.stdin:read()`
-- @return the results of `io.stdin:read(...)`
local function read_secret(...)
  local w_oldflags, p_oldflags

  if sys.isatty(io.stdin) then
    -- backup settings, configure echo flags
    w_oldflags = sys.getconsoleflags(io.stdin)
    p_oldflags = sys.tcgetattr(io.stdin)
    -- set echo off to not show password on screen
    assert(sys.setconsoleflags(io.stdin, w_oldflags - sys.CIF_ECHO_INPUT))
    assert(sys.tcsetattr(io.stdin, sys.TCSANOW, { lflag = p_oldflags.lflag - sys.L_ECHO }))
  end

  local secret, err = io.stdin:read(...)

  -- restore settings
  if sys.isatty(io.stdin) then
    io.stdout:write("\n")  -- Add newline after reading the password
    sys.setconsoleflags(io.stdin, w_oldflags)
    sys.tcsetattr(io.stdin, sys.TCSANOW, p_oldflags)
  end

  return secret, err
end



-- Get username
io.write("Username: ")
local username = io.stdin:read("*l")

-- Get the secret
io.write("Password: ")
local password = read_secret("*l")

-- Get domainname
io.write("Domain  : ")
local domain = io.stdin:read("*l")


-- Print the results
print("")
print("Here's what we got:")
print("  username: " .. username)
print("  password: " .. password)
print("  domain  : " .. domain)