Use terminal.ReadPasword to read password

This commit is contained in:
Jarno Rankinen 2024-07-01 10:14:29 +03:00
parent 54e545a1b5
commit 216b77e0ef
1 changed files with 14 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"golang.org/x/crypto/ssh/terminal"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -217,18 +218,24 @@ func (cfg *NtfyConfig) Setup() {
// existing values are reset // existing values are reset
cfg.User = "" cfg.User = ""
cfg.Password = "" cfg.Password = ""
fmt.Println("Configuring HTTP basic authentication for push endpoint.")
fmt.Println("Previously set username and password have been cleared.")
fmt.Println("Leave values blank to disable basic authentication.")
scanner = bufio.NewScanner(os.Stdin) scanner = bufio.NewScanner(os.Stdin)
fmt.Printf("Input username for push endpoint: ") fmt.Printf("Username: ")
scanner.Scan() scanner.Scan()
if len(scanner.Text()) > 0 { if len(scanner.Text()) > 0 {
cfg.User = scanner.Text() cfg.User = scanner.Text()
} }
scanner = bufio.NewScanner(os.Stdin) fmt.Printf("Password: ")
fmt.Printf("Input password for push endpoint: ") pwBytes, err := terminal.ReadPassword(0)
scanner.Scan() if err != nil {
if len(scanner.Text()) > 0 { fmt.Printf("Error reading password: %v\n", err)
return
}
if len(pwBytes) > 0 {
// Store the password in base64 for a little obfuscation // Store the password in base64 for a little obfuscation
cfg.Password = base64.StdEncoding.EncodeToString(scanner.Bytes()) cfg.Password = base64.StdEncoding.EncodeToString(pwBytes)
} }
// Save bridge password // Save bridge password
if len(cfg.BridgePw) == 0 { if len(cfg.BridgePw) == 0 {
@ -241,7 +248,7 @@ func (cfg *NtfyConfig) Setup() {
fmt.Println("Bridge password is set") fmt.Println("Bridge password is set")
} }
// Save configuration // Save configuration
err := cfg.Save() err = cfg.Save()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)