Added update command; refactored duplicated code for selecting a credential

This commit is contained in:
mitchell 2019-06-16 04:02:49 -07:00
parent e51ee55f07
commit d3fa22dbb2
11 changed files with 362 additions and 186 deletions

View file

@ -1,94 +0,0 @@
package commands
import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"time"
"github.com/spf13/cobra"
clitypes "github.com/mitchell/selfpass/cli/types"
"github.com/mitchell/selfpass/credentials/types"
"github.com/mitchell/selfpass/crypto"
)
func MakeCBCtoGCM(repo clitypes.ConfigRepo, initClient CredentialClientInit) *cobra.Command {
cbcToGCM := &cobra.Command{
Use: "cbc-to-gcm",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
masterpass, cfg, err := repo.OpenConfig()
check(err)
key, err := hex.DecodeString(cfg.GetString(clitypes.KeyPrivateKey))
check(err)
keypass, err := crypto.CombinePasswordAndKey([]byte(masterpass), key)
check(err)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
client := initClient(ctx)
mdch, errch := client.GetAllMetadata(ctx, "")
for {
select {
case err := <-errch:
check(err)
case md, ok := <-mdch:
if !ok {
fmt.Println("All done.")
return
}
cred, err := client.Get(ctx, md.ID)
check(err)
passbytes, err := base64.StdEncoding.DecodeString(cred.Password)
check(err)
plainpass, err := crypto.CBCDecrypt(keypass, passbytes)
check(err)
passbytes, err = crypto.GCMEncrypt(keypass, plainpass)
check(err)
cred.Password = base64.StdEncoding.EncodeToString(passbytes)
if cred.OTPSecret != "" {
passbytes, err := base64.StdEncoding.DecodeString(cred.OTPSecret)
check(err)
plainpass, err := crypto.CBCDecrypt(keypass, passbytes)
check(err)
passbytes, err = crypto.GCMEncrypt(keypass, plainpass)
check(err)
cred.OTPSecret = base64.StdEncoding.EncodeToString(passbytes)
}
_, err = client.Update(ctx, cred.ID, types.CredentialInput{
MetadataInput: types.MetadataInput{
Tag: cred.Tag,
SourceHost: cred.SourceHost,
LoginURL: cred.LoginURL,
Primary: cred.Primary,
},
OTPSecret: cred.OTPSecret,
Password: cred.Password,
Email: cred.Email,
Username: cred.Username,
})
check(err)
}
}
},
}
return cbcToGCM
}

View file

@ -4,6 +4,10 @@ import (
"context"
"fmt"
"os"
"sort"
"time"
"gopkg.in/AlecAivazis/survey.v1"
"github.com/mitchell/selfpass/credentials/types"
)
@ -16,3 +20,81 @@ func check(err error) {
os.Exit(1)
}
}
func selectCredential(client types.CredentialClient) types.Credential {
var (
idKey string
source string
prompt survey.Prompt
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
mdch, errch := client.GetAllMetadata(ctx, "")
mds := map[string][]types.Metadata{}
fmt.Println()
receive:
for {
select {
case <-ctx.Done():
check(ctx.Err())
case err := <-errch:
check(err)
case md, ok := <-mdch:
if !ok {
break receive
}
mds[md.SourceHost] = append(mds[md.SourceHost], md)
}
}
sources := []string{}
for source := range mds {
sources = append(sources, source)
}
sort.Strings(sources)
prompt = &survey.Select{
Message: "Source host:",
Options: sources,
PageSize: 20,
VimMode: true,
}
check(survey.AskOne(prompt, &source, nil))
keys := []string{}
keyIDMap := map[string]string{}
for _, md := range mds[source] {
key := md.Primary
if md.Tag != "" {
key += "-" + md.Tag
}
keys = append(keys, key)
keyIDMap[key] = md.ID
}
prompt = &survey.Select{
Message: "Primary user key (and tag):",
Options: keys,
PageSize: 20,
VimMode: true,
}
check(survey.AskOne(prompt, &idKey, nil))
ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
cred, err := client.Get(ctx, keyIDMap[idKey])
check(err)
return cred
}

View file

@ -30,6 +30,13 @@ func MakeCreate(repo clitypes.ConfigRepo, initClient CredentialClientInit) *cobr
password.`,
Run: func(_ *cobra.Command, args []string) {
var (
otp bool
cleancb bool
newpass bool
ci types.CredentialInput
)
masterpass, cfg, err := repo.OpenConfig()
check(err)
@ -61,7 +68,6 @@ password.`,
Prompt: &survey.Input{Message: "Email:"},
},
}
var ci types.CredentialInput
check(survey.Ask(mdqs, &ci.MetadataInput))
check(survey.Ask(cqs, &ci))
@ -71,7 +77,6 @@ password.`,
keypass, err := crypto.CombinePasswordAndKey([]byte(masterpass), []byte(key))
check(err)
var newpass bool
prompt := &survey.Confirm{Message: "Do you want a random password?", Default: true}
check(survey.AskOne(prompt, &newpass, nil))
@ -104,7 +109,6 @@ password.`,
ci.Password = base64.StdEncoding.EncodeToString(cipherpass)
var otp bool
prompt = &survey.Confirm{Message: "Do you have an OTP/MFA secret?", Default: true}
check(survey.AskOne(prompt, &otp, nil))
@ -138,7 +142,6 @@ password.`,
fmt.Println(c)
var cleancb bool
prompt = &survey.Confirm{Message: "Do you want to clear the clipboard?", Default: true}
check(survey.AskOne(prompt, &cleancb, nil))

View file

@ -2,20 +2,29 @@ package commands
import (
"context"
"fmt"
"time"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
)
func MakeDelete(initConfig CredentialClientInit) *cobra.Command {
func MakeDelete(initClient CredentialClientInit) *cobra.Command {
deleteCmd := &cobra.Command{
Use: "delete [id]",
Use: "delete",
Short: "Delete a credential using the given ID",
Long: `Delete a credential using the given ID, permanently. THERE IS NO UNDOING THIS ACTION.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
client := initClient(ctx)
cred := selectCredential(client)
fmt.Println(cred)
var confirmed bool
prompt := &survey.Confirm{Message: "Are you sure you want to permanently delete this credential?"}
check(survey.AskOne(prompt, &confirmed, nil))
@ -24,7 +33,7 @@ func MakeDelete(initConfig CredentialClientInit) *cobra.Command {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*25)
defer cancel()
check(initConfig(ctx).Delete(ctx, args[0]))
check(initClient(ctx).Delete(ctx, cred.ID))
}
},
}

