github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/engine/gpg_test.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package engine 5 6 import ( 7 "fmt" 8 9 "golang.org/x/net/context" 10 11 "github.com/keybase/client/go/libkb" 12 keybase1 "github.com/keybase/client/go/protocol/keybase1" 13 ) 14 15 type gpgtestui struct { 16 libkb.Contextified 17 index int 18 keyChosenCount int 19 } 20 21 func (g *gpgtestui) SelectKeyAndPushOption(_ context.Context, arg keybase1.SelectKeyAndPushOptionArg) (keybase1.SelectKeyRes, error) { 22 if len(arg.Keys) == 0 { 23 return keybase1.SelectKeyRes{}, fmt.Errorf("no keys in arg") 24 } 25 if g.index >= len(arg.Keys) { 26 return keybase1.SelectKeyRes{}, fmt.Errorf("test index %d outside bounds (num keys = %d)", g.index, len(arg.Keys)) 27 } 28 key := arg.Keys[g.index] 29 return keybase1.SelectKeyRes{KeyID: key.KeyID, DoSecretPush: true}, nil 30 } 31 32 func (g *gpgtestui) SelectKey(_ context.Context, arg keybase1.SelectKeyArg) (string, error) { 33 if len(arg.Keys) == 0 { 34 return "", fmt.Errorf("no keys in arg") 35 } 36 if g.index >= len(arg.Keys) { 37 return "", fmt.Errorf("test index %d outside bounds (num keys = %d)", g.index, len(arg.Keys)) 38 } 39 key := arg.Keys[g.index] 40 return key.KeyID, nil 41 } 42 43 func (g *gpgtestui) WantToAddGPGKey(_ context.Context, _ int) (bool, error) { 44 return true, nil 45 } 46 47 func (g *gpgtestui) ConfirmDuplicateKeyChosen(_ context.Context, _ int) (bool, error) { 48 g.keyChosenCount++ 49 return true, nil 50 } 51 52 func (g *gpgtestui) ConfirmImportSecretToExistingKey(_ context.Context, _ int) (bool, error) { 53 return false, nil 54 } 55 56 func (g *gpgtestui) Sign(ctx context.Context, arg keybase1.SignArg) (string, error) { 57 mctx := g.MetaContext(ctx) 58 fp, err := libkb.PGPFingerprintFromSlice(arg.Fingerprint) 59 if err != nil { 60 return "", err 61 } 62 cli := g.G().GetGpgClient() 63 if err := cli.Configure(mctx); err != nil { 64 return "", err 65 } 66 return cli.Sign(mctx, *fp, arg.Msg) 67 } 68 69 func (g *gpgtestui) GetTTY(_ context.Context) (string, error) { 70 return "", nil 71 } 72 73 func (g *gpgtestui) MetaContext(ctx context.Context) libkb.MetaContext { 74 return libkb.NewMetaContext(ctx, g.G()) 75 } 76 77 type gpgTestUIBadSign struct { 78 gpgtestui 79 } 80 81 func (g *gpgTestUIBadSign) Sign(_ context.Context, arg keybase1.SignArg) (string, error) { 82 return "", libkb.GpgError{M: "Artificial GPG failure for testing"} 83 } 84 85 // doesn't push secret to api server 86 type gpgPubOnlyTestUI struct { 87 *gpgtestui 88 } 89 90 func (g *gpgPubOnlyTestUI) SelectKeyAndPushOption(_ context.Context, arg keybase1.SelectKeyAndPushOptionArg) (keybase1.SelectKeyRes, error) { 91 if len(arg.Keys) == 0 { 92 return keybase1.SelectKeyRes{}, fmt.Errorf("no keys in arg") 93 } 94 key := arg.Keys[0] 95 return keybase1.SelectKeyRes{KeyID: key.KeyID, DoSecretPush: false}, nil 96 }