github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbtest/ui.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 kbtest 5 6 import ( 7 "fmt" 8 "sync" 9 10 "golang.org/x/net/context" 11 12 "github.com/keybase/client/go/libkb" 13 keybase1 "github.com/keybase/client/go/protocol/keybase1" 14 ) 15 16 type GPGTestUI struct { 17 index int 18 } 19 20 func (g *GPGTestUI) SelectKeyAndPushOption(_ context.Context, arg keybase1.SelectKeyAndPushOptionArg) (keybase1.SelectKeyRes, error) { 21 if len(arg.Keys) == 0 { 22 return keybase1.SelectKeyRes{}, fmt.Errorf("no keys in arg") 23 } 24 if g.index >= len(arg.Keys) { 25 return keybase1.SelectKeyRes{}, fmt.Errorf("test index %d outside bounds (num keys = %d)", g.index, len(arg.Keys)) 26 } 27 key := arg.Keys[g.index] 28 return keybase1.SelectKeyRes{KeyID: key.KeyID, DoSecretPush: true}, nil 29 } 30 31 func (g *GPGTestUI) SelectKey(_ context.Context, arg keybase1.SelectKeyArg) (string, error) { 32 if len(arg.Keys) == 0 { 33 return "", fmt.Errorf("no keys in arg") 34 } 35 if g.index >= len(arg.Keys) { 36 return "", fmt.Errorf("test index %d outside bounds (num keys = %d)", g.index, len(arg.Keys)) 37 } 38 key := arg.Keys[g.index] 39 return key.KeyID, nil 40 } 41 42 func (g *GPGTestUI) WantToAddGPGKey(_ context.Context, _ int) (bool, error) { 43 return true, nil 44 } 45 46 func (g *GPGTestUI) ConfirmDuplicateKeyChosen(_ context.Context, _ int) (bool, error) { 47 return true, nil 48 } 49 50 func (g *GPGTestUI) ConfirmImportSecretToExistingKey(_ context.Context, _ int) (bool, error) { 51 return false, nil 52 } 53 54 func (g *GPGTestUI) Sign(_ context.Context, _ keybase1.SignArg) (string, error) { 55 return "", fmt.Errorf("not implemented") 56 } 57 58 func (g *GPGTestUI) GetTTY(_ context.Context) (string, error) { 59 return "", nil 60 } 61 62 // 63 // FakeIdentifyUI 64 // 65 66 type FakeIdentifyUI struct { 67 Proofs map[string]string 68 ProofResults map[string]keybase1.LinkCheckResult 69 User *keybase1.User 70 Confirmed bool 71 Keys map[libkb.PGPFingerprint]*keybase1.TrackDiff 72 DisplayKeyCalls int 73 Outcome *keybase1.IdentifyOutcome 74 StartCount int 75 Token keybase1.TrackToken 76 BrokenTracking bool 77 DisplayTLFArg keybase1.DisplayTLFCreateWithInviteArg 78 DisplayTLFCount int 79 StellarAccounts []keybase1.StellarAccount 80 sync.Mutex 81 } 82 83 var _ libkb.IdentifyUI = (*FakeIdentifyUI)(nil) 84 85 func (ui *FakeIdentifyUI) FinishWebProofCheck(_ libkb.MetaContext, proof keybase1.RemoteProof, result keybase1.LinkCheckResult) error { 86 ui.Lock() 87 defer ui.Unlock() 88 if ui.Proofs == nil { 89 ui.Proofs = make(map[string]string) 90 } 91 ui.Proofs[proof.Key] = proof.Value 92 93 if ui.ProofResults == nil { 94 ui.ProofResults = make(map[string]keybase1.LinkCheckResult) 95 } 96 ui.ProofResults[proof.Key] = result 97 if result.BreaksTracking { 98 ui.BrokenTracking = true 99 } 100 return nil 101 } 102 103 func (ui *FakeIdentifyUI) FinishSocialProofCheck(_ libkb.MetaContext, proof keybase1.RemoteProof, result keybase1.LinkCheckResult) error { 104 ui.Lock() 105 defer ui.Unlock() 106 if ui.Proofs == nil { 107 ui.Proofs = make(map[string]string) 108 } 109 ui.Proofs[proof.Key] = proof.Value 110 if ui.ProofResults == nil { 111 ui.ProofResults = make(map[string]keybase1.LinkCheckResult) 112 } 113 ui.ProofResults[proof.Key] = result 114 if result.BreaksTracking { 115 ui.BrokenTracking = true 116 } 117 return nil 118 } 119 120 func (ui *FakeIdentifyUI) Confirm(_ libkb.MetaContext, outcome *keybase1.IdentifyOutcome) (result keybase1.ConfirmResult, err error) { 121 ui.Lock() 122 defer ui.Unlock() 123 ui.Outcome = outcome 124 result.IdentityConfirmed = outcome.TrackOptions.BypassConfirm 125 result.RemoteConfirmed = outcome.TrackOptions.BypassConfirm && !outcome.TrackOptions.ExpiringLocal 126 return 127 } 128 129 func (ui *FakeIdentifyUI) DisplayCryptocurrency(libkb.MetaContext, keybase1.Cryptocurrency) error { 130 return nil 131 } 132 133 func (ui *FakeIdentifyUI) DisplayStellarAccount(_ libkb.MetaContext, acc keybase1.StellarAccount) error { 134 ui.Lock() 135 defer ui.Unlock() 136 ui.StellarAccounts = append(ui.StellarAccounts, acc) 137 return nil 138 } 139 140 func (ui *FakeIdentifyUI) DisplayKey(_ libkb.MetaContext, ik keybase1.IdentifyKey) error { 141 ui.Lock() 142 defer ui.Unlock() 143 if ui.Keys == nil { 144 ui.Keys = make(map[libkb.PGPFingerprint]*keybase1.TrackDiff) 145 } 146 fp := libkb.ImportPGPFingerprintSlice(ik.PGPFingerprint) 147 148 ui.Keys[*fp] = ik.TrackDiff 149 ui.DisplayKeyCalls++ 150 return nil 151 } 152 func (ui *FakeIdentifyUI) ReportLastTrack(libkb.MetaContext, *keybase1.TrackSummary) error { 153 return nil 154 } 155 func (ui *FakeIdentifyUI) Start(_ libkb.MetaContext, username string, _ keybase1.IdentifyReason, forceDisplay bool) error { 156 ui.Lock() 157 defer ui.Unlock() 158 ui.StartCount++ 159 return nil 160 } 161 func (ui *FakeIdentifyUI) Cancel(_ libkb.MetaContext) error { 162 return nil 163 } 164 func (ui *FakeIdentifyUI) Finish(_ libkb.MetaContext) error { 165 return nil 166 } 167 func (ui *FakeIdentifyUI) Dismiss(_ libkb.MetaContext, _ string, _ keybase1.DismissReason) error { 168 return nil 169 } 170 func (ui *FakeIdentifyUI) LaunchNetworkChecks(_ libkb.MetaContext, id *keybase1.Identity, user *keybase1.User) error { 171 ui.Lock() 172 defer ui.Unlock() 173 ui.User = user 174 return nil 175 } 176 func (ui *FakeIdentifyUI) DisplayTrackStatement(libkb.MetaContext, string) error { 177 return nil 178 } 179 func (ui *FakeIdentifyUI) DisplayUserCard(libkb.MetaContext, keybase1.UserCard) error { 180 return nil 181 } 182 func (ui *FakeIdentifyUI) ReportTrackToken(_ libkb.MetaContext, tok keybase1.TrackToken) error { 183 ui.Token = tok 184 return nil 185 } 186 func (ui *FakeIdentifyUI) SetStrict(b bool) { 187 } 188 func (ui *FakeIdentifyUI) DisplayTLFCreateWithInvite(_ libkb.MetaContext, arg keybase1.DisplayTLFCreateWithInviteArg) error { 189 ui.DisplayTLFCount++ 190 ui.DisplayTLFArg = arg 191 return nil 192 }