diff --git a/sp/commands/create.go b/sp/commands/create.go index a477903..f904665 100644 --- a/sp/commands/create.go +++ b/sp/commands/create.go @@ -72,7 +72,7 @@ password.`, key := cfg.GetString(clitypes.KeyPrivateKey) keypass := crypto.GeneratePBKDF2Key([]byte(masterpass), []byte(key)) - prompt = &survey.Confirm{Message: "Generate a random password?", Default: true} + prompt = &survey.Confirm{Message: "Do you want a random password?", Default: true} check(survey.AskOne(prompt, &newpass, nil)) if newpass { @@ -152,7 +152,7 @@ password.`, fmt.Println(c) - prompt = &survey.Confirm{Message: "Clear the clipboard?", Default: true} + prompt = &survey.Confirm{Message: "Do you want to clear the clipboard?", Default: true} check(survey.AskOne(prompt, &cleancb, nil)) if cleancb { diff --git a/sp/commands/get.go b/sp/commands/get.go index 70a4bbb..c8fb5e4 100644 --- a/sp/commands/get.go +++ b/sp/commands/get.go @@ -9,10 +9,8 @@ import ( "github.com/atotto/clipboard" "github.com/pquerna/otp/totp" "github.com/spf13/cobra" - "github.com/spf13/viper" "gopkg.in/AlecAivazis/survey.v1" - "github.com/mitchell/selfpass/services/credentials/types" "github.com/mitchell/selfpass/sp/crypto" clitypes "github.com/mitchell/selfpass/sp/types" ) @@ -27,6 +25,12 @@ func makeGet(repo clitypes.ConfigRepo, initClient credentialsClientInit) *cobra. decrypting password.`, Run: func(cmd *cobra.Command, args []string) { + var ( + copyPass bool + cleancb bool + prompt survey.Prompt + ) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*60) defer cancel() @@ -36,9 +40,54 @@ decrypting password.`, cred := selectCredential(client, flags.sourceHost) - for getAnother := getCredential(cred, masterpass, cfg); getAnother; { - cred := selectCredential(client, flags.sourceHost) - getAnother = getCredential(cred, masterpass, cfg) + fmt.Println(cred) + + check(clipboard.WriteAll(string(cred.Primary))) + + fmt.Println("Wrote primary user key to clipboard.") + + key := cfg.GetString(clitypes.KeyPrivateKey) + passkey := crypto.GeneratePBKDF2Key([]byte(masterpass), []byte(key)) + + prompt = &survey.Confirm{Message: "Copy password to clipboard?", Default: true} + check(survey.AskOne(prompt, ©Pass, nil)) + + if copyPass { + passbytes, err := base64.StdEncoding.DecodeString(cred.Password) + check(err) + + plainpass, err := crypto.CBCDecrypt(passkey, passbytes) + + check(clipboard.WriteAll(string(plainpass))) + + fmt.Println("Wrote password to clipboard.") + } + + if cred.OTPSecret != "" { + var newOTP bool + prompt = &survey.Confirm{Message: "Generate one time password and copy to clipboard?", Default: true} + check(survey.AskOne(prompt, &newOTP, nil)) + + if newOTP { + secretbytes, err := base64.StdEncoding.DecodeString(cred.OTPSecret) + check(err) + + plainsecret, err := crypto.CBCDecrypt(passkey, secretbytes) + + otp, err := totp.GenerateCode(string(plainsecret), time.Now()) + check(err) + + check(clipboard.WriteAll(otp)) + + fmt.Println("Wrote one time password to clipboard.") + } + } + + prompt = &survey.Confirm{Message: "Do you want to clear the clipboard?", Default: true} + check(survey.AskOne(prompt, &cleancb, nil)) + + if cleancb { + check(clipboard.WriteAll(" ")) } }, } @@ -47,67 +96,3 @@ decrypting password.`, return getCmd } - -func getCredential(cred types.Credential, masterpass string, cfg *viper.Viper) (again bool) { - var ( - copyPass bool - cleancb bool - prompt survey.Prompt - ) - - fmt.Println(cred) - - check(clipboard.WriteAll(string(cred.Primary))) - - fmt.Println("Wrote primary user key to clipboard.") - - key := cfg.GetString(clitypes.KeyPrivateKey) - passkey := crypto.GeneratePBKDF2Key([]byte(masterpass), []byte(key)) - - prompt = &survey.Confirm{Message: "Copy password to clipboard?", Default: true} - check(survey.AskOne(prompt, ©Pass, nil)) - - if copyPass { - passbytes, err := base64.StdEncoding.DecodeString(cred.Password) - check(err) - - plainpass, err := crypto.CBCDecrypt(passkey, passbytes) - - check(clipboard.WriteAll(string(plainpass))) - - fmt.Println("Wrote password to clipboard.") - } - - if cred.OTPSecret != "" { - var newOTP bool - prompt = &survey.Confirm{Message: "Generate one time password and copy to clipboard?", Default: true} - check(survey.AskOne(prompt, &newOTP, nil)) - - if newOTP { - secretbytes, err := base64.StdEncoding.DecodeString(cred.OTPSecret) - check(err) - - plainsecret, err := crypto.CBCDecrypt(passkey, secretbytes) - - otp, err := totp.GenerateCode(string(plainsecret), time.Now()) - check(err) - - check(clipboard.WriteAll(otp)) - - fmt.Println("Wrote one time password to clipboard.") - } - } - - prompt = &survey.Confirm{Message: "Clear the clipboard?", Default: true} - check(survey.AskOne(prompt, &cleancb, nil)) - - if cleancb { - check(clipboard.WriteAll(" ")) - fmt.Println("Clipboard cleared.") - } - - prompt = &survey.Confirm{Message: "Get another credential?", Default: true} - check(survey.AskOne(prompt, &again, nil)) - - return again -} diff --git a/sp/commands/update.go b/sp/commands/update.go index bd793d1..a212d06 100644 --- a/sp/commands/update.go +++ b/sp/commands/update.go @@ -105,7 +105,7 @@ password.`, if newpass { var randpass bool - prompt = &survey.Confirm{Message: "Generate a random password?", Default: true} + prompt = &survey.Confirm{Message: "Do you want a random password?", Default: true} check(survey.AskOne(prompt, &randpass, nil)) if randpass { @@ -181,7 +181,7 @@ password.`, fmt.Println(c) - prompt = &survey.Confirm{Message: "Clear the clipboard?", Default: true} + prompt = &survey.Confirm{Message: "Do you want to clear the clipboard?", Default: true} check(survey.AskOne(prompt, &cleancb, nil)) if cleancb {