2019-05-28 01:16:50 +00:00
|
|
|
package commands
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/atotto/clipboard"
|
2019-05-28 01:16:50 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
2019-05-22 15:22:40 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/AlecAivazis/survey.v1"
|
|
|
|
|
2019-07-11 02:33:22 +00:00
|
|
|
"github.com/mitchell/selfpass/services/credentials/types"
|
2019-07-11 06:05:59 +00:00
|
|
|
"github.com/mitchell/selfpass/sp/crypto"
|
|
|
|
clitypes "github.com/mitchell/selfpass/sp/types"
|
2019-05-22 15:22:40 +00:00
|
|
|
)
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
func makeCreate(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
2019-08-01 03:17:38 +00:00
|
|
|
flags := credentialFlagSet{}.withPasswordFlags()
|
2019-05-28 01:16:50 +00:00
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
createCmd := &cobra.Command{
|
|
|
|
Use: "create",
|
|
|
|
Short: "Create a credential in Selfpass",
|
|
|
|
Long: `Create a credential in Selfpass, and save it to the server after encrypting the
|
|
|
|
password.`,
|
|
|
|
|
|
|
|
Run: func(_ *cobra.Command, args []string) {
|
2019-06-16 11:02:49 +00:00
|
|
|
var (
|
|
|
|
otp bool
|
|
|
|
cleancb bool
|
|
|
|
newpass bool
|
|
|
|
ci types.CredentialInput
|
|
|
|
)
|
|
|
|
|
2019-06-02 08:47:19 +00:00
|
|
|
masterpass, cfg, err := repo.OpenConfig()
|
|
|
|
check(err)
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
mdqs := []*survey.Question{
|
|
|
|
{
|
|
|
|
Name: "primary",
|
|
|
|
Prompt: &survey.Input{Message: "Primary user key:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "sourceHost",
|
|
|
|
Prompt: &survey.Input{Message: "Source host:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "loginURL",
|
|
|
|
Prompt: &survey.Input{Message: "Login url:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "tag",
|
|
|
|
Prompt: &survey.Input{Message: "Tag:"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cqs := []*survey.Question{
|
|
|
|
{
|
|
|
|
Name: "username",
|
|
|
|
Prompt: &survey.Input{Message: "Username:"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "email",
|
|
|
|
Prompt: &survey.Input{Message: "Email:"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
check(survey.Ask(mdqs, &ci.MetadataInput))
|
|
|
|
check(survey.Ask(cqs, &ci))
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
key := cfg.GetString(clitypes.KeyPrivateKey)
|
|
|
|
keypass := crypto.GeneratePBKDF2Key([]byte(masterpass), []byte(key))
|
2019-05-28 01:16:50 +00:00
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
prompt := &survey.Confirm{Message: "Do you want a random password?", Default: true}
|
|
|
|
check(survey.AskOne(prompt, &newpass, nil))
|
|
|
|
|
|
|
|
if newpass {
|
2019-08-01 03:17:38 +00:00
|
|
|
ci.Password = crypto.GeneratePassword(int(flags.length), !flags.noNumbers, !flags.noSpecials)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
var copypass bool
|
|
|
|
prompt = &survey.Confirm{Message: "Copy new pass to clipboard?", Default: true}
|
|
|
|
check(survey.AskOne(prompt, ©pass, nil))
|
|
|
|
|
|
|
|
if copypass {
|
|
|
|
check(clipboard.WriteAll(ci.Password))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prompt := &survey.Password{Message: "Password: "}
|
|
|
|
check(survey.AskOne(prompt, &ci.Password, nil))
|
|
|
|
|
|
|
|
var cpass string
|
2019-05-28 01:16:50 +00:00
|
|
|
prompt = &survey.Password{Message: "Confirm password: "}
|
2019-05-22 15:22:40 +00:00
|
|
|
check(survey.AskOne(prompt, &cpass, nil))
|
|
|
|
|
|
|
|
if ci.Password != cpass {
|
2019-05-30 06:32:56 +00:00
|
|
|
fmt.Println("passwords didn't match")
|
2019-05-22 15:22:40 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
cipherpass, err := crypto.CBCEncrypt(keypass, []byte(ci.Password))
|
2019-05-22 15:22:40 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
ci.Password = base64.StdEncoding.EncodeToString(cipherpass)
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
prompt = &survey.Confirm{Message: "Do you have an 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))
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
ciphersecret, err := crypto.CBCEncrypt(keypass, []byte(secret))
|
2019-05-28 01:16:50 +00:00
|
|
|
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, ©otp, nil))
|
|
|
|
|
|
|
|
if copyotp {
|
|
|
|
otp, err := totp.GenerateCode(secret, time.Now())
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
check(clipboard.WriteAll(otp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
2019-05-22 15:22:40 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
client := initClient(ctx)
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Second*25)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := client.Create(ctx, ci)
|
2019-05-22 15:22:40 +00:00
|
|
|
check(err)
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
fmt.Println(c)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
prompt = &survey.Confirm{Message: "Do you want to clear the clipboard?", Default: true}
|
|
|
|
check(survey.AskOne(prompt, &cleancb, nil))
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
if cleancb {
|
|
|
|
check(clipboard.WriteAll(" "))
|
|
|
|
}
|
|
|
|
},
|
2019-05-22 15:22:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
flags.register(createCmd)
|
2019-05-28 01:16:50 +00:00
|
|
|
|
|
|
|
return createCmd
|
2019-05-22 15:22:40 +00:00
|
|
|
}
|