github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/internal/cli/credentials.go (about) 1 /* 2 * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved. 3 * This software is released under GPL3. 4 * The full license information can be found under: 5 * https://www.gnu.org/licenses/gpl-3.0.en.html 6 * 7 */ 8 9 package cli 10 11 import ( 12 "bufio" 13 "fmt" 14 "os" 15 "strings" 16 17 "github.com/fatih/color" 18 "github.com/sirupsen/logrus" 19 "github.com/vchain-us/vcn/internal/logs" 20 "github.com/vchain-us/vcn/pkg/meta" 21 ) 22 23 func PromptMnemonic() (mnemonic string, err error) { 24 reader := bufio.NewReader(os.Stdin) 25 fmt.Println("Mnemonic code:") 26 if mnemonic, err = reader.ReadString('\n'); err == nil { 27 mnemonic = strings.TrimSpace(mnemonic) 28 } else { 29 mnemonic = "" 30 } 31 return 32 } 33 34 func PromptPassphrase() (passphrase string, err error) { 35 36 color.Set(meta.StyleAffordance()) 37 fmt.Print(` 38 Attention: If you lose this password you will not able to recover it anymore. 39 This password protects your secret against unauthorized access. 40 You will need it every time you want to notarize an asset. 41 `) 42 color.Unset() 43 fmt.Println() 44 45 var keystorePassphrase string 46 var keystorePassphrase2 string 47 48 match := false 49 counter := 0 50 for match == false { 51 52 counter++ 53 54 if counter == 4 { 55 return "", fmt.Errorf("too many failed attempts") 56 } 57 58 keystorePassphrase, _ = readPassword("Notarization password: ") 59 keystorePassphrase2, _ = readPassword("Notarization password (reenter): ") 60 fmt.Println() 61 62 if keystorePassphrase != keystorePassphrase2 { 63 fmt.Println("Your two inputs did not match. Please try again.") 64 } else { 65 match = true 66 } 67 68 } 69 return keystorePassphrase, nil 70 } 71 72 func ProvidePassphrase() (passphrase string, interactive bool, err error) { 73 if _, empty := os.LookupEnv(meta.VcnNotarizationPasswordEmpty); empty { 74 logs.LOG.Trace("Empty notarization password provided (environment)") 75 return "", false, nil 76 } 77 passphrase, ok := os.LookupEnv(meta.VcnNotarizationPassword) 78 if ok { 79 logs.LOG.Trace("Notarization password provided (environment)") 80 return passphrase, false, nil 81 } 82 fmt.Println("Please enter you notarization password to notarize your asset.\nIf you did not set a separate notarization password, use the one used to log in.") 83 passphrase, err = readPassword("Password: ") 84 if err != nil { 85 return "", true, err 86 } 87 logs.LOG.Trace("Notarization password provided (interactive)") 88 return passphrase, true, nil 89 } 90 91 func ProvidePasswordWithMessage(message string) (passphrase string, err error) { 92 passphrase, err = readPassword(message) 93 if err != nil { 94 return "", err 95 } 96 logs.LOG.Trace("Password provided (interactive)") 97 return passphrase, nil 98 } 99 100 func ProvidePlatformUsername() (user string, err error) { 101 user = os.Getenv(meta.VcnUserEnv) 102 if user != "" { 103 logs.LOG.WithFields(logrus.Fields{ 104 "username": user, 105 }).Trace("Platform user provided (environment)") 106 return user, nil 107 } 108 fmt.Print("Email address: ") 109 n, err := fmt.Scanln(&user) 110 if n <= 0 { 111 return "", fmt.Errorf("email address must not be empty") 112 } 113 if err != nil { 114 return "", err 115 } 116 user = strings.TrimSpace(user) 117 logs.LOG.WithFields(logrus.Fields{ 118 "username": user, 119 }).Trace("Platform user provided (interactive)") 120 return user, nil 121 } 122 123 func ProvidePlatformPassword() (password string, err error) { 124 password = os.Getenv(meta.VcnPasswordEnv) 125 if password != "" { 126 logs.LOG.Trace("Platform password provided (environment)") 127 return password, nil 128 } 129 password, err = readPassword("Login password: ") 130 if err != nil { 131 return "", err 132 } 133 if password == "" { 134 return "", fmt.Errorf("password must not be empty") 135 } 136 logs.LOG.Trace("Platform password provided (interactive)") 137 return password, nil 138 } 139 140 func ProvideOtp() (otp string, err error) { 141 if _, empty := os.LookupEnv(meta.VcnOtpEmpty); empty { 142 logs.LOG.Trace("Empty otp provided (environment)") 143 return "", nil 144 } 145 otp = os.Getenv(meta.VcnOtp) 146 if otp != "" { 147 logs.LOG.Trace("Otp provided (environment)") 148 return otp, nil 149 } 150 fmt.Print("One time password (press enter if null): ") 151 w := bufio.NewReader(os.Stdin) 152 otp, err = w.ReadString('\n') 153 if err != nil { 154 return "", err 155 } 156 otp = strings.ReplaceAll(strings.TrimSpace(otp), " ", "") 157 if otp != "" { 158 logs.LOG.WithFields(logrus.Fields{ 159 "otp": otp, 160 }).Trace("Otp provided (interactive)") 161 } 162 return otp, nil 163 }