View file

@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"sort"
"time"
"github.com/atotto/clipboard"
@ -14,7 +13,6 @@ import (
"gopkg.in/AlecAivazis/survey.v1"
clitypes "github.com/mitchell/selfpass/cli/types"
"github.com/mitchell/selfpass/credentials/types"
"github.com/mitchell/selfpass/crypto"
)
@ -26,6 +24,12 @@ func MakeGet(repo clitypes.ConfigRepo, initClient CredentialClientInit) *cobra.C
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*15)
defer cancel()
@ -33,76 +37,7 @@ decrypting password.`,
masterpass, cfg, err := repo.OpenConfig()
check(err)
ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
mdch, errch := client.GetAllMetadata(ctx, "")
mds := map[string][]types.Metadata{}
fmt.Println()
receive:
for count := 0; ; count++ {
select {
case <-ctx.Done():
check(ctx.Err())
case err := <-errch:
check(err)
case md, ok := <-mdch:
if !ok {
break receive
}
mds[md.SourceHost] = append(mds[md.SourceHost], md)
}
}
sources := []string{}
for source := range mds {
sources = append(sources, source)
}
sort.Strings(sources)
var prompt survey.Prompt
prompt = &survey.Select{
Message: "Source host:",
Options: sources,
PageSize: 20,
VimMode: true,
}
var source string
check(survey.AskOne(prompt, &source, nil))
keys := []string{}
keyIDMap := map[string]string{}
for _, md := range mds[source] {
key := md.Primary
if md.Tag != "" {
key += "-" + md.Tag
}
keys = append(keys, key)
keyIDMap[key] = md.ID
}
prompt = &survey.Select{
Message: "Primary user key (and tag):",
Options: keys,
PageSize: 20,
VimMode: true,
}
var idKey string
check(survey.AskOne(prompt, &idKey, nil))
ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
cred, err := client.Get(ctx, keyIDMap[idKey])
check(err)
cred := selectCredential(client)
fmt.Println(cred)
@ -116,7 +51,6 @@ decrypting password.`,
passkey, err := crypto.CombinePasswordAndKey([]byte(masterpass), key)
check(err)
var copyPass bool
prompt = &survey.Confirm{Message: "Copy password to clipboard?", Default: true}
check(survey.AskOne(prompt, &copyPass, nil))
@ -151,7 +85,6 @@ decrypting password.`,
}
}
var cleancb bool
prompt = &survey.Confirm{Message: "Do you want to clear the clipboard?", Default: true}
check(survey.AskOne(prompt, &cleancb, nil))

View file

@ -31,7 +31,7 @@ includes almost all the information but the most sensitive.`,
fmt.Println()
receive:
for count := 0; ; count++ {
for {
select {
case <-ctx.Done():
check(ctx.Err())

View file

@ -0,0 +1,194 @@
package commands
import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"os"
"time"
"github.com/atotto/clipboard"
"github.com/pquerna/otp/totp"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
clitypes "github.com/mitchell/selfpass/cli/types"
"github.com/mitchell/selfpass/credentials/types"
"github.com/mitchell/selfpass/crypto"
)
func MakeUpdate(repo clitypes.ConfigRepo, initClient CredentialClientInit) *cobra.Command {
var length uint
var numbers bool
var specials bool
updateCmd := &cobra.Command{
Use: "update",
Short: "Update a credential in Selfpass",
Long: `Update a credential in Selfpass, and save it to the server after encrypting the
password.`,
Run: func(_ *cobra.Command, args []string) {
var (
newpass bool
otp bool
cleancb bool
prompt survey.Prompt
ci types.CredentialInput
)
masterpass, cfg, err := repo.OpenConfig()
check(err)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
client := initClient(ctx)
cred := selectCredential(client)
mdqs := []*survey.Question{
{
Name: "primary",
Prompt: &survey.Input{
Message: "Primary user key:",
Default: cred.Primary,
},
},
{
Name: "sourceHost",
Prompt: &survey.Input{
Message: "Source host:",
Default: cred.SourceHost,
},
},
{
Name: "loginURL",
Prompt: &survey.Input{
Message: "Login url:",
Default: cred.LoginURL,
},
},
{
Name: "tag",
Prompt: &survey.Input{
Message: "Tag:",
Default: cred.Tag,
},
},
}
cqs := []*survey.Question{
{
Name: "username",
Prompt: &survey.Input{
Message: "Username:",
Default: cred.Username,
},
},
{
Name: "email",
Prompt: &survey.Input{
Message: "Email:",
Default: cred.Email,
},
},
}
check(survey.Ask(mdqs, &ci.MetadataInput))
check(survey.Ask(cqs, &ci))
ci.Password = cred.Password
ci.OTPSecret = cred.OTPSecret
key, err := hex.DecodeString(cfg.GetString(clitypes.KeyPrivateKey))
check(err)
keypass, err := crypto.CombinePasswordAndKey([]byte(masterpass), []byte(key))
check(err)
prompt = &survey.Confirm{Message: "Do you want a new password?", Default: true}
check(survey.AskOne(prompt, &newpass, nil))
if newpass {
var randpass bool
prompt = &survey.Confirm{Message: "Do you want a random password?", Default: true}
check(survey.AskOne(prompt, &randpass, nil))
if randpass {
ci.Password = crypto.GeneratePassword(int(length), numbers, specials)
var copypass bool
prompt = &survey.Confirm{Message: "Copy new pass to clipboard?", Default: true}
check(survey.AskOne(prompt, &copypass, nil))
if copypass {
check(clipboard.WriteAll(ci.Password))
}
} else {
prompt := &survey.Password{Message: "Password: "}
check(survey.AskOne(prompt, &ci.Password, nil))
var cpass string
prompt = &survey.Password{Message: "Confirm password: "}
check(survey.AskOne(prompt, &cpass, nil))
if ci.Password != cpass {
fmt.Println("passwords didn't match")
os.Exit(1)
}
}
cipherpass, err := crypto.GCMEncrypt(keypass, []byte(ci.Password))
check(err)
ci.Password = base64.StdEncoding.EncodeToString(cipherpass)
}
prompt = &survey.Confirm{Message: "Do you want to set a new OTP/MFA secret?", Default: true}
check(survey.AskOne(prompt, &otp, nil))
if otp {
var secret string
prompt := &survey.Password{Message: "OTP secret:"}
check(survey.AskOne(prompt, &secret, nil))
ciphersecret, err := crypto.GCMEncrypt(keypass, []byte(secret))
check(err)
ci.OTPSecret = base64.StdEncoding.EncodeToString(ciphersecret)
var copyotp bool
prompt2 := &survey.Confirm{Message: "Copy new OTP to clipboard?", Default: true}
check(survey.AskOne(prompt2, &copyotp, nil))
if copyotp {
otp, err := totp.GenerateCode(secret, time.Now())
check(err)
check(clipboard.WriteAll(otp))
}
}
ctx, cancel = context.WithTimeout(context.Background(), time.Second*25)
defer cancel()
c, err := initClient(ctx).Update(ctx, cred.ID, ci)
check(err)
fmt.Println(c)
prompt = &survey.Confirm{Message: "Do you want to clear the clipboard?", Default: true}
check(survey.AskOne(prompt, &cleancb, nil))
if cleancb {
check(clipboard.WriteAll(" "))
}
},
}
updateCmd.Flags().BoolVarP(&numbers, "numbers", "n", true, "use numbers in the generated password")
updateCmd.Flags().BoolVarP(&specials, "specials", "s", false, "use special characters in the generated password")
updateCmd.Flags().UintVarP(&length, "length", "l", 32, "length of the generated password")
return updateCmd
}