mirror of
https://github.com/mitchell/selfpass.git
synced 2025-12-14 05:17:22 +00:00
Implemented encryption functionality of spc and password generation; refactors on spc and server
This commit is contained in:
parent
c289eecd54
commit
cd24f6e848
26 changed files with 1151 additions and 1522 deletions
21
credentials/cmds/cmds.go
Normal file
21
credentials/cmds/cmds.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package cmds
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/mitchell/selfpass/credentials/types"
|
||||
)
|
||||
|
||||
type CredentialClientInit func(ctx context.Context) (c types.CredentialClient)
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
const KeyConnConfig = "connection"
|
||||
const KeyPrivateKey = "private_key"
|
||||
146
credentials/cmds/create.go
Normal file
146
credentials/cmds/create.go
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
package cmds
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/atotto/clipboard"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/AlecAivazis/survey.v1"
|
||||
|
||||
"github.com/mitchell/selfpass/credentials/types"
|
||||
"github.com/mitchell/selfpass/crypto"
|
||||
)
|
||||
|
||||
func MakeCreateCmd(masterpass string, cfg *viper.Viper, initClient CredentialClientInit) *cobra.Command {
|
||||
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) {
|
||||
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:"},
|
||||
},
|
||||
}
|
||||
var ci types.CredentialInput
|
||||
|
||||
check(survey.Ask(mdqs, &ci.MetadataInput))
|
||||
check(survey.Ask(cqs, &ci))
|
||||
|
||||
var newpass bool
|
||||
prompt := &survey.Confirm{Message: "Do you want a random password?", Default: true}
|
||||
check(survey.AskOne(prompt, &newpass, nil))
|
||||
|
||||
if newpass {
|
||||
ci.Password = generatePassword(16, true, true)
|
||||
|
||||
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
|
||||
prompt = &survey.Password{Message: "Confirm assword: "}
|
||||
check(survey.AskOne(prompt, &cpass, nil))
|
||||
|
||||
if ci.Password != cpass {
|
||||
fmt.Println("passwords didn't match'")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
key, err := hex.DecodeString(cfg.GetString(KeyPrivateKey))
|
||||
check(err)
|
||||
|
||||
keypass, err := crypto.CombinePasswordAndKey([]byte(masterpass), []byte(key))
|
||||
check(err)
|
||||
|
||||
cipherpass, err := crypto.CBCEncrypt(keypass, []byte(ci.Password))
|
||||
check(err)
|
||||
|
||||
ci.Password = base64.StdEncoding.EncodeToString(cipherpass)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
c, err := initClient(ctx).Create(ctx, ci)
|
||||
check(err)
|
||||
|
||||
mdjson, err := json.MarshalIndent(c.Metadata, "", " ")
|
||||
check(err)
|
||||
fmt.Println(string(mdjson))
|
||||
},
|
||||
}
|
||||
|
||||
return createCmd
|
||||
}
|
||||
|
||||
const alphas = "abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV"
|
||||
const alphanumerics = "abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV1234567890"
|
||||
const alphasAndSpecials = "abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV1234567890!@#$%^&*()"
|
||||
const alphanumericsAndSpecials = "abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV1234567890!@#$%^&*()"
|
||||
|
||||
func generatePassword(length int, numbers, specials bool) string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
pass := make([]byte, length)
|
||||
|
||||
switch {
|
||||
case numbers && specials:
|
||||
for idx := 0; idx < length; idx++ {
|
||||
pass[idx] = alphanumericsAndSpecials[rand.Int63()%int64(len(alphanumericsAndSpecials))]
|
||||
}
|
||||
case numbers:
|
||||
for idx := 0; idx < length; idx++ {
|
||||
pass[idx] = alphanumerics[rand.Int63()%int64(len(alphanumerics))]
|
||||
}
|
||||
case specials:
|
||||
for idx := 0; idx < length; idx++ {
|
||||
pass[idx] = alphasAndSpecials[rand.Int63()%int64(len(alphasAndSpecials))]
|
||||
}
|
||||
default:
|
||||
for idx := 0; idx < length; idx++ {
|
||||
pass[idx] = alphas[rand.Int63()%int64(len(alphas))]
|
||||
}
|
||||
}
|
||||
|
||||
return string(pass)
|
||||
}
|
||||
63
credentials/cmds/get.go
Normal file
63
credentials/cmds/get.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package cmds
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/atotto/clipboard"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/mitchell/selfpass/crypto"
|
||||
)
|
||||
|
||||
func MakeGetCmd(masterpass string, cfg *viper.Viper, initClient CredentialClientInit) *cobra.Command {
|
||||
getCmd := &cobra.Command{
|
||||
Use: "get [id]",
|
||||
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.`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||
defer cancel()
|
||||
|
||||
cbcontents, err := clipboard.ReadAll()
|
||||
check(err)
|
||||
|
||||
restore := func(cbcontents string) {
|
||||
time.Sleep(time.Second * 5)
|
||||
clipboard.WriteAll(cbcontents)
|
||||
}
|
||||
|
||||
cred, err := initClient(ctx).Get(ctx, args[0])
|
||||
check(err)
|
||||
|
||||
key, err := hex.DecodeString(cfg.GetString(KeyPrivateKey))
|
||||
check(err)
|
||||
|
||||
passkey, err := crypto.CombinePasswordAndKey([]byte(masterpass), key)
|
||||
check(err)
|
||||
|
||||
passbytes, err := base64.StdEncoding.DecodeString(cred.Password)
|
||||
check(err)
|
||||
|
||||
plainpass, err := crypto.CBCDecrypt(passkey, passbytes)
|
||||
|
||||
check(clipboard.WriteAll(string(plainpass)))
|
||||
go restore(cbcontents)
|
||||
|
||||
cjson, err := json.MarshalIndent(cred, "", " ")
|
||||
check(err)
|
||||
fmt.Println(string(cjson))
|
||||
fmt.Println("Wrote password to clipboard.")
|
||||
},
|
||||
}
|
||||
|
||||
return getCmd
|
||||
}
|
||||
58
credentials/cmds/list.go
Normal file
58
credentials/cmds/list.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package cmds
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func MakeListCmd(initClient CredentialClientInit) *cobra.Command {
|
||||
var sourceHost string
|
||||
|
||||
listCmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List the metadata for all credentials",
|
||||
Long: `List the metadata for all credentials, with the option to filter by source host. Metadata
|
||||
includes almost all the information but the most sensitive.`,
|
||||
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||
defer cancel()
|
||||
|
||||
mdch, errch := initClient(ctx).GetAllMetadata(ctx, sourceHost)
|
||||
|
||||
receive:
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
check(fmt.Errorf("context timeout"))
|
||||
|
||||
case err := <-errch:
|
||||
check(err)
|
||||
|
||||
case md, ok := <-mdch:
|
||||
if !ok {
|
||||
break receive
|
||||
}
|
||||
|
||||
mdjson, err := json.MarshalIndent(md, "", " ")
|
||||
check(err)
|
||||
fmt.Println(string(mdjson))
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
listCmd.Flags().StringVarP(
|
||||
&sourceHost,
|
||||
"source-host",
|
||||
"s",
|
||||
"",
|
||||
"specify which source host to filter the results by",
|
||||
)
|
||||
|
||||
return listCmd
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue