Skip to content

Commit

Permalink
Always copy passwords to clipboard is autoclip is set
Browse files Browse the repository at this point in the history
Fixes gopasspw#1255

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz committed May 2, 2020
1 parent a94c675 commit 12529d3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 46 deletions.
1 change: 1 addition & 0 deletions pkg/action/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestCopy(t *testing.T) {

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithAutoClip(ctx, false)
act, err := newMock(ctx, u)
require.NoError(t, err)
require.NotNil(t, act)
Expand Down
1 change: 1 addition & 0 deletions pkg/action/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestFind(t *testing.T) {

ctx := context.Background()
ctx = ctxutil.WithTerminal(ctx, false)
ctx = ctxutil.WithAutoClip(ctx, false)
act, err := newMock(ctx, u)
require.NoError(t, err)
require.NotNil(t, act)
Expand Down
5 changes: 3 additions & 2 deletions pkg/action/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
// Generate and save a password
func (s *Action) Generate(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
ctx = WithClip(ctx, c.Bool("clip"))
force := c.Bool("force")
edit := c.Bool("edit")

Expand Down Expand Up @@ -116,13 +117,13 @@ func (s *Action) generateCopyOrPrint(ctx context.Context, c *cli.Context, name,
)
}

if ctxutil.IsAutoClip(ctx) || c.Bool("clip") {
if ctxutil.IsAutoClip(ctx) || IsClip(ctx) {
if err := clipboard.CopyTo(ctx, name, []byte(password)); err != nil {
return ExitError(ctx, ExitIO, err, "failed to copy to clipboard: %s", err)
}
}

if c.Bool("print") || c.Bool("clip") {
if c.Bool("print") || IsClip(ctx) {
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/action/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestInsert(t *testing.T) {
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithTerminal(ctx, false)
ctx = ctxutil.WithAutoClip(ctx, false)
act, err := newMock(ctx, u)
require.NoError(t, err)
require.NotNil(t, act)
Expand Down
87 changes: 43 additions & 44 deletions pkg/action/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,57 +92,27 @@ func (s *Action) showHandleRevision(ctx context.Context, c *cli.Context, name, r

// showHandleOutput displays a secret
func (s *Action) showHandleOutput(ctx context.Context, name string, sec store.Secret) error {
var content string
var key string
if HasKey(ctx) {
key = GetKey(ctx)
content, err := s.showGetContent(ctx, name, sec)
if err != nil {
return err
}
if content == "" {
return ExitError(ctx, ExitNotFound, store.ErrNoBody, store.ErrNoBody.Error())
}

switch {
case key != "":
val, err := sec.Value(key)
if err != nil {
return s.showHandleYAMLError(ctx, name, key, err)
}
if IsClip(ctx) {
if err := clipboard.CopyTo(ctx, name, []byte(val)); err != nil {
return err
}
if IsOnlyClip(ctx) {
return nil
}
}
content = val
case IsPrintQR(ctx):
return s.showPrintQR(ctx, name, sec.Password())
case IsClip(ctx):
if err := clipboard.CopyTo(ctx, name, []byte(sec.Password())); err != nil {
if IsPrintQR(ctx) {
return s.showPrintQR(ctx, name, content)
}

if IsClip(ctx) || ctxutil.IsAutoClip(ctx) {
if err := clipboard.CopyTo(ctx, name, []byte(content)); err != nil {
return err
}
if IsOnlyClip(ctx) {
return nil
}
fallthrough
default:
switch {
case IsPasswordOnly(ctx):
content = sec.Password()
case ctxutil.IsShowSafeContent(ctx) && !IsForce(ctx):
content = sec.Body()
if content == "" {
if ctxutil.IsAutoClip(ctx) {
out.Yellow(ctx, "Info: %s.", store.ErrNoBody.Error())
out.Yellow(ctx, "Copying password instead.")
return clipboard.CopyTo(ctx, name, []byte(sec.Password()))
}
return ExitError(ctx, ExitNotFound, store.ErrNoBody, store.ErrNoBody.Error())
}
default:
buf, err := sec.Bytes()
if err != nil {
return ExitError(ctx, ExitUnknown, err, "failed to encode secret: %s", err)
}
content = string(buf)
if ctxutil.IsAutoClip(ctx) && !IsClip(ctx) && !IsForce(ctx) {
return nil
}
}

Expand All @@ -151,6 +121,35 @@ func (s *Action) showHandleOutput(ctx context.Context, name string, sec store.Se
return nil
}

func (s *Action) showGetContent(ctx context.Context, name string, sec store.Secret) (string, error) {
// YAML key
if HasKey(ctx) {
key := GetKey(ctx)
val, err := sec.Value(key)
if err != nil {
return "", s.showHandleYAMLError(ctx, name, key, err)
}
return val, nil
}

// first line of the secret only
if IsPasswordOnly(ctx) || IsPrintQR(ctx) || ctxutil.IsAutoClip(ctx) {
return sec.Password(), nil
}

// everything but the first line
if ctxutil.IsShowSafeContent(ctx) && !IsForce(ctx) {
return sec.Body(), nil
}

// everything (default)
buf, err := sec.Bytes()
if err != nil {
return "", ExitError(ctx, ExitUnknown, err, "failed to encode secret: %s", err)
}
return string(buf), nil
}

// showHandleError handles errors retrieving secrets
func (s *Action) showHandleError(ctx context.Context, c *cli.Context, name string, recurse bool, err error) error {
if err != store.ErrNotFound || !recurse || !ctxutil.IsTerminal(ctx) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/action/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestShow(t *testing.T) {
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithTerminal(ctx, false)
ctx = ctxutil.WithAutoClip(ctx, false)
act, err := newMock(ctx, u)
require.NoError(t, err)
require.NotNil(t, act)
Expand Down Expand Up @@ -139,6 +140,7 @@ func TestShowHandleRevision(t *testing.T) {
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithTerminal(ctx, false)
ctx = ctxutil.WithAutoClip(ctx, false)
act, err := newMock(ctx, u)
require.NoError(t, err)
require.NotNil(t, act)
Expand Down Expand Up @@ -170,6 +172,7 @@ func TestShowHandleError(t *testing.T) {
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithTerminal(ctx, false)
ctx = ctxutil.WithAutoClip(ctx, false)
act, err := newMock(ctx, u)
require.NoError(t, err)
require.NotNil(t, act)
Expand Down Expand Up @@ -201,6 +204,7 @@ func TestShowHandleYAMLError(t *testing.T) {
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithTerminal(ctx, false)
ctx = ctxutil.WithAutoClip(ctx, false)
act, err := newMock(ctx, u)
require.NoError(t, err)
require.NotNil(t, act)
Expand All @@ -225,6 +229,7 @@ func TestShowPrintQR(t *testing.T) {
ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithTerminal(ctx, false)
ctx = ctxutil.WithAutoClip(ctx, false)
act, err := newMock(ctx, u)
require.NoError(t, err)
require.NotNil(t, act)
Expand Down

0 comments on commit 12529d3

Please sign in to comment.