style: generate the otp code in entry struct

This commit is contained in:
Adrien Waksberg 2019-07-20 08:39:41 +02:00
parent 0e80f704bc
commit 9361d471ef
2 changed files with 9 additions and 3 deletions

View file

@ -24,7 +24,6 @@ import(
"time" "time"
"github.com/atotto/clipboard" "github.com/atotto/clipboard"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
"github.com/pquerna/otp/totp"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
@ -268,8 +267,7 @@ func (c *Cli) copyEntry() {
case "p": case "p":
clipboard.WriteAll(entry.Password) clipboard.WriteAll(entry.Password)
case "o": case "o":
code, _ := totp.GenerateCode(entry.OTP, time.Now()) clipboard.WriteAll(entry.OTPCode())
clipboard.WriteAll(code)
case "q": case "q":
os.Exit(0) os.Exit(0)
default: default:

View file

@ -18,6 +18,8 @@ import(
"fmt" "fmt"
"time" "time"
"net/url" "net/url"
"github.com/pquerna/otp/totp"
) )
// Entry struct have the password informations // Entry struct have the password informations
@ -54,3 +56,9 @@ func (e *Entry) Verify() error {
func (e *Entry) GenerateID() { func (e *Entry) GenerateID() {
e.ID = fmt.Sprintf("%d", time.Now().UnixNano()) e.ID = fmt.Sprintf("%d", time.Now().UnixNano())
} }
// OTPCode generate an OTP Code
func (e *Entry) OTPCode() string {
code, _ := totp.GenerateCode(e.OTP, time.Now())
return code
}