Skip to content

Commit

Permalink
feat: Add --qrbody flag to encode the entire body of a secret into a …
Browse files Browse the repository at this point in the history
…QR (#2946)

This comes in handy when storing Wireguard configs in gopass as it
allows to import the entire config on a mobile device easily.

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz authored Sep 25, 2024
1 parent 30865a7 commit 1eb3850
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/action/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func ShowFlags() []cli.Flag {
Name: "qr",
Usage: "Print the password as a QR Code",
},
&cli.BoolFlag{
Name: "qrbody",
Usage: "Print the body as a QR Code",
},
&cli.BoolFlag{
Name: "unsafe",
Aliases: []string{"u", "force", "f"},
Expand Down
16 changes: 16 additions & 0 deletions internal/action/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
ctxKeyOnlyClip
ctxKeyAlsoClip
ctxKeyPrintChars
ctxKeyWithQRBody
)

// WithClip returns a context with the value for clip (for copy to clipboard)
Expand Down Expand Up @@ -151,3 +152,18 @@ func GetPrintChars(ctx context.Context) []int {

return mv
}

// WithQRBody returns the context with the value of with QR body set.
func WithQRBody(ctx context.Context, qr bool) context.Context {
return context.WithValue(ctx, ctxKeyWithQRBody, qr)
}

// IsQRBody returns the value of with QR body or the default (false).
func IsQRBody(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyWithQRBody).(bool)
if !ok {
return false
}

return bv
}
13 changes: 13 additions & 0 deletions internal/action/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func showParseArgs(c *cli.Context) context.Context {
if c.IsSet("qr") {
ctx = WithPrintQR(ctx, c.Bool("qr"))
}
if c.IsSet("qrbody") {
ctx = WithQRBody(ctx, c.Bool("qrbody"))
}

if c.IsSet("password") {
ctx = WithPasswordOnly(ctx, c.Bool("password"))
Expand Down Expand Up @@ -197,6 +200,13 @@ func (s *Action) showHandleOutput(ctx context.Context, name string, sec gopass.S
return exit.Error(exit.NotFound, store.ErrEmptySecret, store.ErrEmptySecret.Error())
}

if IsPrintQR(ctx) && IsQRBody(ctx) {
if err := s.showPrintQR(name, body); err != nil {
return err
}

return nil
}
if IsPrintQR(ctx) && pw != "" {
if err := s.showPrintQR(name, pw); err != nil {
return err
Expand Down Expand Up @@ -245,6 +255,9 @@ func (s *Action) showGetContent(ctx context.Context, sec gopass.Secret) (string,
// fallback for old MIME secrets.
fullBody := strings.TrimPrefix(string(sec.Bytes()), secrets.Ident+"\n")

if IsQRBody(ctx) {
return pw, fullBody, nil
}
// first line of the secret only.
if IsPrintQR(ctx) || IsOnlyClip(ctx) {
return pw, "", nil
Expand Down

0 comments on commit 1eb3850

Please sign in to comment.