2019-05-28 01:16:50 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/atotto/clipboard"
|
|
|
|
"github.com/pquerna/otp/totp"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/AlecAivazis/survey.v1"
|
|
|
|
|
2019-07-11 06:05:59 +00:00
|
|
|
"github.com/mitchell/selfpass/sp/crypto"
|
|
|
|
clitypes "github.com/mitchell/selfpass/sp/types"
|
2019-05-28 01:16:50 +00:00
|
|
|
)
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
func makeGet(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
2019-08-01 03:17:38 +00:00
|
|
|
flags := credentialFlagSet{}.withHostFlag()
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
getCmd := &cobra.Command{
|
2019-06-01 20:32:11 +00:00
|
|
|
Use: "get",
|
2019-05-28 01:16:50 +00:00
|
|
|
Short: "Get a credential info and copy password to clipboard",
|
|
|
|
Long: `Get a credential's info and copy password to clipboard, from Selfpass server, after
|
|
|
|
decrypting password.`,
|
|
|
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-06-16 11:02:49 +00:00
|
|
|
var (
|
|
|
|
copyPass bool
|
|
|
|
cleancb bool
|
|
|
|
prompt survey.Prompt
|
|
|
|
)
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
2019-05-28 01:16:50 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-06-01 20:32:11 +00:00
|
|
|
client := initClient(ctx)
|
2019-06-02 08:47:19 +00:00
|
|
|
masterpass, cfg, err := repo.OpenConfig()
|
|
|
|
check(err)
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
cred := selectCredential(client, flags.sourceHost)
|
2019-05-28 01:16:50 +00:00
|
|
|
|
|
|
|
fmt.Println(cred)
|
|
|
|
|
|
|
|
check(clipboard.WriteAll(string(cred.Primary)))
|
|
|
|
|
|
|
|
fmt.Println("Wrote primary user key to clipboard.")
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
key := cfg.GetString(clitypes.KeyPrivateKey)
|
|
|
|
passkey := crypto.GeneratePBKDF2Key([]byte(masterpass), []byte(key))
|
2019-05-28 01:16:50 +00:00
|
|
|
|
2019-06-01 20:32:11 +00:00
|
|
|
prompt = &survey.Confirm{Message: "Copy password to clipboard?", Default: true}
|
2019-05-28 01:16:50 +00:00
|
|
|
check(survey.AskOne(prompt, ©Pass, nil))
|
|
|
|
|
|
|
|
if copyPass {
|
|
|
|
passbytes, err := base64.StdEncoding.DecodeString(cred.Password)
|
|
|
|
check(err)
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
plainpass, err := crypto.CBCDecrypt(passkey, passbytes)
|
2019-05-28 01:16:50 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
plainsecret, err := crypto.CBCDecrypt(passkey, secretbytes)
|
2019-05-28 01:16:50 +00:00
|
|
|
|
|
|
|
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(" "))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
flags.register(getCmd)
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
return getCmd
|
|
|
|
}
|