mirror of https://github.com/mitchell/selfpass.git
Fix setting empty OTP secret; minor refactors
This commit is contained in:
parent
2096d6ada8
commit
e136b40b70
|
@ -6,7 +6,6 @@ import (
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"go.etcd.io/bbolt"
|
"go.etcd.io/bbolt"
|
||||||
|
|
||||||
|
@ -38,24 +37,19 @@ func (db BoltDB) GetAllMetadata(ctx context.Context, sourceHost string, errch ch
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
c := bkt.hostPrimaryIndex.Cursor()
|
c := bkt.hostPrimaryIndex.Cursor()
|
||||||
|
|
||||||
if sourceHost == "" {
|
if sourceHost == "" {
|
||||||
for key, value := c.First(); key != nil; key, value = c.Next() {
|
for key, value := c.First(); key != nil; key, value = c.Next() {
|
||||||
wg.Add(1)
|
unmarshalAndSendCred(value, mdch, errch)
|
||||||
unmarshalAndSendCred(value, mdch, errch, &wg)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
hostBytes := []byte(sourceHost)
|
hostBytes := []byte(sourceHost)
|
||||||
for key, value := c.Seek(hostBytes); bytes.HasPrefix(key, hostBytes); key, value = c.Next() {
|
for key, value := c.Seek(hostBytes); bytes.HasPrefix(key, hostBytes); key, value = c.Next() {
|
||||||
wg.Add(1)
|
unmarshalAndSendCred(value, mdch, errch)
|
||||||
unmarshalAndSendCred(value, mdch, errch, &wg)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -67,9 +61,7 @@ func (db BoltDB) GetAllMetadata(ctx context.Context, sourceHost string, errch ch
|
||||||
return mdch
|
return mdch
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshalAndSendCred(value []byte, mdch chan<- types.Metadata, errch chan<- error, wg *sync.WaitGroup) {
|
func unmarshalAndSendCred(value []byte, mdch chan<- types.Metadata, errch chan<- error) {
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
var cred types.Credential
|
var cred types.Credential
|
||||||
|
|
||||||
err := gobUnmarshal(value, &cred)
|
err := gobUnmarshal(value, &cred)
|
||||||
|
|
|
@ -111,20 +111,23 @@ password.`,
|
||||||
check(survey.AskOne(prompt, &otp, nil))
|
check(survey.AskOne(prompt, &otp, nil))
|
||||||
|
|
||||||
if otp {
|
if otp {
|
||||||
|
var copyOTP bool
|
||||||
var secret string
|
var secret string
|
||||||
|
|
||||||
prompt = &survey.Password{Message: "OTP secret:"}
|
prompt = &survey.Password{Message: "OTP secret:"}
|
||||||
check(survey.AskOne(prompt, &secret, nil))
|
check(survey.AskOne(prompt, &secret, nil))
|
||||||
|
|
||||||
|
if secret != "" {
|
||||||
ciphersecret, err := crypto.CBCEncrypt(keypass, []byte(secret))
|
ciphersecret, err := crypto.CBCEncrypt(keypass, []byte(secret))
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
ci.OTPSecret = base64.StdEncoding.EncodeToString(ciphersecret)
|
ci.OTPSecret = base64.StdEncoding.EncodeToString(ciphersecret)
|
||||||
|
|
||||||
var copyotp bool
|
|
||||||
prompt = &survey.Confirm{Message: "Copy new OTP to clipboard?", Default: true}
|
prompt = &survey.Confirm{Message: "Copy new OTP to clipboard?", Default: true}
|
||||||
check(survey.AskOne(prompt, ©otp, nil))
|
check(survey.AskOne(prompt, ©OTP, nil))
|
||||||
|
}
|
||||||
|
|
||||||
if copyotp {
|
if copyOTP {
|
||||||
otp, err := totp.GenerateCode(secret, time.Now())
|
otp, err := totp.GenerateCode(secret, time.Now())
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
|
@ -132,9 +135,9 @@ password.`,
|
||||||
fmt.Println("Wrote one time password to clipboard.")
|
fmt.Println("Wrote one time password to clipboard.")
|
||||||
|
|
||||||
prompt = &survey.Confirm{Message: "Anotha one?", Default: true}
|
prompt = &survey.Confirm{Message: "Anotha one?", Default: true}
|
||||||
check(survey.AskOne(prompt, ©otp, nil))
|
check(survey.AskOne(prompt, ©OTP, nil))
|
||||||
|
|
||||||
if copyotp {
|
if copyOTP {
|
||||||
otp, err := totp.GenerateCode(secret, time.Now().Add(time.Second*30))
|
otp, err := totp.GenerateCode(secret, time.Now().Add(time.Second*30))
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
|
|
|
@ -145,20 +145,25 @@ password.`,
|
||||||
check(survey.AskOne(prompt, &otp, nil))
|
check(survey.AskOne(prompt, &otp, nil))
|
||||||
|
|
||||||
if otp {
|
if otp {
|
||||||
|
var copyOTP bool
|
||||||
var secret string
|
var secret string
|
||||||
|
|
||||||
|
ci.OTPSecret = ""
|
||||||
|
|
||||||
prompt = &survey.Password{Message: "OTP secret:"}
|
prompt = &survey.Password{Message: "OTP secret:"}
|
||||||
check(survey.AskOne(prompt, &secret, nil))
|
check(survey.AskOne(prompt, &secret, nil))
|
||||||
|
|
||||||
|
if secret != "" {
|
||||||
ciphersecret, err := crypto.CBCEncrypt(keypass, []byte(secret))
|
ciphersecret, err := crypto.CBCEncrypt(keypass, []byte(secret))
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
ci.OTPSecret = base64.StdEncoding.EncodeToString(ciphersecret)
|
ci.OTPSecret = base64.StdEncoding.EncodeToString(ciphersecret)
|
||||||
|
|
||||||
var copyotp bool
|
|
||||||
prompt = &survey.Confirm{Message: "Copy new OTP to clipboard?", Default: true}
|
prompt = &survey.Confirm{Message: "Copy new OTP to clipboard?", Default: true}
|
||||||
check(survey.AskOne(prompt, ©otp, nil))
|
check(survey.AskOne(prompt, ©OTP, nil))
|
||||||
|
}
|
||||||
|
|
||||||
if copyotp {
|
if copyOTP {
|
||||||
otp, err := totp.GenerateCode(secret, time.Now())
|
otp, err := totp.GenerateCode(secret, time.Now())
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
|
@ -166,9 +171,9 @@ password.`,
|
||||||
fmt.Println("Wrote one time password to clipboard.")
|
fmt.Println("Wrote one time password to clipboard.")
|
||||||
|
|
||||||
prompt = &survey.Confirm{Message: "Anotha one?", Default: true}
|
prompt = &survey.Confirm{Message: "Anotha one?", Default: true}
|
||||||
check(survey.AskOne(prompt, ©otp, nil))
|
check(survey.AskOne(prompt, ©OTP, nil))
|
||||||
|
|
||||||
if copyotp {
|
if copyOTP {
|
||||||
otp, err := totp.GenerateCode(secret, time.Now().Add(time.Second*30))
|
otp, err := totp.GenerateCode(secret, time.Now().Add(time.Second*30))
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
|
|
|
@ -12,5 +12,5 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func GeneratePBKDF2Key(password, salt []byte) []byte {
|
func GeneratePBKDF2Key(password, salt []byte) []byte {
|
||||||
return pbkdf2.Key([]byte(password), []byte(salt), PBKDF2Rounds, KeyLength, sha256.New)
|
return pbkdf2.Key(password, salt, PBKDF2Rounds, KeyLength, sha256.New)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ require (
|
||||||
github.com/atotto/clipboard v0.1.2
|
github.com/atotto/clipboard v0.1.2
|
||||||
github.com/c-bata/go-prompt v0.2.3
|
github.com/c-bata/go-prompt v0.2.3
|
||||||
github.com/google/uuid v1.1.1
|
github.com/google/uuid v1.1.1
|
||||||
|
github.com/mattn/go-tty v0.0.0-20190424173100-523744f04859 // indirect
|
||||||
github.com/mitchell/selfpass/services v0.0.0-00010101000000-000000000000
|
github.com/mitchell/selfpass/services v0.0.0-00010101000000-000000000000
|
||||||
github.com/mitchellh/go-homedir v1.1.0
|
github.com/mitchellh/go-homedir v1.1.0
|
||||||
github.com/ncw/rclone v1.48.0
|
github.com/ncw/rclone v1.48.0
|
||||||
|
|
|
@ -135,6 +135,8 @@ github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI
|
||||||
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||||
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
|
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
|
||||||
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||||
|
github.com/mattn/go-tty v0.0.0-20190424173100-523744f04859 h1:smQbSzmT3EHl4EUwtFwFGmGIpiYgIiiPeVv1uguIQEE=
|
||||||
|
github.com/mattn/go-tty v0.0.0-20190424173100-523744f04859/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||||
github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed h1:3dQJqqDouawQgl3gBE1PNHKFkJYGEuFb1DbSlaxdosE=
|
github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed h1:3dQJqqDouawQgl3gBE1PNHKFkJYGEuFb1DbSlaxdosE=
|
||||||
github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed/go.mod h1:dSsfyI2zABAdhcbvkXqgxOxrCsbYeHCPgrZkku60dSg=
|
github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed/go.mod h1:dSsfyI2zABAdhcbvkXqgxOxrCsbYeHCPgrZkku60dSg=
|
||||||
|
|
Loading…
Reference in New Issue