Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/mdp/qrterminal/v3"
"github.com/pquerna/otp/totp"
"github.com/sethvargo/go-password/password"
)

Expand All @@ -29,9 +28,6 @@ var GeneratePassword = password.Generate
// GenerateQrCode creates a QR Code and writes it out to io.Writer.
var GenerateQrCode = qrterminal.Generate

// GenerateTotpCode creates a TOTP token using the current time.
var GenerateTotpCode = totp.GenerateCode

// OpenDatabase opens the database before running a subcommand.
var OpenDatabase = openDatabase

Expand Down
3 changes: 2 additions & 1 deletion commands/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/mdp/qrterminal/v3"
"github.com/pquerna/otp/totp"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -122,7 +123,7 @@ func readPasswords(db *sql.DB, opts searchOptions) ([]string, error) {
return nil, fmt.Errorf("parsePassword() failed: %s", err)
}

password, err = GenerateTotpCode(sharedSecret, time.Now())
password, err = totp.GenerateCode(sharedSecret, Now())
if err != nil {
return nil, fmt.Errorf("totp.GenerateCode() failed: %s", err)
}
Expand Down
8 changes: 2 additions & 6 deletions commands/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ import (
"io"
"os"
"testing"
"time"

"rsc.io/qr"
)

func GenerateTotpCodeForTesting(secret string, t time.Time) (string, error) {
return "output-from-oathtool", nil
}

func TestSelect(t *testing.T) {
ctx := CreateContextForTesting(t)
expectedMachine := "mymachine"
Expand Down Expand Up @@ -90,7 +85,8 @@ func TestSelectTotpCode(t *testing.T) {
if actualRet != expectedRet {
t.Fatalf("Main() = %q, want %q", actualRet, expectedRet)
}
expectedOutput := "machine: mymachine, service: myservice, user: myuser, password type: TOTP code, password: output-from-oathtool\n"
// TOTP code depends on the 2020 time produced by NowForTesting()
expectedOutput := "machine: mymachine, service: myservice, user: myuser, password type: TOTP code, password: 013567\n"
actualOutput := outBuf.String()
if actualOutput != expectedOutput {
t.Fatalf("actualOutput = %q, want %q", actualOutput, expectedOutput)
Expand Down
4 changes: 0 additions & 4 deletions commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ func CreateContextForTesting(t *testing.T) Context {
GeneratePassword = GeneratePasswordForTesting
t.Cleanup(func() { GeneratePassword = oldGeneratePassword })

oldGenerateTotpCode := GenerateTotpCode
GenerateTotpCode = GenerateTotpCodeForTesting
t.Cleanup(func() { GenerateTotpCode = oldGenerateTotpCode })

oldNow := Now
Now = NowForTesting
t.Cleanup(func() { Now = oldNow })
Expand Down